Reputation: 619
I'm developing a simple telegram bot with node.js telegram-bot. https://github.com/yagop/node-telegram-bot-api For 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
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).
Upvotes: 17
Views: 80045
Reputation: 41
I've used telegraf
library while working on telegram-bot
, most of the libraries are same so you can consider it for reference.
You will get callback_data
as an input when user click on the button
await bot.telegram.sendMessage(ctx.chat.id, "Please click on button below.", {
reply_markup: {
inline_keyboard: [
[
{
text: "Yes",
callback_data: "btn_yes"
},
{
text: "No",
callback_data: "btn_no"
},
]
]
}
});
Upvotes: 1
Reputation: 1302
You are using inline keyboard, instead you can try reply keyboard markup which appears in the bottom of telegram screen as you said.
It is implemented easily and you can find some useful information about it here and here.
Upvotes: 4
Reputation: 8091
You need to listen for the callback_query
.. As outlined :
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
Some further reading indicates : https://core.telegram.org/bots/api#callbackquery
TelegramBot emits callback_query when receives a Callback Query
This relates to this documentation here : 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_id
will be present.
Exactly one of the fields data
or game_short_name
will be present.
Upvotes: 11