2016-07-24 36 views
-1

當我創建一個帶有node.js API的facebook messenger bot時,我需要將它連接到我的Facebook頁面(而不是個人配置文件),因此用戶可以單擊'消息'(默認情況下,它允許向該頁面寫入消息)按鈕開始使用機器人。如何分離Facebook機器人和函數以寫入消息到頁面?

那麼,我該如何保存這兩個功能:寫一條消息到頁面並開始使用機器人?

回答

0

那麼,我該如何保存這兩個功能:寫一條消息到頁面並開始使用機器人?

我打算將此解釋爲「如何設置Messenger問候語並開始使用按鈕」。

下面是關於如何在node.js中做這些的例子:

//this sets the messenger greeting 
function setMessengerGreeting(){ 
    var messageData = { 
    setting_type: "greeting", 
    greeting: { 
     text: "Hi mom" 
    } 
    } 
    callSendAPISetup(messageData); 
} 

//this sets the Get Started button and welcome message 
function setWelcomeMessage(){ 
    var messageData = { 
    setting_type: "call_to_actions", 
    thread_state: "new_thread", 
    call_to_actions: [ 
     {payload: "hi"} 
    ] 
    } 
    callSendAPISetup(messageData); 
} 

//Sends the messageData for setup 
function callSendAPISetup(messageData) { 
    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) { 
     console.log('response: ' + response.body.result); 
    } else { 
     console.log('error sending curl'); 
     console.error(response); 
     console.error(error); 
    } 
    }); 
} 

文檔:

Messenger的問候:https://developers.facebook.com/docs/messenger-platform/thread-settings/greeting-text

入門按鈕:https://developers.facebook.com/docs/messenger-platform/thread-settings/get-started-button

+0

如果這ISN」你在找什麼,請澄清你的問題。我不是你正在問的問題。 – user2322082