0

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?

2 Answers 2

0

It works for me. I mean, the handler is called. But there is an error:

    bot.send_message(callback.message.chat.id, f"YOU PRESSED A BUTTON")
Sign up to request clarification or add additional context in comments.

Comments

0

use call instead of callback. It works

3 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
is issue solved?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.