Madiyor
Madiyor

Reputation: 811

How to change inline button text when it is clicked in telegram bot?

I am pretty new in telegram bot creating. I am using python-telegram-bot module to build a telegram bot. I got stuck on implementing an inline button that changes its text when it is clicked. Can you share some source or way? Thanks in advance.

In short what I mean as follows. For example an inline button with text "1" and when I click I want it to change to "2".

def one(update, context):
    """Show new choice of buttons"""
    query = update.callback_query
    bot = context.bot
    keyboard = [
        [InlineKeyboardButton("3", callback_data=str(THREE)),
         InlineKeyboardButton("4", callback_data=str(FOUR))]
    ]
    keyboard[0][int(query)-1] = InlineKeyboardButton("X", callback_data=str(THREE))
    reply_markup = InlineKeyboardMarkup(keyboard)
    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text="First CallbackQueryHandler, Choose a route",
        reply_markup=reply_markup
    )
    return FIRST

Upvotes: 5

Views: 13264

Answers (1)

Madiyor
Madiyor

Reputation: 811

Finally, I could achieve what I wanted. Maybe a bit long and not cool looking code, but I posted my solution.

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
    Updater, 
    CommandHandler,
    CallbackQueryHandler,
    ConversationHandler)

import logging

FIRST, SECOND = range(2)

counter = 0

keyboard = [[InlineKeyboardButton('Decrement', callback_data='0')],
            [InlineKeyboardButton(f'[{counter}]', callback_data='1')],
            [InlineKeyboardButton('Increment', callback_data='2')]]


def start(update, context):
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(
        'Please choose one of our services\n',
        reply_markup=reply_markup
    )

    return FIRST


def update_and_get_reply_markup(inc_or_dec=None):
    global keyboard
    global counter
    if inc_or_dec == True:
        counter += 1
    else:
        counter -= 1

    keyboard[1][0] = InlineKeyboardButton(
        f'[{counter}]', callback_data='2')

    reply_markup = InlineKeyboardMarkup(keyboard)

    return reply_markup


def increment(update, context):
    query = update.callback_query

    reply_markup = update_and_get_reply_markup(inc_or_dec=True)
    bot = context.bot
    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text="Please choose one of our services\n",
        reply_markup=reply_markup
    )

    return FIRST


def decrement(update, context):
    query = update.callback_query

    reply_markup = update_and_get_reply_markup()
    bot = context.bot
    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text="Please choose one of our services\n",
        reply_markup=reply_markup
    )

    return FIRST


def main():
    updater = Updater(
        'TOKEN', use_context=True)
    dp = updater.dispatcher
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [CallbackQueryHandler(decrement, pattern='^'+str(0)+'$'),
                    CallbackQueryHandler(increment, pattern='^'+str(2)+'$')]
        },
        fallbacks=[CommandHandler('start', start)]
    )

    dp.add_handler(conv_handler)
    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

Upvotes: 8

Related Questions