Identifying Facebook Messenger user with UserID from a Facebook Login

前端 未结 5 2014
逝去的感伤
逝去的感伤 2021-01-30 09:27

I am trying out the new Facebook Messenger Platform and have hit a bit of a problem.

When a user first chats with my Bot, I want to use the sender.id to loo

5条回答
  •  自闭症患者
    2021-01-30 09:36

    I solved it different way but it works perfectly by graph API,i read mailbox of my page so that when any one can interact by messenger bot than i read pages inbox and iterate through,

    Step 1:

    Do a HTTP get request by graph API , you want a PAGE_UNIQUE_ID from Graph API ,

    GET REQUEST

    https://graph.facebook.com/v2.6/?fields=conversations.limit(10){participants,updated_time,id}&access_token=
    

    PAGE_UNIQUE_ID Hints:

    Go ===> https://developers.facebook.com/tools/explorer/ ==> Press "Get Token" ===> Select your desired page ===>Finally Submit , You show a id on output that is PAGE_UNIQUE_ID in here.

    Check on browser for resoponse.

    Step 2:

    After doing above http request ,you get a JSON object that will show latest 10 conversation of pages where desired facebook id included.

    Step 3:

    Do iteration through by user full name or you can use lodash or underscore at your choice,

    getUserID(response,'Zahid Rahman')
    
    
    function getUserID(response, fullname) {
      if (response && response.conversations && response.conversations.data) { //Check response is correct
        var conversations = response.conversations.data;                       //Get all conversations
        for (var j = 0; j < conversations.length; j++) {
          var conversationsParticipants = conversations[j].participants.data; 
          for (var k = 0; k < conversationsParticipants.length; k++) {         //Get all particiapnts of a single conversation.
    
            var conversationsParticipantsEach = conversationsParticipants[k];
            if (conversationsParticipantsEach.name === fullname) {             //Check fullname match or not
              console.log("Desired Facebook User ID : " + conversationsParticipants[k].id);
              return conversationsParticipants[k].id;     
            } 
          }
        }
      }
    }
    

    Hope it will help you.

提交回复
热议问题