2

so am making a telegram bot using Telegraf.js in Node and the bot requires the user's phone number and in Telegraf one way of requesting phone number is like this

ctx.reply(
    "Phone number is required",
    {
      ...Markup.keyboard([
        Markup.button.contactRequest("Send Contact"),
      ]).resize(),
    }
  );

and this works perfectly fine, but I don't know how to handle the result of the contact request, I've tried using bot.hears by treating it like a callback button (Markup.button.callback("Send Contact"))

bot.hears("Send Contact",(ctx)=>{
  console.log("contact received")
})

but this doesn't work, so I was wondering if there is a special way to handle the response of Markup.button.contactRequest

boom pow
  • 181
  • 9

1 Answers1

2

I found a solution. Hope it helps you too

await ctx.telegram.sendMessage(ctx.chat.id, "Some text", {
    parse_mode: "Markdown",
    reply_markup: {
      one_time_keyboard: true,
      keyboard: [
        [
          {
            text: "Share Phone Number",
            request_contact: true,
          },
          {
            text: "Cancel",
          },
        ],
      ],
      force_reply: true,
    },
  });
bot.on("contact", (ctx: any) => {
  const contact = ctx.message.contact.phone_number;
  console.log("Hello Contact",contact);
});