Javascript 向电报机器人添加可点击的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42786711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add clickable buttons to telegram-bot
提问by Kamiky
I'm developing a simple telegram bot with node.js telegram-bot. https://github.com/yagop/node-telegram-bot-apiFor now I want user to stop typing messages (with letters), just pressing one of a few buttons. And when he clicks on the button, his telegram's client has to send back to my bot another message (something like "clicked yes" or "clicked no"). I've found that it can be made with
我正在使用 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);
So user receives 3 kind of messages, and when he clicks them, he doesn't send me back anything. I need another type of buttons (which will be in the bottom of client's app).
所以用户收到 3 种消息,当他点击它们时,他不会给我发回任何东西。我需要另一种类型的按钮(位于客户端应用程序的底部)。
采纳答案by Pogrindis
You need to listen for the callback_query.. As outlined :
您需要聆听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);
});
More info can be found in the library itself. : https://github.com/yagop/node-telegram-bot-api/blob/0174b875ff69f6750fc80a049315c9a0d7a5e471/examples/polling.js#L78
更多信息可以在图书馆本身中找到。: https://github.com/yagop/node-telegram-bot-api/blob/0174b875ff69f6750fc80a049315c9a0d7a5e471/examples/polling.js#L78
Some further reading indicates : https://core.telegram.org/bots/api#callbackquery
一些进一步的阅读表明:https: //core.telegram.org/bots/api#callbackquery
TelegramBot emits callback_query when receives a Callback Query
TelegramBot 在收到回调查询时发出 callback_query
This relates to this documentation here : https://core.telegram.org/bots/api#callbackquery
这与此处的文档有关:https: //core.telegram.org/bots/api#callbackquery
If the button was attached to a message sent via the bot (in inline mode), the field inline_message_idwill be present.
如果按钮附加到通过机器人发送的消息(内联模式),该字段inline_message_id将出现。
Exactly one of the fields dataor game_short_namewill be present.
字段data或game_short_name将出现的字段之一。

