Telegraf: How to handle button of a dynamic keyboard

Created on 6 Jan 2018  路  3Comments  路  Source: telegraf/telegraf

I have an inline keyboard which it's buttons come from a JSON obj like RSP. It is something like this:

[{"caption", "A"},
{"caption", "B"},
{"caption", "C"}]
the number of objects in RSP and the values of them("A", "B",...) are not constant and I get them by calling an API.
Now I don't know how to handle the response of each key.
Should I use Scenes or something else?

Thanks for any help.

question

All 3 comments

Hi @kmahsi

We have few solutions here:

Dispatch callback query data manually:

bot.on('callback_query', (ctx) => {
  console.log(ctx.callbackQuery.data)
})

Or using predicate function:

function predicateFn (callbackData) {
   return callbackData === 'A' || callbackData === 'B' 
}

bot.action(predicateFn, ({ reply }) => reply('foo'))

Hi @dotcypress
Thanks for your answer.
What is the application of this "action" method?
Is it just about inline keyboard and callbackQuerys?
Another thing is that I have many objects like RSP and each of them have its own inline keyboard and they have different logics from each other, thus your first solution doesn't work in this situation ( It handles every call back query in the same way.)
But I don't know if your second solution works or not.

@kmahsi Yes, the second solution will work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bostrot picture bostrot  路  3Comments

jwandekoken picture jwandekoken  路  3Comments

MohGanji picture MohGanji  路  3Comments

supermomme picture supermomme  路  3Comments

bostrot picture bostrot  路  3Comments