swifft
swifft

Reputation: 121

How can I retrieve the location coordinates from a WhatsApp location share?

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.

Upvotes: 3

Views: 12648

Answers (7)

Abs
Abs

Reputation: 1

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.

Upvotes: 0

philnash
philnash

Reputation: 73065

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.

Upvotes: 3

Mochamad Aris Zamroni
Mochamad Aris Zamroni

Reputation: 51

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

Upvotes: -1

joe
joe

Reputation: 435

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.

Upvotes: 0

Rucc
Rucc

Reputation: 73

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

Upvotes: 1

specter epiphane
specter epiphane

Reputation: 11

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.

Upvotes: 1

nnamdi igwe
nnamdi igwe

Reputation: 1

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

Upvotes: -1

Related Questions