Skip to content

Commit

Permalink
Merge pull request #61 from bottenderjs/facebook-batch-multi-pages
Browse files Browse the repository at this point in the history
add batch support to multi pages
  • Loading branch information
tw0517tw authored Nov 7, 2018
2 parents 02664ac + 6643561 commit a0bfadd
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 21 deletions.
8 changes: 8 additions & 0 deletions examples/facebook-batch-multi-pages/.env.sample
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
5 changes: 5 additions & 0 deletions examples/facebook-batch-multi-pages/index.js
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`);
});
15 changes: 15 additions & 0 deletions examples/facebook-batch-multi-pages/package.json
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"
}
}
54 changes: 54 additions & 0 deletions examples/facebook-batch-multi-pages/server.js
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;
7 changes: 7 additions & 0 deletions examples/facebook-batch/.env.sample
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
7 changes: 1 addition & 6 deletions examples/facebook-batch/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require('dotenv').config();
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
const APP_SECRET = process.env.APP_SECRET;
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
const POST_ID = process.env.POST_ID;

const bot = new Bot({
connector: new FacebookConnector({
Expand All @@ -25,11 +24,7 @@ const bot = new Bot({
bot.onEvent(async context => {
console.log(context.event);

if (
context.event.isCommentAdd &&
context.event.comment.post_id === POST_ID &&
!context.event.isSentByPage
) {
if (context.event.isCommentAdd && !context.event.isSentByPage) {
try {
await context.sendPrivateReply('OK!');
await context.sendComment('Public Reply!');
Expand Down
2 changes: 2 additions & 0 deletions examples/facebook-batch/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const axios = require('axios');

require('dotenv').config();

const USER_TOKEN = process.env.USER_TOKEN;
const PAGE_ID = process.env.PAGE_ID;

Expand Down
7 changes: 7 additions & 0 deletions examples/single-page/.env.sample
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
7 changes: 1 addition & 6 deletions examples/single-page/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require('dotenv').config();
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
const APP_SECRET = process.env.APP_SECRET;
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
const POST_ID = process.env.POST_ID;

const bot = new Bot({
connector: new FacebookConnector({
Expand All @@ -19,11 +18,7 @@ const bot = new Bot({
bot.onEvent(async context => {
console.log(context.event);

if (
context.event.isCommentAdd &&
context.event.comment.post_id === POST_ID &&
!context.event.isSentByPage
) {
if (context.event.isCommentAdd && !context.event.isSentByPage) {
try {
await context.sendPrivateReply('OK!');
await context.sendComment('Public Reply!');
Expand Down
2 changes: 2 additions & 0 deletions examples/single-page/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const axios = require('axios');

require('dotenv').config();

const USER_TOKEN = process.env.USER_TOKEN;
const PAGE_ID = process.env.PAGE_ID;

Expand Down
56 changes: 47 additions & 9 deletions src/FacebookBatch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
function sendPrivateReply(objectId: string, message: string) {
/* @flow */
/* eslint-disable camelcase */

import querystring from 'querystring';

function sendPrivateReply(objectId: string, message: string, options?: Object) {
return {
method: 'POST',
relative_url: `${objectId}/private_replies`,
body: {
message,
...options,
},
};
}
Expand All @@ -17,7 +23,8 @@ function sendComment(
attachment_share_url?: string,
attachment_url?: string,
message?: string,
}
},
options?: Object
) {
let body;

Expand All @@ -32,34 +39,65 @@ function sendComment(
return {
method: 'POST',
relative_url: `${objectId}/comments`,
body,
body: {
...body,
...options,
},
};
}

function sendLike(objectId: string) {
function sendLike(objectId: string, options?: Object) {
return {
method: 'POST',
relative_url: `${objectId}/likes`,
body: {
...options,
},
};
}

function getComment(
commentId: string,
{ fields }: { fields?: ?(Array<string> | string) } = {}
{
fields,
access_token,
}: { fields?: ?(Array<string> | string), access_token?: ?string } = {}
) {
const conjunctFields = Array.isArray(fields) ? fields.join(',') : fields;
const fieldsQuery = conjunctFields ? `fields=${conjunctFields}` : '';

const query = {};

if (conjunctFields) {
query.fields = conjunctFields;
}

if (access_token) {
query.access_token = access_token;
}

return {
method: 'GET',
relative_url: `${commentId}?${fieldsQuery}`,
relative_url: `${commentId}?${querystring.stringify(query)}`,
};
}

function getLikes(objectId: string, { summary }: { summary?: boolean } = {}) {
function getLikes(
objectId: string,
{ summary, access_token }: { summary?: boolean, access_token?: ?string } = {}
) {
const query = {};

if (summary) {
query.summary = summary;
}

if (access_token) {
query.access_token = access_token;
}

return {
method: 'GET',
relative_url: `${objectId}/likes?${summary ? 'summary=true' : ''}`,
relative_url: `${objectId}/likes?${querystring.stringify(query)}`,
};
}

Expand Down
68 changes: 68 additions & 0 deletions src/__tests__/FacebookBatch.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FacebookBatch from '../FacebookBatch';

const COMMENT_ID = '1234567890';
const CUSTOM_ACCESS_TOKEN = 'custom-access-token';

describe('sendPrivateReply', () => {
it('should create send private reply request', () => {
Expand All @@ -12,6 +13,21 @@ describe('sendPrivateReply', () => {
},
});
});

it('should support custom access token', () => {
expect(
FacebookBatch.sendPrivateReply(COMMENT_ID, 'ok', {
access_token: CUSTOM_ACCESS_TOKEN,
})
).toEqual({
method: 'POST',
relative_url: '1234567890/private_replies',
body: {
message: 'ok',
access_token: CUSTOM_ACCESS_TOKEN,
},
});
});
});

describe('sendComment', () => {
Expand All @@ -38,13 +54,43 @@ describe('sendComment', () => {
},
});
});

it('should support custom access token', () => {
expect(
FacebookBatch.sendComment(COMMENT_ID, 'ok', {
access_token: CUSTOM_ACCESS_TOKEN,
})
).toEqual({
method: 'POST',
relative_url: '1234567890/comments',
body: {
message: 'ok',
access_token: CUSTOM_ACCESS_TOKEN,
},
});
});
});

describe('sendLike', () => {
it('should create send like request', () => {
expect(FacebookBatch.sendLike(COMMENT_ID)).toEqual({
method: 'POST',
relative_url: '1234567890/likes',
body: {},
});
});

it('should support custom access token', () => {
expect(
FacebookBatch.sendLike(COMMENT_ID, {
access_token: CUSTOM_ACCESS_TOKEN,
})
).toEqual({
method: 'POST',
relative_url: '1234567890/likes',
body: {
access_token: CUSTOM_ACCESS_TOKEN,
},
});
});
});
Expand All @@ -56,6 +102,17 @@ describe('getComment', () => {
relative_url: '1234567890?',
});
});

it('should support custom access token', () => {
expect(
FacebookBatch.getComment(COMMENT_ID, {
access_token: CUSTOM_ACCESS_TOKEN,
})
).toEqual({
method: 'GET',
relative_url: `1234567890?access_token=${CUSTOM_ACCESS_TOKEN}`,
});
});
});

describe('getLikes', () => {
Expand All @@ -65,4 +122,15 @@ describe('getLikes', () => {
relative_url: '1234567890/likes?',
});
});

it('should support custom access token', () => {
expect(
FacebookBatch.getLikes(COMMENT_ID, {
access_token: CUSTOM_ACCESS_TOKEN,
})
).toEqual({
method: 'GET',
relative_url: `1234567890/likes?access_token=${CUSTOM_ACCESS_TOKEN}`,
});
});
});

0 comments on commit a0bfadd

Please sign in to comment.