How can I use the same bot on multiple facebook pages using bot framework

可紊 提交于 2019-11-29 02:45:32

Sorry if my answer is late

You can very well handle all your page traffic through just one bot backend

  1. Create an fb app and select product as messenger
  2. Add webook config pointing to your bot
  3. Select all the pages you want to associate one by one And keep the page access token handy.
  4. Go and search page id in your fb page and keep it handy
  5. Either in constant or dB maintain page access token against the page I’d
  6. When you get a callback on webhook you get a page entry and Id== page id
  7. Based on page I’d have your business logic
  8. Call send api using page access token which you have stored againtst the page id

Hope this helps

When you call the Facebook Send API, you pass a page access token through the access_token parameter. You can specify which page to direct your message by modifying this access token. To know which page initiated the message, you can access the id field of the entry of the message post.

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
           ...

You would then need to maintain an object mapping page ids to the access token associated with each page id:

const accessTokens = {
    myPageId1: 'myPageAccessToken1',
    myPageId2: 'myPageAccessToken2',
}

Then when sending the response, just specify the corresponding page access_token

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,
     })

You can subscribe same app to multiple pages. Once the facebook app is subscribed the messenger associated with that app would be associated with the page.

https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps/ this api is used to add facebook application to a page

Each page needs its own facebook application. Once you have created your applications you can link them to the same bot but they will use a different page token in case that you are validating the signature in your code and probably you want to use a different url for each of them.

The way I would handle this is to deploy the same Bot backend on a different server. By this, I will have the same backend source but different webhook URL. This makes each Bot, though similar functionality, can be isolated and maintained separately. This is extremely an important design consideration when building a Bot because it can potentially have a lot of conversations.

To answer your question, yes it's possible by just passing the page token and validation token for every request and change it when a user converse with the other page - but I don't recommend doing it this way.

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