0

I'm getting familiar with Telegram API and wondering if there is any possibility to update a button title on a message from bot after a user licks on it?

Here is my code example:

const TelegramBot = require('node-telegram-bot-api');

// Insert token from Telegram BotFather
const token = '...';

// Enable server polling
const bot = new TelegramBot(token, {polling: true});

let categoryButtons = [
    [
        { text: 'A', callback_data: 'A' },
        { text: 'B', callback_data: 'B' }
    ],

    [
        { text: 'C', callback_data: 'C' },
        { text: 'D', callback_data: 'D' }
    ],

    [
        { text: 'E', callback_data: 'E' }
    ]
]

bot.onText(/start/, (msg, match) => {
    const fromId = msg.from.id;

    sendGreetingMessage(fromId);

    bot.on('callback_query', (data) => {
        switch(data.data) {
            case 'A':
                addCheckSignToTitle(fromId, data);
                break;
            case 'B':
                addCheckSignToTitle(fromId, data);
                break;
            case 'C':
                addCheckSignToTitle(fromId, data);
                break;
            case 'D':
                addCheckSignToTitle(fromId, data);
                break;
            case 'E':
                break;
        }
    });

    bot.on('polling_error', (msg) => {console.log(msg)})
});

sendGreetingMessage = (fromId) => {
    const mainTitle = '...';
    const options = {
        reply_markup: {
            inline_keyboard: categoryButtons
        }
    }

    bot.sendMessage(fromId, mainTitle, options);
}

addCheckSignToTitle = (fromId, data) => {
    const checkSign = '...';
    for(let i = 0, arrayLength = categoryButtons.length; i < arrayLength; i++) {
        for(let j = 0, itemLength = categoryButtons[i].length; j < itemLength; j++) {
            if(categoryButtons[i][j].callback_data === data.data) {
                categoryButtons[i][j].text = checkSign + ' ' + categoryButtons[i][j].text;

                let chat_id = data.message.chat.id;
                let message_id = data.message.message_id;
                // let reply_markup = InlineKeyboardMarkup(util.build_menu(categoryButtons))

                // bot.editMessageReplyMarkup(chat_id, message_id, reply_markup);
            }
        }
    }
}

How properly work with InlineKeyboardMarkup?

I'm getting an error that reply_markup is not correctly defined. I understood that I have to import InlineKeyboardMarkup from somewhere but couldn't find from where.

Could someone experienced please advise me how to update a button title after clicking on that?

Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
Ivan Burzakovskyi
  • 693
  • 1
  • 8
  • 25

0 Answers0