1

InlineKeyboardButton with callback_data doesn't work. When I click on it, nothing happens.

Here is how I create the button:

from django.apps import apps
from asgiref.sync import sync_to_async
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
from telegram.ext import CommandHandler, ApplicationBuilder, MessageHandler, filters, CallbackQueryHandler

async def start(self, update: Update, context):
    keyboard = InlineKeyboardMarkup([
        [InlineKeyboardButton("Already registred", callback_data="already_registered")],
        [InlineKeyboardButton("Register", web_app=WebAppInfo(url=WEB_APP_URL))]
    ])

    await update.message.reply_text(
        "Welcome",
        reply_markup=keyboard
    )
    return

This is the callback query handler:

async def button(self, update: Update, context):
    query = update.callback_query
    if not query:
        return

    await query.answer()

    if query.data == "already_registered":
        await context.bot.send_message(
            chat_id=update.message.chat_id,
            text='Good'
        )
        return

and this is how I run:

def run(self):
    TOKEN = apps.get_model('app.Config').objects.get_mailing_tg_bot_token()
    app = ApplicationBuilder().token(TOKEN).build()
    app.add_handler(CommandHandler('start', self.start))
    app.add_handler(CallbackQueryHandler(self.button))
0

1 Answer 1

0

When I run it in console (without django) and click button "Already registred" then I see error

chat_id=update.message.chat_id, 

AttributeError: 'NoneType' object has no attribute 'chat_id'

It seems message is None. Maybe InlineKeyboardButton doesn't send message.

But in documentation you can see example inlinekeyboard.py and it uses

await query.edit_message_text(text= ...)

instead of await context.bot.send_message(...) to send message.
And it doesn't need chat_id. And it works for me.

async def button(update: Update, context):
    query = update.callback_query
    if not query:
        return

    await query.answer()

    if query.data == "already_registered":
        await query.edit_message_text(text='Good')
        #await context.bot.send_message(
        #    chat_id=update.message.chat_id,
        #    text='Good'
        #)

EDIT:

update.message is None but exists query.message and it has query.message.chat.id and this also works for me:

        await context.bot.send_message(
        #    chat_id=update.message.chat_id,
            chat_id=query.message.chat.it
            text='Good'
        )
Sign up to request clarification or add additional context in comments.

2 Comments

I added print('it is work') as the first line of the callback handler function and ran the bot locally. However, after clicking the first button ("I am already registered"), nothing happened. It seems that the bot does not receive a callback_query when the button is clicked and I don't understand why?
maybe you have something else in code which makes problem. Some modules for telegram may catch all errors and hide them - so it can work all time even if there are mistakes. It may need to change settings in logging to see all errors. python - How to log errors from the Telegram bot console - Stack Overflow

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.