boom pow
boom pow

Reputation: 321

How to disable Telegraf.js inline keyboard after one click

I just started using Telegraf to make a Telegram bot with node, and the bot uses an inline keyboard for users to select from a given option. But after the user selects one option and gets a response, the user can still click on the other options and get their response.

ctx.reply("Do you want to order the product?", {
        parse_mode: "HTML",
        ...Markup.inlineKeyboard([
          Markup.button.callback("Order", "order"),
          Markup.button.callback("Cancel", "cancel"),
        ]),
      });

I tried to disable it using bot.hears method

bot.hears("order", (ctx) => {
  "removed",
    {
      reply_markup: {
        remove_keyboard: true,
      },
    };
});

bot.hears("cancel", (ctx) => {
  "removed",
    {
      reply_markup: {
        remove_keyboard: true,
      },
    };
});

but the methods don't seem to disable/ remove the options. So is there a way to disable the inline keyboards after one click

Upvotes: 3

Views: 4532

Answers (1)

Usitha Indeewara
Usitha Indeewara

Reputation: 1005

Instead of bot.hears, you have to use bot.action. Try this code:

bot.action("order", (ctx) => {
    ctx.editMessageReplyMarkup();
    ctx.editMessageText("removed");
});

bot.action("cancel", (ctx) => {
    ctx.editMessageReplyMarkup();
    ctx.editMessageText("removed");
});

bot.editMessageReplyMarkup function with no arguments will remove the inline keyboard. bot.editMessageText function will edit the message with another text.

I hope this helps :)

Upvotes: 1

Related Questions