colinmorgan
colinmorgan

Reputation: 55

Facebook webhook, how to get conversation id from mid id

Hello everyone i'm building a chat bot, i'm having a problem that is, when a user sends a message to my app, i don't get the conversation id like "t_31231231231231", instead I get "mid", I don't know how to get the conversation from "mid", i tried to find the document but maybe I haven't found it yet. :(

{
  "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
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

Upvotes: 4

Views: 1391

Answers (2)

ice
ice

Reputation: 159

A little late here but I had the same problem and actually found the solution in the Instagram API documentation. I tried it in the facebook graph api and it worked perfectly.

In the webhook event, you have the message ID and the sender ID. Using the sender ID, you can make a call to the Graph API (yes, still an additional API call) using the following endpoint:

/{page_id}/conversations?access_token={access_token}&user_id={user_id}

this will return only the conversation between your page and the specified user.

Upvotes: 1

Masroor
Masroor

Reputation: 1604

This might not be the ideal solution, but can serve as an improvisation. Since, you have the message_id you can fetch message to determine who the participants are, then you can match them with the participants in the conversations.participants.
What I did was to fetch all the conversations with their participants field. i.e

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

Which returns

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

In the participants.data one participant is the page and the other is page_scoped_user. You can parse the response to match your participants and get the conversation id e.g here the conversation id would be "t_781422919067688"

Upvotes: 1

Related Questions