Python Telegram bot错误语法错误:语法无效

时间:2019-07-08 09:09:39

标签: python python-2.7 pip telegram chatbot

我在python中创建了一个与DialogFlow通信的简单电报机器人。这是代码

#!/usr/bin/env python
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import apiai, json
updater = Updater (token = '')
dispatcher = updater.dispatcher
# Processing commands
def startCommand (bot, update):
    bot.send_message (chat_id = update.message.chat_id, text = '')
def textMessage (bot, update):
    request = apiai.ApiAI (''). text_request()
request.lang = 'it'
request.session_id = 'TestBot'
request.query = update.message.text
responseJson = json.loads ( request.getresponse (). read (). decode ('utf-8'))
response = responseJson ['result'] ['fulfillment'] ['speech']

if response:
    bot.send_message (chat_id = update.message.chat_id, text = response)
else:
    bot.send_message (chat_id = update.message.chat_id, text = '')

start_command_handler = CommandHandler ('start', startCommand)
text_message_handler = MessageHandler (Filters.text, textMessage)

dispatcher.add_handler (start_command_handler)
dispatcher.add_handler (text_message_handler)

updater.start_polling (clean = True)

updater.idle ()

当我尝试启动它时,出现此错误

Traceback (most recent call last):
  File "testbot.py", line 2, in <module>
    from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  File "/usr/local/lib/python2.7/dist-packages/python_telegram_bot-12.0.0b1-py2.7.egg/telegram/ext/__init__.py", line 28, in <module>
    from .updater import Updater
  File "/usr/local/lib/python2.7/dist-packages/python_telegram_bot-12.0.0b1-py2.7.egg/telegram/ext/updater.py", line 33, in <module>
    from telegram.utils.webhookhandler import (WebhookServer, WebhookAppClass)
  File "/usr/local/lib/python2.7/dist-packages/python_telegram_bot-12.0.0b1-py2.7.egg/telegram/utils/webhookhandler.py", line 27, in <module>
    from tornado.httpserver import HTTPServer
  File "/usr/local/lib/python2.7/dist-packages/tornado-6.0.3-py2.7-linux-x86_64.egg/tornado/httpserver.py", line 144
    def __init__(self, *args: Any, **kwargs: Any) -> None:
                            ^
SyntaxError: invalid syntax

我已经以这种方式安装了pip

sudo apt-get install python-pip

1 个答案:

答案 0 :(得分:1)

此:*args: Any是仅Python3的语法。您已经安装了tornado的最新版本,该版本仅支持Python 3+。对于Python 2.7,您需要降级:

pip install -U tornado==5.1.1

5.1.1是当前支持Python 2.7的最新版本。