向电报机器人添加可点击的按钮

Kam*_*iky 7 javascript node.js telegram-bot

我正在使用 node.js 电报机器人开发一个简单的电报机器人。 https://github.com/yagop/node-telegram-bot-api 现在我希望用户停止输入消息(带字母),只需按下几个按钮之一。当他点击按钮时,他的电报客户端必须向我的机器人发送另一条消息(例如“点击是”或“点击否”)。我发现它可以用

var options = {
      reply_markup: JSON.stringify({
        inline_keyboard: [
          [{ text: 'Some button text 1', callback_data: '1' }],
          [{ text: 'Some button text 2', callback_data: '2' }],
          [{ text: 'Some button text 3', callback_data: '3' }]
        ]
      })
    };
bot.sendMessage(msg.chat.id, "answer.", option);
Run Code Online (Sandbox Code Playgroud)

所以用户收到 3 种消息,当他点击它们时,他不会给我发回任何东西。我需要另一种类型的按钮(位于客户端应用程序的底部)。

Pog*_*dis 5

您需要聆听callback_query.. 概述:

bot.on('callback_query', function onCallbackQuery(callbackQuery) {
  const action = callbackQuery.data;
  const msg = callbackQuery.message;
  const opts = {
    chat_id: msg.chat.id,
    message_id: msg.message_id,
  };
  let text;

  if (action === '1') {
    text = 'You hit button 1';
  }

  bot.editMessageText(text, opts);
});
Run Code Online (Sandbox Code Playgroud)

更多信息可以在图书馆本身中找到。: https://github.com/yagop/node-telegram-bot-api/blob/0174b875ff69f6750fc80a049315c9a0d7a5e471/examples/polling.js#L78

一些进一步的阅读表明:https : //core.telegram.org/bots/api#callbackquery

TelegramBot 在收到回调查询时发出 callback_query

这与此处的文档有关:https : //core.telegram.org/bots/api#callbackquery

如果按钮附加到通过机器人发送的消息(内联模式),该字段inline_message_id将出现。

字段datagame_short_name将出现的字段之一。