所以我在 Node 中使用 Telegraf.js 制作一个电报机器人,该机器人需要用户的电话号码,在 Telegraf 中请求电话号码的一种方式如下
ctx.reply(
"Phone number is required",
{
...Markup.keyboard([
Markup.button.contactRequest("Send Contact"),
]).resize(),
}
);
这工作得很好,但我不知道如何处理联系请求的结果, 我尝试过使用 bot.hears,将其视为回调按钮 (Markup.button.callback("Send Contact"))
bot.hears("Send Contact",(ctx)=>{
console.log("contact received")
})
但这不起作用,所以我想知道是否有特殊的方法来处理 Markup.button.contactRequest
的响应最佳答案
我找到了解决方案。希望对你也有帮助
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);
});
关于node.js - 如何在 Telegraf 中处理 contactRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73107698/