1

I am new to telegram inline bot. I have no clue how I can retrieve the callback function of telegram's inlinekeyboardbuttons. Would be forever greatful if someone can explain this to me how I could retrieve user response.

def options(update, context):
button_list = []
for each in ["yes", "no"]:
    button_list.append(InlineKeyboardButton(each, callback_data=each))
reply_markup = InlineKeyboardMarkup(build_menu(button_list))
context.bot.send_message(chat_id=update.message.chat_id,
                         text="Option Selected:",
                         reply_markup=reply_markup)

def build_menu(buttons, n_cols=1, header_buttons=None, footer_buttons=None):
"""
Returns a list of inline buttons used to generate inlinekeyboard responses

:param buttons: `List` of InlineKeyboardButton
:param n_cols: Number of columns (number of list of buttons)
:param header_buttons: First button value
:param footer_buttons: Last button value
:return: `List` of inline buttons
"""
menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
if header_buttons:
    menu.insert(0, header_buttons)
if footer_buttons:
    menu.append(footer_buttons)
return menu
6
  • I don't understand what is your problem. Commented Jun 14, 2021 at 12:05
  • you can't retrive callback. You can only assign callback to buttons and telegram will run this callback when you press button. And if you want to access some value from this function then you have to put value in global variable. OR you have to run all code directly inside callback and use this value directly in callback Commented Jun 14, 2021 at 12:09
  • Telebot: how to retrieve InlineKeyboardButton callback data? Commented Jun 14, 2021 at 12:17
  • what module do you use telegram, telebot or other? Commented Jun 14, 2021 at 12:23
  • I used the telegram module Commented Jun 14, 2021 at 12:36

1 Answer 1

2

You have to use CallbackQueryHandler to assing function to data

If you use pattern .* then it will get all data in one function

def handle_callback_query(update, context):
    print(update.callback_query.data)
    context.bot.send_message(chat_id=update.effective_chat.id, 
                             text='[handle_callback_query] callback data: ' + update.callback_query.data)

dispatcher.add_handler(CallbackQueryHandler(handle_callback_query, pattern='.*'))

You can also use pattern to assing different functions

def query_yes(update, context):
    print(update.callback_query.data)
    context.bot.send_message(chat_id=update.effective_chat.id, 
                             text='[query_yes] callback data: ' + update.callback_query.data)

def query_no(update, context):
    print(update.callback_query.data)
    context.bot.send_message(chat_id=update.effective_chat.id, 
                             text='[query_no] callback data: ' + update.callback_query.data)

dispatcher.add_handler(CallbackQueryHandler(query_yes, pattern='^yes$'))
dispatcher.add_handler(CallbackQueryHandler(query_no,  pattern='^no$'))

Minimal working example.

import os
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup

# --- init ---

TOKEN = os.getenv('TELEGRAM_TOKEN')

updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher

# --- commands ---

def options(update, context):
    button_list = []
    for each in ["yes", "no"]:
        button_list.append(InlineKeyboardButton(each, callback_data=each))
    reply_markup = InlineKeyboardMarkup(build_menu(button_list))
    context.bot.send_message(chat_id=update.message.chat_id,
                             text="Option Selected:",
                             reply_markup=reply_markup)
    
def build_menu(buttons, n_cols=1, header_buttons=None, footer_buttons=None):
    """
    Returns a list of inline buttons used to generate inlinekeyboard responses
    
    :param buttons: `List` of InlineKeyboardButton
    :param n_cols: Number of columns (number of list of buttons)
    :param header_buttons: First button value
    :param footer_buttons: Last button value
    :return: `List` of inline buttons
    """
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    if header_buttons:
        menu.insert(0, header_buttons)
    if footer_buttons:
        menu.append(footer_buttons)
    return menu
dispatcher.add_handler(CommandHandler('options', options))

# --- one function for all data ---

def handle_callback_query(update, context):
    print(update.callback_query.data)
    context.bot.send_message(chat_id=update.effective_chat.id, 
                             text='[handle_callback_query] callback data: ' + update.callback_query.data)

#dispatcher.add_handler(CallbackQueryHandler(handle_callback_query, pattern='.*'))

# --- different functions for different data ---

def query_yes(update, context):
    print(update.callback_query.data)
    context.bot.send_message(chat_id=update.effective_chat.id, 
                             text='[query_yes] callback data: ' + update.callback_query.data)

def query_no(update, context):
    print(update.callback_query.data)
    context.bot.send_message(chat_id=update.effective_chat.id, 
                             text='[query_no] callback data: ' + update.callback_query.data)

dispatcher.add_handler(CallbackQueryHandler(query_yes, pattern='^yes$'))
dispatcher.add_handler(CallbackQueryHandler(query_no,  pattern='^no$'))

# --- start ---

print('Starting ...')    
updater.start_polling()
updater.idle()
Sign up to request clarification or add additional context in comments.

Comments

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.