[英]python-telegram-bot ERROR 'CallbackContext' object has no attribute 'message'
Below is an example of the telegram bot.以下是电报机器人的示例。 Help me understand why function message_handler works in my code, and greet_user - not working.帮助我理解为什么函数message_handler在我的代码中起作用,而greet_user - 不起作用。
There is a error from error_log decorator: 'CallbackContext' object has no attribute 'message'
error_log 装饰器出错: 'CallbackContext' object has no attribute 'message'
from telegram import Update, Bot
from telegram.ext import Updater, MessageHandler, Filters, CallbackContext, CommandHandler
from telegram.utils.request import Request
def log_error(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as exc:
print(f'Error: {exc}')
raise exc
return wrapper
def message_handler(update: Update, context: CallbackContext):
update.message.reply_text(text='Example')
@log_error
def greet_user(bot, update):
update.message.reply_text('hello')
def main():
req = Request(connect_timeout=0.5)
t_bot = Bot(
request=req,
token='xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
base_url='https://telegg.ru/orig/bot',
)
updater = Updater(bot=t_bot, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', greet_user))
dp.add_handler(MessageHandler(filters=Filters.all, callback=message_handler))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
I figured out what the problem was .我想出了问题所在。 If we use use_context=True
in Updater
, we should pass arguments to the function this way:如果我们在Updater
使用use_context=True
,我们应该以这种方式将参数传递给函数:
def greet_user(`update: Update, context: CallbackContext`):
update.message.reply_text('hello')
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-12.0 https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-12.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.