python-telegram-bot 出错

Eror in python-telegram-bot

我试图用 python 在电报中创建一个机器人,但它不起作用。我的代码如下:

from telegram.ext import Updater, CommandHandler
updater = Updater(Token)
def start(bot,update) :
    chat_id = update.message.chat_id
    bot.sendMessage(chat_id, 'Hello')
start_command = CommandHandler('start' , start)
updater.dispatcher.add_handler(start_command)
updater.start_polling()
updater.idle()

并且 error_log 装饰器出现错误:

No error handlers are registered, logging exception. Traceback (most recent call last): File "c:\users\Parsa\anaconda3\lib\site-packages\telegram\ext\dispatcher.py", line 442, in process_update handler.handle_update(update, self, check, context) File "c:\users\Parsa\anaconda3\lib\site-packages\telegram\ext\handler.py", line 160, in handle_update return self.callback(update, context) File "", line 4, in start chat_id = update.message.chat_id AttributeError: 'CallbackContext' object has no attribute 'message'

我该怎么办?

我想你对开始的定义是错误的。试试这个:

from telegram.ext import Updater, CommandHandler
updater = Updater(Token)
def start(update, _) :
    update.message.reply_text('Hello')
start_command = CommandHandler('start' , start)
updater.dispatcher.add_handler(start_command)
updater.start_polling()
updater.idle()