首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >发生ConnectionError。项目通过Telegram Bot API连接

发生ConnectionError。项目通过Telegram Bot API连接
EN

Stack Overflow用户
提问于 2018-05-30 06:49:49
回答 1查看 609关注 0票数 1
ERROR - TeleBot: "ConnectionError occurred, args=(ProtocolError('Connection 
aborted.', ConnectionRefusedError(10061, 'Подключение не установлено, т.к. 
конечный компьютер отверг запрос на подключение', None, 10061, None)),)

有人知道问题出在哪里吗?

机器人需要向用户询问他想玩的游戏: Hangman或TicTacToe (它们是与它们相关的其他文件)。我几周前开始使用Telegram,所以我对它还很陌生。

代码:

import telebot
import constants

bot = telebot.TeleBot(constants.token)

@bot.message_handler(content_types=["start"])
def start(m):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add(*[types.KeyboardButton(name) for name in ['Hangman', 'Tic Tac Toe']])
    msg = bot.send_message(m.chat.id, 'What do you choose?',
        reply_markup=keyboard)
    bot.register_next_step_handler(msg, name)

def name(m):
    if m.text == 'Hangman':
        import hangman
        hangman
    elif m.text == 'Tic Tac Toe':
        import TicTacToe
        TicTacToe

bot.polling()

绞刑器代码是:

PICS = ['''

  _____
  |   |
      |
      |
      |
      |
~~~~~~~~''','''

  _____
  |   |
  O   |
      |
      |
      |
~~~~~~~~''','''

  _____
  |   |
  O   |
  |   |
      |
      |
~~~~~~~~''','''

  _____
  |   |
  O   |
 /|   |
      |
      |
~~~~~~~~''','''

  _____
  |   |
  O   |
 /|\  |
      |
      |
~~~~~~~~''','''

  _____
  |   |
  O   |
 /|\  |
 /    |
      |
~~~~~~~~''','''

  _____
  |   |
  O   |
 /|\  |
 / \  |
      |
~~~~~~~~''']

keywords = 'lyceum human king guitar music chair case pencil table memes book apple phone computer program boulevard dream university physics mathematics algebra analysis geometry chemistry biology decision property grammar hedgehog progress'.split()

import random

def Random(list):
    i = random.randint(0, len(list) - 1)
    return list[i]

def Again():
        print('Again? (yes/no)')
        inp = input().lower()
        if inp == 'yes':
            return True
        else:
            return False

def Info(PICS, wrong, right, keyword):
    print(PICS[len(wrong)])
    print()
    print('Wrong letters:', end=' ')
    for letter in wrong:
        print(letter, end=' ')
    print()
    print('Word:', end = ' ')
    star = '*' * len(keyword)
    for j in range(len(keyword)):
        if keyword[j] in right:
            star = star[:j] + keyword[j] + star[j+1:]
    for letter in star:
        print(letter, end=' ')
    print()

def Done(doneword):
    while True:
        print('Put a letter:')
        word = input().lower()
        if word in doneword:
            print ('You have tried this one. Choose another letter')
        elif word not in 'mnbvcxzlkjhgfdsapoiuytrewq':
            print('Please, put a small latin letter')
        elif len(word) != 1:
            print('Your letter:')
        else:
            return word


#start
right = ''
wrong = ''
keyword = Random(keywords)
end = False
while True:
    Info(PICS, wrong, right, keyword)
    word = Done(wrong + right)
    if word in keyword:
        right = right + word
        all = True
        for a in range(len(keyword)):
            if keyword[a] not in right:
                all = False
                break
        if all:
            print('Win!')
            end = True
    else:
        wrong = wrong + word
        if len(wrong) == len(PICS) - 1:
            Info(PICS, wrong, right, keyword)
            print('You lose. Keyword:'+keyword+'"')
            end = True
    if end:
        if Again():
            wrong = ''
            right = ''
            end = False
            keyword = Random(keywords)
        else:
            break

游戏没有任何错误,只有机器人有错误。

Traceback (most recent call last):
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
   body=body, headers=headers)
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\connectionpool.py", line 341, in _make_request
self._validate_conn(conn)
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\connectionpool.py", line 761, in _validate_conn
conn.connect()
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\connection.py", line 204, in connect
conn = self._new_conn()
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\util\connection.py", line 88, in create_connection
raise err
  File "C:\Users\Соня\AppData\Roaming\Python\Python36\site-packages\requests\packages\urllib3\util\connection.py", line 78, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Подключение не установлено, т.к. конечный компьютер отверг запрос на подключение
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-30 08:07:12

如果TicTacToehangman是函数,则需要像调用TicTacToe()hangman()一样调用它们

如果它们是包含函数的类,则语法略有不同,但如果不发布这些模块,则很难判断

张贴这些模块会很有帮助。

另外,根据python的PEP8样式指南- https://www.python.org/dev/peps/pep-0008/#imports

导入始终放在文件的顶部,紧跟在任何模块注释和文档字符串之后,模块全局变量和常量之前。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50594060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档