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

て烟熏妆下的殇ゞ 提交于 2019-11-28 18:21:34

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

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
           }

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!