Facebook messenger API获取用户位置

时间:2016-06-04 11:52:21

标签: javascript facebook facebook-graph-api

您好我正在尝试使用Facebook messenger API,在他们的文档中,他们只会告诉您如何接收文本输入和附件。但是没有告诉用户的位置。我已经看过信使机器人使用信使发送的位置。

有没有关于API的文档或信息,您可能使用过messenger API的人知道了什么?

关于信使API输入的facebook文档 - https://developers.facebook.com/docs/messenger-platform/webhook-reference

3 个答案:

答案 0 :(得分:1)

您可以要求用户为您提供位置,用户可以从信使位置按钮分享,并选择地图上的位置或其当前位置。

当您从用户那里获得位置数据时,这是您可以使用的位置数据

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)
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

我用PHP代码Here

找到的示例教程

答案 1 :(得分:0)

现在可以通过“快速回复”按钮要求用户向您发送其位置信息。

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>"

如果用户单击并发送,您将收到一个包含其纬度和经度的JSON。 您可以在Messenger documentation

上找到更多信息。

答案 2 :(得分:-1)

我认为API不会提供比国家更精确的位置。您可以使用网址

在您的应用中发送GET请求
url = "https://graph.facebook.com/v2.6/" +
sender_id +
'?fields=first_name,last_name,profile_pic,locale,timezone,gender' +
'&access_token=' + app_token

repsonse将包含一个JSON可解析的locale字段。