-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from bottenderjs/facebook-batch-multi-pages
add batch support to multi pages
- Loading branch information
Showing
12 changed files
with
217 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
PAGE_1_PAGE_ID=XXX | ||
PAGE_1_ACCESS_TOKEN=XXX | ||
|
||
PAGE_2_PAGE_ID=XXX | ||
PAGE_2_ACCESS_TOKEN=XXX | ||
|
||
APP_SECRET=XXX | ||
VERIFY_TOKEN=XXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const server = require('./server'); | ||
|
||
server.listen(5000, () => { | ||
console.log(`Server is running on localhost:5000`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"scripts": { | ||
"dev": "nodemon --exec \"node -r dotenv/config\" index.js", | ||
"start": "node -r dotenv/config index.js" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^1.11.0" | ||
}, | ||
"dependencies": { | ||
"bottender": "latest", | ||
"bottender-facebook": "latest", | ||
"dotenv": "^4.0.0", | ||
"messenger-batch": "^0.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { Bot } = require('bottender'); | ||
const { createServer } = require('bottender/koa'); | ||
const { FacebookConnector } = require('bottender-facebook'); | ||
const { isError613 } = require('messenger-batch'); | ||
|
||
const PAGE_1_PAGE_ID = process.env.PAGE_1_PAGE_ID; | ||
const PAGE_1_ACCESS_TOKEN = process.env.PAGE_1_ACCESS_TOKEN; | ||
|
||
const PAGE_2_PAGE_ID = process.env.PAGE_2_PAGE_ID; | ||
const PAGE_2_ACCESS_TOKEN = process.env.PAGE_2_ACCESS_TOKEN; | ||
|
||
const APP_SECRET = process.env.APP_SECRET; | ||
const VERIFY_TOKEN = process.env.VERIFY_TOKEN; | ||
|
||
const mapPageToAccessToken = pageId => { | ||
switch (pageId) { | ||
case PAGE_1_PAGE_ID: | ||
return PAGE_1_ACCESS_TOKEN; | ||
case PAGE_2_PAGE_ID: | ||
default: | ||
return PAGE_2_ACCESS_TOKEN; | ||
} | ||
}; | ||
|
||
const bot = new Bot({ | ||
connector: new FacebookConnector({ | ||
accessToken: PAGE_1_ACCESS_TOKEN, // Top level access token should be specified for batch request. | ||
appSecret: APP_SECRET, | ||
mapPageToAccessToken, | ||
batchConfig: { | ||
delay: 1000, | ||
shouldRetry: isError613, // (#613) Calls to this api have exceeded the rate limit. | ||
retryTimes: 2, | ||
}, | ||
}), | ||
}); | ||
|
||
bot.onEvent(async context => { | ||
if (context.event.isCommentAdd && !context.event.isSentByPage) { | ||
try { | ||
await context.sendPrivateReply('OK!'); | ||
await context.sendComment('Public reply!'); | ||
await context.sendLike(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} else { | ||
await context.sendText('text..'); | ||
} | ||
}); | ||
|
||
const server = createServer(bot, { verifyToken: VERIFY_TOKEN }); | ||
|
||
module.exports = server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
USER_TOKEN=XXX | ||
PAGE_ID=XXX | ||
|
||
APP_ID=XXX | ||
APP_SECRET=XXX | ||
WEBHOOK_URL=XXX | ||
VERIFY_TOKEN=XXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
USER_TOKEN=XXX | ||
PAGE_ID=XXX | ||
|
||
APP_ID=XXX | ||
APP_SECRET=XXX | ||
WEBHOOK_URL=XXX | ||
VERIFY_TOKEN=XXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters