Binding a Callback to an InlineKeyboardButton in Python-Telegram-Bot v20+?

Working with python telegram bot v20+, how can I trigger distinct functions from individual InlineKeyboardButtons? For example, the ‘First’ button should initiate a support function.

from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, ContextTypes

async def initiate(update: Update, context: ContextTypes.DEFAULT_TYPE):
    buttons = [
        [InlineKeyboardButton('First', callback_data='first'), InlineKeyboardButton('Second', callback_data='second')],
        [InlineKeyboardButton('Third', callback_data='third')]
    ]
    keyboard = InlineKeyboardMarkup(buttons)
    await update.message.reply_text('Welcome!')
    await update.message.reply_text('Please choose an option:', reply_markup=keyboard)

async def handle_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
    callback = update.callback_query
    await callback.answer()
    await callback.edit_message_text(text=f'You selected: {callback.data}')
    if callback.data == 'first':
        await callback.message.reply_text('Support function has been triggered!')

hey, i solved it by mapping keys directly to funcions rather than many if’s. so when the callback data is ‘first’, you call its own function. works good and cleans up the callback handler a bit!

An alternative approach is to use a dispatcher or routing function that maps callback values to their respective handler functions in a more organized manner. In my projects, I found that setting up a dictionary where keys represent the callback data and values are the corresponding asynchronous functions can simplify maintenance and debugging. This pattern reduces nested if-else conditions and allows for easy extension when additional buttons or features are implemented. It also makes unit testing individual functions more straightforward, leading to more robust code.