pythonpython-3.xcallbackbotspy-telegram-bot-api

function @bot.callback_query_handler doesn't work


This is my first telegram bot and I ran into a problem. Function @bot.callback_query_handler doesn't start, although the bot outputs buttons and the user can click on them.

from telebot import types
from loader import bot
import time

current_year = time.localtime().tm_year
year = types.InlineKeyboardMarkup(row_width=2)

btn1 = types.InlineKeyboardButton(current_year, callback_data='btn1')
btn2 = types.InlineKeyboardButton(current_year + 1, callback_data='btn2')
year.add(btn1, btn2)


@bot.callback_query_handler(func=lambda call: True)
def check_callback(callback):
    if callback.data == 'btn1':
        bot.send_message(callback.message.chat.id, f'You choose {current_year} year')
    elif callback.data == 'btn2':
        bot.send_message(callback.message.chat.id, f'You choose {current_year + 1} year')

I read the documentation for pyTelegrambotApi, but I didn't find anything that would help solve this problem. If anyone knows what to do, please help


Solution

  • You gave call: True but here your gave callback and callback.data. Just change it to callback: True. Rewrite as:

    @bot.callback_query_handler(func=lambda callback: True)
    def check_callback(callback):
    ......