node.js - 如何在 Telegraf 中处理 contactRequest

标签 node.js telegram-bot telegraf telegraf.js

所以我在 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/

相关文章:

telegram - 通过 Telegram 聊天 ID 可以做什么?

Python Telegram Bot 如何等待用户回答问题并返回

influxdb - 如何在 Telegraf 中转换 "Tags Values"

monitoring - 如何使用 telegraf 监控远程系统指标?

node.js - 不和谐机器人 : Call a user by name?

node.js - 使用 fast-csv 同步处理 csv 文件

node.js - 为什么 AWS Lambda 使用较旧的 Node 版本?

javascript - 遍历元素数组获取数据

php - 使用 php 发送 mp3 文件到 telegram

kubernetes-helm - Telegraf Helm Chart 更新时出错 : Error parsing data