Facebook Messenger Chatbot how do I collect the users geo location that they send?

Lee Woodman picture Lee Woodman · May 17, 2016 · Viewed 17.8k times · Source

In Facebook Messenger there is an icon allowing the user to send their geo coordinates.

Location Sent through Facebook Messenger

Is this available on the Facebook Messenger platform yet i.e. if a user sends me their location does my Chatbot have access to it? If so how is it done because i can't see it in the response in my webhook.

Answer

Anupam Mohanty picture Anupam Mohanty · May 18, 2016

You get the location as attachment in message. See sample below:

{ mid: 'mid.1463464074086:96b149e1a047e47842',
  seq: 2076,
  attachments: 
    [ { title: 'Anupam\'s Location',
   url: 'https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3D19.120002%252C%2B72.863715%26FORM%3DFBKPL1%26mkt%3Den-US&h=AAQH523sr&s=1&enc=AZNmEBjv3zHHm0_dYnEIC6j7EDsJNt8PZRZZyaXbIZ6VzjPsQUOOaMIPGtXFH17CevUiNK0_K594CgDQHAMQSru7uS_jjbkxojBWNwBnncqzaw',
   type: 'location',
   payload: [Object] } ] }

From the payload, you can access the Latitude and Longitude using:

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