【问题标题】:Telegram Bot - How bind an InlineKeyboardButton with a CallbackQueryTelegram Bot - 如何将 InlineKeyboardButton 与 CallbackQuery 绑定
【发布时间】:2017-05-05 14:37:59
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js telegram telegram-bot


    【解决方案1】:

    我使用了以下解决方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      相关资源
      最近更新 更多