如何解决telebot中callback_query_handlers的列表不可调用错误?
问题解决:Telebot回调处理器报错 'list' object is not callable
你遇到的错误是因为回调查询装饰器的名称写错了:你用了@bot.callback_query_handlers(复数),但正确的装饰器是@bot.callback_query_handler(单数)。
bot.callback_query_handlers是TeleBot实例中存储所有回调处理器的列表,不是可调用的装饰器函数,所以当你把它当作装饰器使用时,就会抛出“列表对象不可调用”的错误。
修正后的代码
import telebot from telebot import types bot = telebot.TeleBot("MY_TOKEN") @bot.message_handler(commands=['show_buttons']) def bar(message): markup = types.InlineKeyboardMarkup(row_width=2) button_m = types.InlineKeyboardButton('М', callback_data='cМ') button_i = types.InlineKeyboardButton('I', callback_data='cI') markup.add(button_m, button_i) bot.send_message(message.chat.id, text='SOME_TXT', reply_markup=markup) @bot.message_handler(commands=['foo']) def foo(message): bot.reply_to(message, 'foo') # 修正装饰器名称:去掉末尾的s @bot.callback_query_handler(func=lambda call: True) def answer(callback): print('It filters!') if callback.message: print('that works!') bot.infinity_polling()
修改后,装饰器会正确注册你的回调处理函数,点击按钮时就能触发对应的逻辑了。
内容的提问来源于stack exchange,提问作者DanVader
相关产品推荐
相关产品推荐

