I have several facebook pages and I would like to run the same bot on each page. I am using the bot framework and everything works perfectly for one page now. How do I associate with multiple pages?
5 Answers
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,
})

- 3,626
- 3
- 30
- 39
-
@UriAbramson Yes, let me know if you have any questions about implementing it – Danny Sullivan Sep 18 '17 at 23:20
-
This solution will not work with the [Bot Framework](https://dev.botframework.com/) – Alexander Jul 16 '18 at 19:08
-
What if you were doing this for pages that didn't belong to you as a middleware? Is there a way to accomplish this without going in and getting that pageAccessToken manually? – syntakks Mar 09 '19 at 05:27
-
@syntakks I don't believe there would be a way to send messages on behalf of pages that don't belong to you. You'll need to have the access token. – Danny Sullivan Mar 09 '19 at 16:33
Sorry if my answer is late
You can very well handle all your page traffic through just one bot backend
- Create an fb app and select product as messenger
- Add webook config pointing to your bot
- Select all the pages you want to associate one by one And keep the page access token handy.
- Go and search page id in your fb page and keep it handy
- Either in constant or dB maintain page access token against the page I’d
- When you get a callback on webhook you get a page entry and Id== page id
- Based on page I’d have your business logic
- Call send api using page access token which you have stored againtst the page id
Hope this helps

- 21
- 1
- 3
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

- 366
- 5
- 17
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.

- 2,213
- 2
- 18
- 23
-
i have an approved facebook app, i want to use this bot for a page i donot own, i have that page's ID and token number, yet i am unable to use my bot. how do i do it? – talhatahir Jun 16 '17 at 17:20
-
this is wrong, you CAN use one app on as many pages as you want to. you don´t need a separate app for each page. – andyrandy Feb 19 '18 at 13:58
-
@luschn you can have the same FB app published to more than one FB page. But unless I am missing something: the issue is that the FB bot framework connector needs settings to the FB page as well as the app. Therefore there is no way to link it to multiple pages. – Ed Y Mar 14 '18 at 17:31
-
of course you can install one bot to multiple pages. the only thing you cannot do it set one bot to multiple callback urls, that´s all. – andyrandy Mar 14 '18 at 18:06
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.

- 710
- 1
- 8
- 24