PyTelegrambotApi — @callback_query_handler does not work

17 views Asked by At

I'm trying to create my first telegram bot with PyTelegrambotApi, but I encountered a weird problem: I get no callback after I press any inline buttons. Nothing happens. According to the documentation and articles, you're supposed to use callback_query_handler decorator, but I never succeeded to get any callback ever.

Here's the code:

import telebot
from loguru import logger
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup

from config_data.config import BOT_TOKEN
from response_templates.response_templates import BOT_RESPONSE_TEMPLATES

bot = telebot.TeleBot(BOT_TOKEN, parse_mode=None)

website_choice_keyboard = InlineKeyboardMarkup(row_width=2)
website_choice_buttons = [InlineKeyboardButton("Yes", callback_data="cb_yes"),
                           InlineKeyboardButton("No", callback_data="cb_no")]
website_choice_keyboard.add(*website_choice_buttons)


logger.add('logs/main_log.log', format="{time:MMMM D, YYYY > HH:mm:ss} | {level} | {message} | {extra}",
           level="DEBUG", rotation="60 MB", retention="7 days")


@bot.message_handler(commands=['help',])
def process_help_command(message):
    bot.send_message(message.chat.id, BOT_RESPONSE_TEMPLATES['/help'], reply_markup=None)


@bot.message_handler(commands=['start',])
def process_start_command(message):
    bot.send_message(message.chat.id, BOT_RESPONSE_TEMPLATES['/start'], reply_markup=website_choice_keyboard)


@bot.callback_query_handler(func=lambda call: call.data)
def process_callback_query(callback):
    print("BUTTON PRESSED")
    if callback.message:
        bot.send_message(callback.message.chat_id, f"YOU PRESSED A BUTTON")


bot.infinity_polling()

The function process_callback_query is never called. What am I doing wrong?

0

There are 0 answers