【问题标题】:Eror in python-telegram-botpython-telegram-bot 中的错误
【发布时间】:2021-06-21 08:47:26
【问题描述】:

我尝试使用 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装饰器的错误:

没有注册错误处理程序,记录异常。回溯(最 最近通话最后):文件 "c:\users\Parsa\anaconda3\lib\site-packages\telegram\ext\dispatcher.py", 第 442 行,在 process_update 中 handler.handle_update(更新、自我、检查、上下文)文件“c:\users\Parsa\anaconda3\lib\site-packages\telegram\ext\handler.py”, 第 160 行,在句柄更新中 return self.callback(update, context) File "", line 4, in start chat_id = update.message.chat_id AttributeError: 'CallbackContext' object has no attribute 'message'

我该怎么办?

【问题讨论】:

  • 错误信息告诉你Updater实例update,没有message属性:update.message不存在。根据教程,update.effective_chat.id 有一个聊天 ID。

标签: python bots telegram telegram-bot python-telegram-bot


【解决方案1】:

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

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()

【讨论】:

  • 谢谢你,bot 我还有一个问题。其他方法如何找到chat_id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2022-11-11
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多