Skip to content

Commit

Permalink
Add multi-page example (#13)
Browse files Browse the repository at this point in the history
* Move original example to single-page folder

* Add multi-page example
  • Loading branch information
kpman authored Dec 1, 2017
1 parent ade1344 commit 9ad3bdb
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 6 deletions.
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/multi-pages/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
WEBHOOK_URL=XXX
File renamed without changes.
14 changes: 14 additions & 0 deletions examples/multi-pages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"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-fb": "latest",
"dotenv": "^4.0.0"
}
}
44 changes: 44 additions & 0 deletions examples/multi-pages/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { Bot } = require('bottender');
const { createServer } = require('bottender/koa');
const { FacebookConnector } = require('bottender-fb');

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({
appSecret: APP_SECRET,
mapPageToAccessToken,
}),
});

bot.onEvent(async context => {
if (context.event.isCommentAdd) {
try {
await context.sendPrivateReply('OK!');
await context.sendComment('Public reply!');
} catch (err) {
console.log(err);
}
}
});

const server = createServer(bot, { verifyToken: VERIFY_TOKEN });

module.exports = server;
File renamed without changes.
6 changes: 4 additions & 2 deletions example/README.md → examples/single-page/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Single Page

## Install and Run

Download this example or clone [bottender-fb](https://github.com/bottenderjs/bottender-fb).

```
curl https://codeload.github.com/bottenderjs/bottender-fb/tar.gz/master | tar -xz --strip=2 bottender-fb-master/example
cd example
curl https://codeload.github.com/bottenderjs/bottender-fb/tar.gz/master | tar -xz --strip=2 bottender-fb-master/examples/single-page
cd single-page
npm install
npm run dev
```
Expand Down
5 changes: 5 additions & 0 deletions examples/single-page/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const server = require('./server');

server.listen(3000, () => {
console.log(`Server is running on localhost:3000`);
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions src/FacebookConnector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

import { MessengerConnector, MessengerContext } from 'bottender';
import { MessengerConnector } from 'bottender';
import warning from 'warning';

import FacebookContext from './FacebookContext';
Expand Down Expand Up @@ -67,14 +67,16 @@ export default class FacebookConnector extends MessengerConnector {
event,
session,
initialState,
}: Object): MessengerContext {
}: Object): FacebookContext {
let customAccessToken;

if (this._mapPageToAccessToken) {
const { rawEvent } = event;

let pageId = null;

if (rawEvent.message && rawEvent.message.is_echo && rawEvent.sender) {
if (event.isFeed) {
pageId = rawEvent.value.post_id.split('_')[0];
} else if (event.isEcho && rawEvent.sender) {
pageId = rawEvent.sender.id;
} else if (rawEvent.recipient) {
pageId = rawEvent.recipient.id;
Expand Down

0 comments on commit 9ad3bdb

Please sign in to comment.