Telegraf: Action after keyboard selection

Created on 4 Nov 2017  路  3Comments  路  Source: telegraf/telegraf

Hi everyone, hope someone of you can help me with this.

I'm starting with Telegraf and I'm trying to work with keyboards, so my scenario is:

When my bot hears 'status' he respond:

bot.hears(/status/i, ctx => {
    return ctx.reply('Wich one of these you want check?', Extra.markup(
        Markup.keyboard([
            ['One', 'Two', 'Three', 'Four']
        ], { columns: 4 })
        .oneTime()
        .extra()
    ))
})

What I want: When the user select one of these options, my bot will do something.

What I tried to do was:

bot.hears(/status/i, ctx => {
    return ctx.reply('Wich one of these you want check?', Extra.markup(
        Markup.keyboard([
            ['One', 'Two', 'Three', 'Four']
        ], { columns: 4 })
        .oneTime()
        .extra()))
    })

bot.action(/.+/, (ctx) => {
    return ctx.answerCallbackQuery(`Oh, ${ctx.match[0]}! Great choise`)
})

I know this is a dummy question, but I hope someone can help me.

Thanks!

question

All 3 comments

Hi @mncustodio

Telegram keyboard is nothing more than shortcuts for text messages, in other words: bot don't see any difference between keyboard click and manually typed message.

Otherwise, bot.action will handle only callback queries, not text updates.

So there are two choices:

  1. Switch to Markup.inlineKeyboard, and handle buttons with .action handler.
  2. Use bot.hears to listen for text messages from the keyboard.

Thanks @dotcypress!

I'll try it and give you a feedback.

Thanks again @dotcypress!

It's working fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bostrot picture bostrot  路  3Comments

expelliamus picture expelliamus  路  3Comments

expelliamus picture expelliamus  路  3Comments

MohGanji picture MohGanji  路  3Comments

kuhel picture kuhel  路  3Comments