Facebook Messenger Platform Setup

我怕爱的太早我们不能终老 提交于 2019-12-11 07:32:39

问题


I have a general question regarding the setup for a "bot" in the Facebook Messenger Platform. If I understand the architecture right, I can create an App as a developer add the Messenger function and associate 1 Page with the Messenger function. Does this mean I need an app for each page ? Or could I crete a "bot backend" serving multiple / different pages from different users ?


回答1:


Yes, you can have one robot serving multiple pages. You just have to set <token> for different pages in API call, here is setup for a page. From documentation:

Graph API requires Page access tokens to manage Facebook Pages. They are unique to each Page, admin and app and have an expiration time.




回答2:


Fritak is correct. You can use one app for multiple pages. For each page you will have to subscribe the app to that page and generate a page access token specifically for that page. At your webhook, you'll have to distinguish the callbacks for the specific page.




回答3:


When you receive a request, you need to map the incoming page id to the access token as described in this answer: How can I use the same bot on multiple facebook pages using bot framework

app.post('/webhook', (req, res) => {
    const data = req.body

    // Make sure this is a page subscription
   if (data.object === 'page') {
       // Iterate over each entry
       data.entry.forEach((pageEntry) => {
           // get the pageId
           const pageId = pageEntry.id
           ...
const accessTokens = {
    myPageId1: 'myPageAccessToken1',
    myPageId2: 'myPageAccessToken2',
}
const callSendAPI = (pageId, messageData) =>
    rp({
        uri: 'https://graph.facebook.com/v2.8/me/messages',
        qs: { access_token: accessTokens[pageId] },
        method: 'POST',
        body: messageData,
        json: true,
     })


来源:https://stackoverflow.com/questions/36664400/facebook-messenger-platform-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!