Facebook Messenger Bot 持久菜单

Facebook Messenger Bot Persistent Menu

我正在生成我的第一个使用 node.js 和 heroku 的机器人,但发现在理解持久菜单功能时遇到一些困难。

问题 1) 如何将事件附加为回调?

function persistentMenu(sender){
 request({
    url: 'https://graph.facebook.com/v2.6/me/thread_settings',
    qs: {access_token:token},
    method: 'POST',
    json:{
        setting_type : "call_to_actions",
        thread_state : "existing_thread",
        call_to_actions:[
            {
              type:"postback",
              title:"FAQ",
              payload:"DEVELOPER_DEFINED_PAYLOAD_FOR_HELP"
            },
            {
              type:"postback",
              title:"I Prodotti in offerta",
              payload:"DEVELOPER_DEFINED_PAYLOAD_FOR_HELP"
            },
            {
              type:"web_url",
              title:"View Website",
              url:"https://google.com/"
            }
          ]
    }

}, function(error, response, body) {
    console.log(response)
    if (error) {
        console.log('Error sending messages: ', error)
    } else if (response.body.error) {
        console.log('Error: ', response.body.error)
    }
})

}

问题 2) 我发现清空持久菜单并生成新菜单的唯一方法是通过终端 ("as Facebook documented") 发出删除请求m 是否有可能清除插入刷新功能我的 app.js 文件?

curl -X DELETE -H "Content-Type: application/json" -d '{"setting_type":"call_to_actions","thread_state":"existing_thread"}' "https://graph.facebook.com/v2.6/me/thread_settingsaccess_token=PAGE_ACCESS_TOKEN"    

FB 示例机器人的回调结构不完善。我还没有找到在 Node 回调或承诺模型中构建示例的好方法。我相信 Node 专家可以重组它。

至于永久菜单,如果您发送一个空 call_to_actions 数组,菜单将会消失。菜单看起来有点 'sticky',但是当消息发送时它不会立即 appear/disappear。

我将您的代码片段合并到我的示例机器人中。你可以在

看到它

https://messenger.com/t/dynamicmemorysolutions

来源位于:

https://github.com/matthewericfisher/fb-robot

查看 add/remove 菜单命令和功能。

编辑:永久菜单 API 已更新。有关详细信息,请参阅 问题。

这对我有用:

    function menuButton() {
  var messageData = {
    setting_type : "call_to_actions",
    composerinputdisabled :"TRUE",
    thread_state : "existing_thread",
    call_to_actions:[
    {
      type:"postback",
      title:"¿Tiempo de espera?",
      payload:"ACTUALIZAR"
    },
    {
      type:"postback",
      title:"Ver Promociones",
      payload:"DEVELOPER_DEFINED_PAYLOAD_FOR_START_ORDER"
    }
    ]    
  }
  request({
    uri: 'https://graph.facebook.com/v2.6/me/thread_settings',
    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;

      console.log("Successfully sent generic message with id %s to recipient %s", 
        messageId, recipientId);
    } else {
      console.error("Unable to send message.");
      console.error(response);
      console.error(error);
    }
    });
    }

我在开头调用了这个函数

    app.post('/webhook', function(req, res){

    var data = req.body;

    if (data.object == 'page') {
    menuButton();

    data.entry.forEach(function(entry) {
     var pageID = entry.id;
     var timeOfEvent = entry.time;

          // Iterate over each messaging event
          entry.messaging.forEach(function(event) {
         if (event.message) {
           receivedMessage(event);

         }else if (event.postback) {
          receivedPostback(event);
        } else {
         console.log("Webhook received unknown event: ", event);
       }
     });
        });

    res.sendStatus(200);
  }

})

我无法做的是删除自由文本输入选项。 Facebook 声称现在是可能的,但没有找到关于如何做到这一点的说明或示例。有什么线索吗?

如果您想禁用自由文本输入,您应该将以下参数添加到您的永久菜单请求中:

"composer_input_disabled": true

而不是

composerinputdisabled :"TRUE"

FB API document 声明要将持久菜单应用到页面特定机器人的 API link 是:

https://graph.facebook.com/v2.6/me/messenger_profile?access_token=<PAGE_ACCESS_TOKEN>

请注意版本号后的 me,即本例中的 v2.6。然而,this did not worked for a lot of people

在 API link 中有小的变化:

graph.facebook.com/v2.6/Page ID/messenger_profile?access_token=PAGE ACCESS TOKEN

请注意,me 已替换为 fb 页面 ID。

并且示例负载仍然相同:

{
  "get_started": {
    "payload": "Get started"
  },
  "persistent_menu": [
    {
      "locale": "default",
      "composer_input_disabled": false,
      "call_to_actions": [
        {
          "title": "Subscribe",
          "type": "postback",
          "payload": "subscribe"
        },
        {
          "title": "Stop notifications",
          "type": "nested",
          "call_to_actions": [
            {
              "title": "For 1 week",
              "type": "postback",
              "payload": "For_1_week"
            },
            {
              "title": "For 1 month",
              "type": "postback",
              "payload": "For_1_month"
            },
            {
              "title": "For 1 year",
              "type": "postback",
              "payload": "For_1_year"
            }
          ]
        },
        {
          "title": "More",
          "type": "nested",
          "call_to_actions": [
            {
              "title": "Fresh jobs",
              "type": "postback",
              "payload": "fresh jobs"
            },
            {
              "title": "Like us",
              "type": "web_url",
              "url": "https://www.facebook.com/onlysoftwarejobs/"
            },
            {
              "title": "Feedback",
              "type": "web_url",
              "url": "https://docs.google.com/forms/d/e/1FAIpQLScjgFRbfBLznO55kFIskcH_eFc23zRSUUxzIgv_o44uj0GMpw/viewform"
            }
          ]
        }
      ]
    }
  ]
}

请注意,在设置 persistent_menu 之前必须配置 get_started 按钮。