Facebook Messenger Bot持久菜单

时间:2016-07-21 18:26:19

标签: node.js facebook heroku bots messenger

我正在生成我的第一个使用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)我找到用于清空持久性菜单并生成新菜单的唯一方法是通过终端删除请求(“如Facebook记录”)m是否有可能清除在我的应用程序上插入刷新功能.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"    

4 个答案:

答案 0 :(得分:3)

FB示例机器人的回调结构不合理。我还没有找到在Node回调或promise模型中构造示例的好方法。我确信Node专家可以重组它。

对于持久性菜单,如果发送空的call_to_actions数组,菜单将消失。菜单看起来有点“粘性”,因为它在发送消息时不会立即显示/消失。

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

看到它

https://messenger.com/t/dynamicmemorysolutions

来源位于:

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

请参阅添加/删除菜单命令和功能。

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

答案 1 :(得分:0)

这对我有用:

    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声称现在可能还没有找到关于如何做的说明或示例。有线索吗?

答案 2 :(得分:0)

如果要禁用自由文本输入,请将以下参数添加到持久性菜单请求中:

"composer_input_disabled": true

而不是

composerinputdisabled :"TRUE"

答案 3 :(得分:0)

FB API document表示用于将持久性菜单应用于页面特定机器人的API链接是:

Array
(
    [0] => Array
        (
            [id] => 108
            [id_tindak_lanjut] => 10.2
            [nilai_tindak_lanjut] => 0
            [memo_tindak_lanjut] => 
            [tindak_lanjut_no] => 1
            [tgl_tindak_lanjut] => 2018-01-19
        )

    [1] => Array
        (
            [id] => 109
            [id_tindak_lanjut] => 30.3
            [nilai_tindak_lanjut] => 0
            [memo_tindak_lanjut] => Tes
            [tindak_lanjut_no] => 2
            [tgl_tindak_lanjut] => 2018-01-22
        )

)

请注意版本号之后的 me ,在此特定情况下为https://graph.facebook.com/v2.6/me/messenger_profile?access_token=<PAGE_ACCESS_TOKEN> 。但是,this did not worked for a lot of people

要点击的API链接发生微小变化:

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

请注意, me 将替换为fb Page Id。

样本有效负载仍然可以相同:

v2.6

请注意,在设置{ "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" } ] } ] } ] } 之前必须配置get_started按钮。