node-telegram-bot-api: Inline Keyboard, Callback_query not being called

It is amazing tool for NodeJS.

But I am stuck with in-line keyboard.

bot.onText(/(.+)$/, function (msg, match) {
    const opts = {
    reply_markup: {
      inline_keyboard: [
        [
          {
            text: 'Edit Text',
            callback_data: 'edit'
          }
        ]
      ]
    }
  };
  bot.sendMessage(msg.from.id, 'Original Text', opts);
});

bot.on("callback_query", function(callbackQuery) {
    // 'callbackQuery' is of type CallbackQuery
    console.log(callbackQuery);
});

Referring to the above code, callback_query is not executed upon clicking Edit Text inline-button

How can i solve this?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 24 (2 by maintainers)

Most upvoted comments

I found the problem, @GochoMugo… in my package.json I had “node-telegram-bot-api”: “^0.21.1”, changed the version and it’s worked! Thanks!

const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true });

bot.onText(/\/callbackquery/, (msg, match) => {
    bot.sendMessage(msg.chat.id, "Click a button won't you!", {
        "reply_markup": {
            "inline_keyboard": [
                [
                    {
                        text: "Click me!",
                        callback_data: "click",
                    },
                ],
            ],
        },
    });
});


bot.on("callback_query", (callbackQuery) => {
    const msg = callbackQuery.message;
    bot.answerCallbackQuery(callbackQuery.id)
        .then(() => bot.sendMessage(msg.chat.id, "You clicked!"));
});

Works for me.

screenshot

@ravi72munde I believe the latest version on npm already supports callback queries, etc.


OP has not reacted to any comments. I’ll go ahead and assume the issue is resolved by @luiguild’s solution above. Thanks everyone. 👍

#ANS

@mnb3000 but in this case anyone that send one message with the same content, the bot will execute the function 😨

It’s not possible using something like callback_data: "Opt1", that is used in inline keyboard?