Mr.D Mr.D - 7 months ago 26
Javascript Question

Facebook Messsenger, how to handle sent location

According to this guide, it describes us to how to send messages and handle buttons texts etc.

However, when users sends to bot its

geolocation
, my webhook receives it as this:

{ sender: { id: 99999999999999},
recipient: { id: 99999999999999 },
timestamp: 99999999999,
message:
{ mid: 'mid.9999999:999999999',
seq: 231,
attachments: [ [Object] ] } }


Location is sent to webhook as some kind of object inside
attachments
array.

When in my code I try to get them like this:

event.message.attachments[0].longitude
event.message.attachments[0].latitude


I get
undefined
.

How to get longitude and latitude of Location that user has sent? There is no description of this in documentation.

UPDATE



After some trying I was able to get all its properties. When users sents their location I get it in this structure:

{url: https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3D43.230507%252C%2B76.945031%26FORM%3DFBKPL1%26mkt%3Den-US&h=vAQHe08Kz&s=1&enc=AZPRe3VQFnThq3_jdPFg8R4WWUZxWJA1XjbVvVua4fD0PNpt2u26hvS-noUEU55qQNC6Wm9hNGwpGntrUgu1nx7KsSIKRbGQyKEXueAImV3Kyg}


It is GIANT URL. If you open this link via your browser, it will open Bing Maps and shows this location.

It is hard to work with this data. It forces users to write their own parsers.

Answer

Latitude and Longitude are there in the URL:

...where1%3D43.230507%252C%2B76.945031%26FORM...

So one way would be to convert the URL to text and just parse this text to get Lat and Long.

UPDATE Now lat and lng can be directly accessed from the attachments JSON:

lat = event.message.attachments[0].payload.coordinates.lat
lng = event.message.attachments[0].payload.coordinates.long