发生ConnectionError。项目与Telegram Bot API连接

时间:2018-05-29 22:49:49

标签: python-3.x pycharm telegram telegram-bot python-telegram-bot

ERROR - TeleBot: "ConnectionError occurred, args=(ProtocolError('Connection 
aborted.', ConnectionRefusedError(10061, 'Подключение не установлено, т.к. 
конечный компьютер отверг запрос на подключение', None, 10061, None)),)

该项目与Telegram Bot API相关联。有人知道问题出在哪里吗?

Bot需要向用户询问他想要玩的游戏: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()

Hangman代码是:

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] Подключение не установлено, т.к. конечный компьютер отверг запрос на подключение

1 个答案:

答案 0 :(得分:1)

如果TicTacToehangman是函数,那么您需要像TicTacToe()hangman()

那样调用它们

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

发布这些模块会很有帮助。

另一方面,根据python的PEP 8样式指南 - https://www.python.org/dev/peps/pep-0008/#imports

  

导入总是放在文件的顶部,就在任何模块注释和文档字符串之后,以及模块全局变量和常量之前。