Telegraf.js How to pass parameters to a function from inline keyboard

4k views Asked by At

I have an array of messages containing some text and an inline keyboard with "delete" button.

bot.command('items', ctx => {
    items.forEach(async data => {
        await ctx.reply(data.Title,
            Markup.inlineKeyboard([Markup.button.callback("delete item", "DeleteItem")]));
    })
})

And I have a function bot.action for the delete buttons on my inline keyboard. How do I pass a parameter to the bot.action so I can delete the item via the delete button.

bot.action("DeleteItem", async ctx => {
    ctx.reply("deleting item");
})
1

There are 1 answers

1
Roma On BEST ANSWER

For delete message, you can use:

bot.action('delete', ctx => ctx.deleteMessage())

As you can see from examples/echo-bot-module.js.

If you need pass a parameter to the bot.action, you may try:

bot.action(/^data-(\d+)$/, (ctx) => {
  return ctx.answerCbQuery(`Param: ${ctx.match[1]}! `)
})

// And send with callback data
Markup.button.callback('Button', `data-${Math.round(Math.random()*1000)}`)