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
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:You will receive the username within
optin
in your webhook event:Then, you can call the Send API to start messaging the user using the
user_ref
.…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: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 usinguser_ref
. When user receives the message, thewebhook
send me ajson
data as mentioned in the documentation. To identify for whichuser_ref
I have received this response, I set custom metadata which is combination of somestring + user_ref
before sending the message to user usinguser_ref
. Using this custom metadata I identify thesender.id
of the user for which I previously sent message usinguser_ref
. Thesender.id
is mypageid
andrecipient.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 aspsid
.Above if just the logical part mentioned which I usually do.
For detail solution along with code, I have already posted it here: