例如,您可以在用户选择使用可选ref
参数时发送附加信息。您可以发送登录我网站的用户的用户名:
function confirmOptIn() {
FB.AppEvents.logEvent('MessengerCheckboxUserConfirmation', null, {
'app_id':'APP_ID',
'page_id':'PAGE_ID',
'ref': 'myuser@mywebsite.com',
'user_ref':'UNIQUE_REF_PARAM'
});
optin
您将在 webhook 事件中收到用户名:
{
"recipient":{
"id":"PAGE_ID"
},
"timestamp":1234567890,
"optin":{
"ref":"myuser@mywebsite.com",
"user_ref":"UNIQUE_REF_PARAM"
}
}
然后,您可以调用 Send API 以使用user_ref
.
如果对 Send API 的调用成功,则响应将包含一个 recipient_id 参数,这是一个稳定的用户 ID,您现在可以在未来的 API 调用中使用它。
...因此您将收到 Messenger ID,您可以将其映射到您已有的网站用户名。在这里,我对官方开发者网站上的示例进行了一些修改,以调用发送 API,user_ref
并将我在响应中获得的用户 ID 映射到我网站的用户名:
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)
}