skip to Main Content

We’d like to add the facebook messenger checkbox plugin at the end of a request form so users can opt-in for notifications via messenger.

When the user opts in, our webhook receives a callback with the user_ref that we set in the form.

We send a confirmation of opt-in to this user_ref

But other messages we receive like delivery, read receipt or actual messages from the user do contain the user ref anymore but the user id.

This is the official documentation of facebook:

After you receive the callback event, you can call the Send API to start messaging the user using the user_ref identifier in recipient as shown below. Note that this field is the same as the unique user_ref param used before when the plugin was rendered and in confirming the opt-in.

If the call to the Send API is successful, the response will contain a recipient_id parameter, which is a stable user ID that you can now use in future API calls.

Therefore it’s impossible to keep track between the initial message and new ones. Does anyone found a solution for this?

Thank you very much in advance.

2

Answers


  1. You can, for example, send additional information when the user opts in using the optional ref parameter. You can send the username of the user logged on my website:

    function confirmOptIn() {
          FB.AppEvents.logEvent('MessengerCheckboxUserConfirmation', null, {
            'app_id':'APP_ID',
            'page_id':'PAGE_ID',
            'ref': '[email protected]',
            'user_ref':'UNIQUE_REF_PARAM'
          });
    

    You will receive the username within optin in your webhook event:

    {
      "recipient":{
        "id":"PAGE_ID"
      },
      "timestamp":1234567890,
      "optin":{
        "ref":"[email protected]",
        "user_ref":"UNIQUE_REF_PARAM"
      }
    }   
    

    Then, you can call the Send API to start messaging the user using the user_ref.

    If the call to the Send API is successful, the response will contain a recipient_id parameter, which is a stable user ID that you can now use in future API calls.

    …so you will received the Messenger ID which you can map to the username of your website you already have. Here, I modified a little the example from the official developers site to call the send API with user_ref and map the user ID I get in the response to the username of my website:

    function callSendAPICheckbox(messageData, userApplicationId) {
    ((userApplicationId) => {
        request({
                uri: 'https://graph.facebook.com/v2.6/me/messages',
                qs: {
                    access_token: PAGE_ACCESS_TOKEN
                },
                method: 'POST',
                json: messageData
    
            },
            function(error, response, body) {
                if (!error && response.statusCode == 200) {
                    var recipientId = body.recipient_id;
                    var messageId = body.message_id;
    
                    if (messageId) {
                        console.log("Map messenger user ID %s with the username of my website %s", recipientId, userApplicationId);
                    } else {
                        console.log("Successfully called Send API for recipient %s",
                            recipientId);
                    }
                } else {
                    console.error("Failed calling Send API for userId " +
                        recipientId, response.statusCode, response.statusMessage, body.error);
                }
            });
    
    })(userApplicationId)
    }
    
    Login or Signup to reply.
  2. Why don’t you make use of metadata field of sendTextMessage. Each and every message you send to your user, you send the metadata too, and when you receive response of the message being delivered, you find the metadata field in it.

    Here is what I do:

    When user select the checkbox plugin and event is triggered I receive the call on my server, check if it contains user_ref. If it does, then I send a text message to user with a custom metadata using user_ref. When user receives the message, the webhook send me a json data as mentioned in the documentation. To identify for which user_ref I have received this response, I set custom metadata which is combination of some string + user_ref before sending the message to user using user_ref. Using this custom metadata I identify the sender.id of the user for which I previously sent message using user_ref. The sender.id is my pageid and recipient.id the the user id which you are trying to get and using which we generally send message to the user and is also know as psid.

    Above if just the logical part mentioned which I usually do.
    For detail solution along with code, I have already posted it here:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search