1
votes

I am using Node JS + twilio API to get whatsapp messages from a bot. Right now I am trying to get the GPS location sent by the user, but it does not appear in the body of the message, while in the SMS version, it does.

//Import model
const WhatsappTwilio = require('../models/WhatsappModel');

exports.postWhatsapp = (req, res, next) => {
  const Body = req.body.Body;
  const From = req.body.From;
  const To = req.body.To;

  const whatsappTwilio = new WhatsappTwilio({
    Body: Body,
    From: From,
    To: To
  });
  let responseMsg = '';
  if (Body.includes('http://maps.google.com/maps')) {
    responseMsg ='Location received';
  } else {
    responseMsg ='Error, could not get location';
  }

  //Response through twilio whatsapp
  client.messages
    .create({
        body: responseMsg ,
        from: 'whatsapp:' + To,
        to: 'whatsapp:' + From
    })
    .then(message => console.log(message.sid));
};

This is the controller that handles the whatsapp bot, in the SMS version it works, but in the whatsapp version it does not.

1

1 Answers

0
votes

If a "Location" is attached to a Whatsapp message, it will be present in the POST request parameters and you can get its properties similarly with how you get the body of the message.

if (req.body.Latitude && req.body.Longitude) {
  console.log('Whatsapp location received...');
  console.log(req.body.Latitude + ', ' + req.body.Longitude);

}

An address might also be present and you can get it with req.body.Address.