首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在nodejs中注册whatsapp webhooks

如何在nodejs中注册whatsapp webhooks
EN

Stack Overflow用户
提问于 2022-06-30 09:13:09
回答 2查看 440关注 0票数 2

我正在尝试将whatsapp帐户与bot框架bot集成,但在集成过程中遇到了一个问题

我的代码是:

代码语言:javascript
复制
const restify = require('restify');

// Create HTTP server
let server = restify.createServer();

server.listen(process.env.port || process.env.PORT || 3978, function() {
    console.log(`\n${ server.name } listening to ${ server.url }`);
});
const token = "verify-token"
// verfiy the web hok
server.get('/webhooks',  (req, res) => {
    console.log(req);
    if (
        req.query['hub.mode'] == 'subscribe' &&
        req.query['hub.verify_token'] == token
    ) {
        res.send(req.query['hub.challenge']);
    } else {
        res.sendStatus(400);
    }
});

Ok问题是我无法验证WhatsAppweb钩子见图

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-04 18:02:20

无法从本地主机连接到api。Whatsapp需要一个带有证书的https连接。在heroku/glitch等上托管您的服务器。

票数 0
EN

Stack Overflow用户

发布于 2022-07-09 18:09:25

您错过了web钩子的POST请求,您可以在收到请求后获得通知的正文,

代码语言:javascript
复制
// verfiy the web hok
server.get('/webhooks',  (req, res) => {
    console.log(req);
    if (
        req.query['hub.mode'] == 'subscribe' &&
        req.query['hub.verify_token'] == token
    ) {
        res.send(req.query['hub.challenge']);
    } else {
        res.sendStatus(400);
    }
});

// Accepts POST requests at /webhook endpoint
app.post("/webhook", (req, res) => {
  // Parse the request body from the POST
  let body = req.body;

  // info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
  if (req.body.object) {
    if (
      req.body.entry &&
      req.body.entry[0].changes &&
      req.body.entry[0].changes[0] &&
      req.body.entry[0].changes[0].value.messages &&
      req.body.entry[0].changes[0].value.messages[0]
    ) {

      // do your stuff here.....

      let phone_number_id =
        req.body.entry[0].changes[0].value.metadata.phone_number_id;
      let from = req.body.entry[0].changes[0].value.messages[0].from; // extract the phone number from the webhook payload
      let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; // extract the message text from the webhook payload
    }
    res.sendStatus(200);
  } else {
    // Return a '404 Not Found' if event is not from a WhatsApp API
    res.sendStatus(404);
  }
});

请按照WhatsApp 示例应用程序端点了解更详细的说明。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72813196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档