Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add clickable buttons to telegram-bot

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

like image 975
Kamiky Avatar asked Mar 14 '17 12:03

Kamiky


People also ask

What is inline button in telegram?

Switch to Inline buttons Pressing a switch to inline button prompts the user to select a chat, opens it and inserts the bot's username into the input field. You can also pass a query that will be inserted along with the username – this way your users will immediately get some inline results they can share.

What are telegram buttons?

All the telegram buttons which the entities are showing you a content Is called basic buttons. Basic buttons are buttons wich you by using them can show texts or any types of files or even you can send the map to them. The same as the story for button that does this for your users.


2 Answers

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.

like image 124
Pogrindis Avatar answered Oct 09 '22 17:10

Pogrindis


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.

like image 37
Naser.Sadeghi Avatar answered Oct 09 '22 16:10

Naser.Sadeghi