20

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.

Lee Woodman
  • 1,319
  • 2
  • 16
  • 30

3 Answers3

21

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
Anupam Mohanty
  • 460
  • 1
  • 6
  • 11
  • Has Facebook only recently started allowing this information in the response because I've examined the location message before and nothing was there other than mid etc. Do I need any specific permissions to be able to get this? – Lee Woodman May 18 '16 at 06:59
  • 1
    I've been using it since past 25 days. Try logging event.message.attachments[0].payload – Anupam Mohanty May 18 '16 at 07:28
  • You're right! I am convinced I had tried this several times but there was nothing in the payload. Oh well. Thank you for the help. – Lee Woodman May 18 '16 at 16:57
  • 1
    i think we can receive location to our server but can't send location back to the messenger boat as facebook docs don't specify any location type attachment https://developers.facebook.com/docs/messenger-platform/send-api-reference – Rishabh Agrawal Jul 14 '16 at 19:30
  • 1
    @ANinJa yes, as of now i don't think there's a way to send location to bot. What i am doing is show a button to user with a google maps url. When user clicks on it, it opens in Maps – Anupam Mohanty Jul 15 '16 at 07:36
  • Location is not beeing sent anymore as Anupam described. – 4F2E4A2E Sep 10 '16 at 19:00
  • Location is not beeing sent anymore as Anupam described by the Facebook API, BUT if the user sends his location via Messenger, then you have the coordinates attached to the message. – 4F2E4A2E Sep 12 '16 at 11:22
  • 1
    @4F2E4A2E can you give an example of how you are receiving location. I checked today for location sent from messenger..it was the same for me – Anupam Mohanty Sep 12 '16 at 12:06
1

Yes, the location will be sent as an attachment in the message. If you are referring to the example code given in the facebook messenger platform documentation the fix can be done as follows....(please refer the complete code here https://developers.facebook.com/docs/messenger-platform/quickstart)

in the else section

else if (messageAttachments) {

              console.log(messageAttachments[0].payload.coordinates.lat); //gives you lat

              console.log(messageAttachments[0].payload.coordinates.long); // gives you long
           }
0

There is a bug for now about getting some of users' location.

When my users share their location in facebook-messenger-bot;

While I can get the location of users like below response;

    { mid: 'mid.1463464074086:96b149e1a047e47842',
      seq: 2076,
      attachments: 
        [ { title: 'Fatih\'s Location',
       url: 'https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%..',
       type: 'location',
       payload: [Object] 
    } ] }

But can't get the location of some other users like below response;

   { 
    { mid: 'mid.$cAAD53Ka90kBmfY23q1gTEdy6rrmW', seq: 19104}
   }

Facebook Team still working on this bug, this is the link if you want to browse: https://developers.facebook.com/bugs/160926314660178/

Fatih Turgut
  • 319
  • 3
  • 9