Pavel Kazakov
Pavel Kazakov

Reputation: 13

(Telegram BOT) Is there a way to listen for an answer in a specific menu block

I've set up a simple telegram bot application but I'm struggling with message interception for a specific menu button.

bot.start((ctx) => ctx.reply('start handler', {
  reply_markup: JSON.stringify({
    reply_to_message_id: ctx.message.message_id,
    resize_keyboard: true,
    keyboard: [
      [{text: 'ZIP'}, {text: 'birthday'}],
    ]
  })
}));

bot.hears('ZIP', (ctx) => ctx.reply('input your ZIP code', {
  reply_markup: JSON.stringify({
    resize_keyboard: true,
    keyboard: [
      ['Отмена']
    ]
  })
}));

bot.hears('birthday', (ctx) => ctx.reply('input day only', {
    reply_markup: JSON.stringify({
      resize_keyboard: true,
      keyboard: [
        ['Отмена']
      ]
    })
  })
);

What kind of approach should I use to listen to answer in a 'ZIP' menu for example. I don't want that general listeners will listen for this answer. How could I understand that the answer comes from the 'ZIP' button??

Upvotes: 0

Views: 1110

Answers (1)

Tibebes. M
Tibebes. M

Reputation: 7548

You may choose to use inline keyboards instead (if you want to 100% accurately listen to updates only from the menu)

That being said there are some workarounds you might consider if you want to keep using reply keyboards and avoid the confusion..

  1. the first one is to add emoji to the keyboard buttons (to make them unique and decrease the chance of accidental reaction to text messages from users)
  2. use the Telegraf stages & scenes abstraction to register handlers at specific context.. (that way your bot only reacts in that scene not globally)

Upvotes: 1

Related Questions