通过线程控制 Facebook 信使机器人

Passing Thread Control Facebook messenger bot

我正在为我的机器人尝试 Handover Protocal,但我无法获得 关于 passing thread control 虽然我仍然阅读文档,但我很困惑

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{"id":"<PSID>"},
  "target_app_id":123456789,
  "metadata":"String to pass to secondary receiver app" 
}' "https://graph.facebook.com/v2.6/me/pass_thread_control?access_token=<PAGE_ACCESS_TOKEN>"

示例pass_thread_control事件

{
  "sender":{
    "id":"<PSID>"
  },
  "recipient":{
    "id":"<PAGE_ID>"
  },
  "timestamp":1458692752478,
  "pass_thread_control":{
    "new_owner_app_id":"123456789",
    "metadata":"Additional content that the caller wants to set"
  }
}

我想知道这里的 PSID 是什么以及 target_app_id 是什么。 谁能帮我举个例子。

我花了一点时间才弄明白,所以我完全理解你的沮丧!在使用切换协议时没有足够的文档。

我当前的用例是一个机器人,如果它无法理解用户,它可以提供与代表交谈的选项。这是通过使用回发按钮完成的,这意味着切换由用户激活。用户点击按钮后,如下代码为运行:

graph.setAccessToken(<YOUR_APP_ACCESS_TOKEN>);
graph.setAppSecret(<YOUR_APP_SECRET>);
graph.setVersion("2.12");

graph.post(
    `me/pass_thread_control?access_token=${functions.config().messenger.token}`, 
    handoverMessage, 
    (err, res) => {
        if(err) {
            console.log('HANDOVER ERROR:', err);
            return;
        }                    
        console.log('HANDOVER SUCCESS:', res);
});

handoverMessage 变量是放置 psid 和 target_app_id:

的地方
let handoverMessage = {
    "recipient": {
        "id": <PSID>
     },
     "target_app_id": <YOUR_TARGET_APP_ID>
}

调用此代码会将对话的控制权移交给您的 target_app_id。

如果您想知道 'graph' 是什么,它是一个名为 'fbgraph' 的节点包,我发现它在处理 facebook 图 api 时非常有用。它肯定不是唯一的包装器,可能还有更好的包装器,但到目前为止它对我有用。