Telegram Bot - How bind an InlineKeyboardButton with a CallbackQuery

3.3k Views Asked by At

I'm using node-telegram-bot-api. I would have multiple InlineKeyboardButton and bind them with different CallbackQuery throw answerCallbackQuery method. Can you show me an example please? Thank you.

1

There are 1 best solutions below

0
On

I have used the following workaround:

...
var eventEmitter = new events.EventEmitter();


eventEmitter.on('my_fancy_event_1', function(){
  ...
})

eventEmitter.on('my_fancy_event_2', function(){
  ...
})

eventEmitter.on('my_fancy_event_3', function(){
  ...
})


var options = {
  polling: true
};

...

var bot = new TelegramBot(token, options);

bot.onText(config.commands.commandStart, function onMessage(msg) {
  var options = {
    reply_markup: {
        inline_keyboard: [
            [{text: config.inlineText.addPurchase, callback_data: 'my_fancy_event_1'}],
            [{text: config.inlineText.addRevenue, callback_data: 'my_fancy_event_2'}],
            [{text: config.inlineText.getReport, callback_data: 'my_fancy_event_3'}]
        ]
    }
};
bot.sendMessage(msg.from.id, "Choose an operation.",options);
});

bot.on('callback_query', function onCallbackQuery(callbackQuery) {
   eventEmitter.emit(callbackQuery.data);
   bot.answerCallbackQuery(callbackQuery.id, "Hi", false);
});