Search code examples
telegramtelegram-botpython-3.9telebot

How to avoid the uncallable list error in callback_query_handlers?


I am making a telegram bot using telebot. This bot has to send a message with buttons. To process the callback data I use callback_query_handlers, but Python shows such a mistake when I start the code:

Traceback (most recent call last):
  File "C:\MY_FILE", line 124, in <module>
    @bot.callback_query_handlers(func=lambda call: True)
TypeError: 'list' object is not callable

Here's the code:

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')
@bot.callback_query_handlers(func=lambda call: True)
def answer(callback):
    print('It filters!')
    if callback.message:
        print('that works!')
bot.infinity_polling()

Solution

  • You are using:

    @bot.callback_query_handlers(func=lambda call: True)
    

    But the correct handler is:

    @bot.callback_query_handler(func=lambda call: True)
    

    So it's singular, without the s.


    When in doubt, I'd recommend browsing through the official examples.