AttributeError: 'CallbackContext' ('Update') 对象没有属性 'message' [英] AttributeError: 'CallbackContext' ('Update') object has no attribute 'message'

查看:82
本文介绍了AttributeError: 'CallbackContext' ('Update') 对象没有属性 'message'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的 tg-bot 转移到新服务器后出现问题.完全不知道为什么我有错误.好像我丢失了一些要安装的 python 数据包.在操作系统参数相同的旧服务器中,我对这个机器人没有任何问题.

I have a problem after transfer my tg-bot to new server. Absolutely no idea why I have error. It seems like I lost some python packets to install. In old server with identical parameters of OS I haven't any problems with this bot.

对不起,如果我的错误幼稚.我试着用谷歌搜索我的错误,但没有答案.

Sorry if my mistake childish. I try to google my error and haven't answers.

代码 1:

def start(bot, update):
    if update.message.from_user.username == AAA:
        bot.sendMessage(chat_id=update.message.chat_id, text="Text_one")
    else:
        bot.sendMessage(chat_id=update.message.chat_id, text="Text_two")
...
updater = Updater(token=bot_token)

start_handler = CommandHandler('start', start)
updater.dispatcher.add_handler(start_handler)

错误 1:

File "/usr/local/lib/python3.7/site-packages/telegram/ext/dispatcher.py", line 425, in process_update handler.handle_update(update, self, check, context)
File "/usr/local/lib/python3.7/site-packages/telegram/ext/handler.py", line 145, in handle_update return self.callback(update, context)
File "./bot.py", line 43, in start
if update.message.from_user.username == AAA:
AttributeError: 'CallbackContext' object has no attribute 'message'

代码 2:

def rating(bot, update):
    bot.send_sticker(chat_id,'some_sticker_id')
...
rating_handler = CommandHandler('rating', rating)
updater.dispatcher.add_handler(rating_handler)

错误 2:

File "/usr/local/lib/python3.7/site-packages/telegram/ext/dispatcher.py", line 425, in process_update handler.handle_update(update, self, check, context)
File "/usr/local/lib/python3.7/site-packages/telegram/ext/handler.py", line 145, in handle_update return self.callback(update, context)
File "./bot.py", line 108, in rating
bot.send_sticker(chat_id, 'some_sticker_id')
AttributeError: 'Update' object has no attribute 'send_sticker'

推荐答案

处理程序签名不正确:

def start(update, context):
   context.bot.sendMessage(chat_id=update.message.chat_id, 'Text One')

updater.dispatcher.add_handler(CommandHandler('start', start))

这篇关于AttributeError: 'CallbackContext' ('Update') 对象没有属性 'message'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆