rksh
rksh

Reputation: 4050

Facebook messenger API getting user location

Hi I'm trying out the Facebook messenger API and in their documentation they only tell you how to receive text inputs, and attachments. But does not tell about user's location. I have seen messenger bots using location sent by the messenger.

Is there any documentation or information about the API that you guys who might have used the messenger API know about?

The facebook documentation about messenger API inputs - https://developers.facebook.com/docs/messenger-platform/webhook-reference

Upvotes: 1

Views: 1844

Answers (3)

F Blanchet
F Blanchet

Reputation: 1510

It's now possible to ask users to send you their location by using "Quick replies" buttons.

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{
    "id":"<PSID>"
  },
  "message":{
    "text": "Here is a quick reply!",
    "quick_replies":[
      {
        "content_type":"text",
        "title":"Search",
        "payload":"<POSTBACK_PAYLOAD>",
        "image_url":"http://example.com/img/red.png"
      },
      {
        "content_type":"location"
      }
    ]
  }
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"

If the user click and send, you will receive a JSON containing his latitude and longitude. You can find more information on Messenger documentation

Upvotes: 0

Touhidul Alam
Touhidul Alam

Reputation: 331

You can ask user to give you the locations, user can share from messenger location button, and select location on map or their current location.

when you've got the location data from user, this is the kind of location data will be available to you

array(2) {
  ["object"]=>
  string(4) "page"
  ["entry"]=>
  array(1) {
    [0]=>
    array(3) {
      ["id"]=>
      int(1151635421536343)
      ["time"]=>
      int(1463265031987)
      ["messaging"]=>
      array(1) {
        [0]=>
        array(4) {
          ["sender"]=>
          array(1) {
            ["id"]=>
            int(1159277174102575)
          }
          ["recipient"]=>
          array(1) {
            ["id"]=>
            int(1151635421536343)
          }
          ["timestamp"]=>
          int(1463265031923)
          ["message"]=>
          array(3) {
            ["mid"]=>
            string(36) "mid.1463265031752:07d973ce3f3f276421"
            ["seq"]=>
            int(3531)
            ["attachments"]=>
            array(1) {
              [0]=>
              array(4) {
                ["title"]=>
                string(24) "What Sweet Hell is This?"
                ["url"]=>
                string(322) "https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3DSouth%2BBurlington%252C%2BVermont%26FORM%3DFBKPL1%26mkt%3Den-US&h=0AQHaPVgX&s=1&enc=AZPoRntntMTJ6k2Iq9g9uBLH3IFFNySg98jLJEy81cg7zr_ttunOt4DSO9CNdowHocRGLViscMsczdSyYJqtedi8_PT2aHMZgP9FLfAIWDtkuQ"
                ["type"]=>
                string(8) "location"
                ["payload"]=>
                array(1) {
                  ["coordinates"]=>
                  array(2) {
                    ["lat"]=>
                    float(44.4493674)
                    ["long"]=>
                    float(-73.1594363)
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

An example tutorials i found with PHP code Here

Upvotes: 1

JASON G PETERSON
JASON G PETERSON

Reputation: 2223

I don't think the API exposes a more precise location than country. You can get that by sending a GET request in your app using the url:

url = "https://graph.facebook.com/v2.6/" +
sender_id +
'?fields=first_name,last_name,profile_pic,locale,timezone,gender' +
'&access_token=' + app_token

The repsonse will include a JSON-parseable locale field.

Upvotes: -1

Related Questions