简体   繁体   中英

Retrieving the sender's whatsapp number and/or the message with twilio's whatsapp api?

I have setup a webhook for twilio's whatsapp api with a twilio sandbox. Currently, all it does is send a message to a hardcoded whatsapp number (mine). Everything works as I am able to send a message when I receive one, however, I am incapable of retrieving the sender's number or the message he sent. I have tried looking into the req object (see below), but I have not found what I was looking for. For example req.body = {} , there is no "sender" or "To" or "From".

This is what I have as of now.

'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 

Is there something I'm missing, is it even possible, should I expect another parameter?

// Body Parser Middleware

app.use(express.json());

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM