I am building a Node.js application that receives messages from WhatsApp via Twilio. Sending text messages is not a problem, but when I try to send the current location from the users WhatsApp account I cannot find the coordinates in the request object sent to the server.

This is what I am doing to see what is being sent to the app from Twilio.

app.post('/incoming', (req, res) => { var incomingMessage = req.body.Body; console.log(incomingMessage); }); 

When I share my current location with the app, the request body is empty.

Can anyone shed any light on this or explain how WhatsApp shares a users location within the app?

Thanks.

Tag:node.js, javascript, google-maps, twilio

12 comments.

  1. philnash

    Twilio developer evangelist here.

    Update: since 12th Nov 2019 Twilio does support location in WhatsApp messages

    When a user sends a location message to your Twilio enabled WhatsApp number, Twilio will send on the details as parameters in the webhook request to your application. The parameters are: Latitude, Longitude and optionally Label and Address if the user sent a specific place.

    In your Node application you are able to read these from the request's body like this:

    app.post('/incoming', (req, res) => { const longitude = req.body.Longitude; const latitude = req.body.Latitude; console.log(`The user sent this from ${longitude}, ${latitude}`); // do something with the location data })

    Check out these blog posts on how to use location data from WhatsApp to search nearby restaurants or how to build a location aware weather bot with the Twilio API for WhatsApp.

    Original answer from September 23rd 2019:

    The Twilio API for WhatsApp doesn't currently support sending or receiving location coordinates.

    1. josue.0

      I think they do support sending/receiving locations now (source: support.twilio.com/hc/en-us/articles/…), but I don't know if it possible to receive the user's current location

    2. philnash

      Ah, good point, you absolutely can. I'll update my answer

  2. Rucc

    Here it says is it possible to recieve location data on the Body: https://www.twilio.com/docs/whatsapp/api#location-messages-with-whatsapp

    You can also receive inbound location messages with the Twilio API for WhatsApp. Locations will not show up in the Twilio Console at this time. However, your web application will receive the location data in the POST request that Twilio sends. Below is a sample payload containing location information. Please note that the Body={name} parameter is not required for inbound messages.

    Latitude=37.7879277&Longitude=-122.3937508&Address=375+Beale+St%2C+San+Francisco%2C+CA+94105&SmsMessageSid=SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&NumMedia=0&SmsSid=SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&Label=Twilio+Inc&Body=&To=whatsapp%3A%2B14155238886&NumSegments=1&MessageSid=SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&AccountSid=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&From=whatsapp%3A%2B12345678900&ApiVersion=2010-04-01
    1. Rucc

      I tried and I succesfully recieved Latitude and Longitude data.

  3. specter epiphane

    At the moment it is possible to send to the user some location but you can't get user location directly through twilio whatsapp api. https://www.twilio.com/blog/send-location-details-whatsapp-node-js.

  4. Abs

    I solved the issue by installing Google Earth on the same phone. Then I clicked the WhatsApp location share, and opened it with Google Earth.

    Google Earth showed the coordinates in geographic degrees, minutes and seconds.

    1. Community

      Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

  5. joe

    Update your code with:

    var incomingMessage = req.body;

    Twilio supporting both sending and receiving location. The below one is the response of the location when a user sends from his WhatsApp to the Twilio WhatsApp.

    { "Latitude": "24.7969323", "Longitude": "46.6301663", "SmsMessageSid": "xxxxxxxxxxxxxxxxxxxxxx", "NumMedia": "0", "SmsSid": "xxxxxxxxxxxxxxxxxxxxxx", "SmsStatus": "received", "Body": "", "To": "whatsapp:+1415xxxxxxx", "NumSegments": "1", "MessageSid": "xxxxxxxxxxxxxxxxxxxxxx", "AccountSid": "xxxxxxxxxxxxxxxxxxxxxx", "From": "whatsapp:+966xxxxxxxxxxxxxxxxxxxxxx", "ApiVersion": "2010-04-01"

    }

    Here you can see the Latitude and Longitude of the user.

  6. nnamdi igwe

    Go to Your Phone Menu.

    Find WhatsApp.

    Tap on the WhatsApp Icon.

    Make Sure That, You Are on the Chats Tab.

    Tap on the “Group” or “Individual Chat” Where You Want to Share Your Live Location.

    Tap on the “Attach Icon”.

    Tap on the “Location”.

    Tap on the “Share Live Location

    1. swifft

      Thanks for the attempt. I am looking for the coordinates that are sent from the user to the app.

  7. Mochamad Aris Zamroni

    Simply prompt user to open a company webpage that contains Javascript that capture user's location You can read more detail in here: https://ma-zamroni.medium.com/workaround-for-whatsapp-business-api-to-get-customers-current-location-392c63b4b365

Add a new comment.