Facebook Webhook,如何从中间ID获取对话ID

时间:2019-08-30 07:56:28

标签: facebook facebook-graph-api facebook-javascript-sdk chatbot facebook-webhooks

大家好,我正在构建一个聊天机器人,我遇到的问题是,当用户向我的应用发送消息时,我没有像“ t_31231231231231”这样的对话ID,相反,我得到了“ “,我不知道如何从“中”开始对话,我试图找到该文档,但也许我还没有找到它。 :(

{
  "object": "page",
  "entry": [
    {
      "id": "553014938133297",
      "time": 1567149324484,
      "messaging": [
        {
          "sender": {
            "id": "2112675102192095"
          },
          "recipient": {
            "id": "553014938133297"
          },
          "timestamp": 1567149323879,
          "message": {
            "mid": "n89QDNpjbh7UUZjDj7mkfk-Mqd_vry00MlXChtxjo-ZLokFwJAtZ6udnPZibQjzAZpuqsN64UVjTly5cTCEKTQ",
            "text": "dasddsad",
            "nlp": {
              "entities": {},
              "detected_locales": [
                {
                  "locale": "vi_VN",
                  "confidence": 0.8299
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

这可能不是理想的解决方案,但可以即兴发挥作用。由于您拥有message_id,因此可以获取message以确定参与者是谁,然后可以将其与participants中的conversations.participants进行匹配。
我要做的是使用其conversations字段来获取所有participants。 即

curl -i -X GET \
 "https://graph.facebook.com/v8.0/me/conversations?fields=participants&access_token=your-access-token-goes-here"

返回哪个

{"data"=>
  [{"participants"=>
     {"data"=>
       [{"name"=>"Masroor Hussain",
         "email"=>"3275685679217538@facebook.com",
         "id"=>"4275685679217538"},
        {"name"=>"Testpage",
         "email"=>"115083020351552@facebook.com",
         "id"=>"115083020351552"}]},
    "id"=>"t_781422919067688"}],
 "paging"=>
  {"cursors"=>
    {"before"=>
      "QVFIUlRKd1RBYmtvVXlicm95QUNZAbjVKUW5LbU5lYzhQM24ycmY1aTBXM1NTbnRSLURhc2xnSlBnOWE3OTJ4ZAy1KbS1LLVhUUEdqYmM0MmVzZAXZAZAX2xRdzJCbXpJSEowMmxzUHc0NlBXQ0FTVEdRSEZAZASmI2SGxsNlNOdC1XOWNSZADl0",
     "after"=>
      "QVFIUkdEUjNPek5jbE9RNUhzZAXpJTm9hWERvQVdGYVV5cHlfcDl6cEJyZAG5NaGpjR1NkUHI4R0JDd1VkWEU0RUh2ZADFOQUVzN0RwQ2tyYmpXcThEV0hjUDM2QXAxbFRLWDVzUGoyNmFjbkcyUzl3X0Myc1AtanRYUndjMDBSdVZAJZAnI0"
    }
  }
}

participants.data中,一个参与者是页面,另一个参与者是page_scoped_user。您可以解析响应以匹配您的参与者并获得对话id,例如,此处的对话ID为"t_781422919067688"