Telegram Bot - 如何将InlineKeyboardButton与CallbackQuery绑定

时间:2016-12-20 11:28:58

标签: javascript node.js telegram telegram-bot

我正在使用 node-telegram-bot-api 。我会有多个InlineKeyboardButton并使用不同的CallbackQuery throw answerCallbackQuery方法绑定它们。你能告诉我一个例子吗?谢谢。

1 个答案:

答案 0 :(得分:3)

我使用了以下解决方法:

...
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);
});