首页 > 解决方案 > 使用 twilio 的 whatsapp api 检索发件人的 whatsapp 号码和/或消息?

问题描述

我已经为 twilio 的 whatsapp api 设置了一个带有 twilio 沙箱的 webhook。目前,它所做的只是向硬编码的 whatsapp 号码(我的)发送消息。一切正常,因为我可以在收到消息时发送消息,但是,我无法检索发件人的号码或他发送的消息。我曾尝试查看该req对象(见下文),但我还没有找到我要找的东西。例如req.body = {},没有“发件人”或“收件人”或“发件人”。

这就是我现在所拥有的。

'use strict';

const express = require('express')
const router = express.Router()


const whatsAppConfig = {
  accountSid: process.env.WHATSAPPACCOUNTSID || 'AC73XXXXXXXXXXXXXXXXXXXXXXX',
  authToken: process.env.WHATSAPPAUTHTOKEN || 'd3XXXXXXXXXXXXXXXXXXXXXXXXX',
  from: process.env.WHATSAPPFROM || 'whatsapp:+1**********',
};  


const client = require('twilio')(whatsAppConfig.accountSid, whatsAppConfig.authToken);

router.post('/whatsapp', (req, res) => {  

  console.log(req); //this is the object that contains the request, so all the information I need should be in here

  
  // Return a '200 OK' response to all events
  res.status(200).send('EVENT_RECEIVED');

  let message = "THIS IS A TEST MESSAGE";
  sendWhatsAppMessage(whatsAppConfig.from, message, "whatsapp:+1******"); //This works
});

//

function sendWhatsAppMessage(from_number, message, to_number) {
  client.messages
      .create({
         from: from_number,
         body: message,
         to: to_number
       })
      .then(message => console.log(message.sid));
}

module.exports = router 

有什么我遗漏的东西,甚至可能吗,我应该期待另一个参数吗?

标签: node.jstwiliowhatsapp

解决方案


// Body Parser Middleware

app.use(express.json());

app.use(express.urlencoded({ extended: false }));

推荐阅读