skip to Main Content

I am making a telegram-bot in NodeJS. This is a code snippet I am having an issue with:

    let counter = 0
    
    bot.onText(/flexbox (.+)/i, async (msg, match) => {
    console.log(msg)
    console.log(match)
    const chatId = msg.chat.id;
    bot.sendMessage(msg.from.id, 'Original Text', {
        reply_markup: {
            inline_keyboard: [
                [
                    {
                        text: `sample text`,
                        callback_data: 'callbackData',
                        url: `https://example.com`,
                    }
                ]
            ]
        }
    });
    bot.on('callback_query', function onCallbackQuery(callbackQuery) {
      // increment counter when everytime the button is pressed
      counter = counter + 1
      console.log(counter)
    });

So basically what I am trying to achieve is that whenever the user clicks on the button, I want to increment the counter so that I can have a track/count of the total button clicks.
The callback function is not getting triggered at all if I use the field url in the inline_keyboard. If I remove the url field the callback is triggered.

Can someone help me achieve this functionality?

2

Answers


  1. The API Documnetation explains that exactly one of the fields data or game_short_name will be present in a callback_query update. This means that the bot gets notified if and only if the button had a data field or a callback_game defined for it. Since your button has neither of them, pressing the button will not notify the bot.

    Login or Signup to reply.
  2. Please remove the tag py-telegram-bot-api, py is for python.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search