15

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

1

3 Answers 3

10

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.

2
  • worked perfectly for me. thank you. can you please also explain how to make custom buttons(which are in the bottom of the telegram app screen), if you know?
    – Kamiky
    Mar 14, 2017 at 14:57
  • @Kamiky I'm actually not too sure if you can access the native layer, but maybe one_time_keyboard would be something you could use ? core.telegram.org/bots/api#replykeyboardmarkup It's refered to as a custom keyboard.. Here also : core.telegram.org/bots#keyboards
    – Pogrindis
    Mar 14, 2017 at 15:09
4

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.

0

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"
                },
               
            ]
        ]
    }
});

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.