使用twilio的whatsapp api检索发件人的whatsapp号和/或消息?

时间:2020-07-29 20:58:47

标签: node.js twilio whatsapp

我已经使用twilio沙箱为twilio的whatsapp api设置了一个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 

我是否缺少某些东西,甚至有可能,我是否应该期待另一个参数?

1 个答案:

答案 0 :(得分:0)

// Body Parser Middleware

app.use(express.json());

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