Skip to content

Commit

Permalink
0.16.15 to 0.17.6
Browse files Browse the repository at this point in the history
some missing updates
  • Loading branch information
fczuardi committed Nov 3, 2016
2 parents c4a2ca1 + 8d72b68 commit 2728d94
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ language: node_js

node_js:
- "6"
- "5"
- "4"
# - "5"
# - "4"

git:
depth: 3
Expand Down
13 changes: 9 additions & 4 deletions localbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { FacebookMessengerBot } from './src/lib/facebook'; // eslint-disable-lin

const {
PORT,
FB_CALLBACK_PATH,
FB_CALLBACK_PATH
} = process.env;

const listeners = {
onUpdate: (update) => {
onUpdate: update => {
console.log('Update:', update.update);
}
}
};
const bot = new FacebookMessengerBot({ listeners });
bot.start().then(() => {
console.log(`Bot listening on localhost:${PORT}${FB_CALLBACK_PATH}`)
console.log(`Bot listening on localhost:${PORT}${FB_CALLBACK_PATH}`);
console.log('Test of setting welcome message');
bot.setWelcomeMessage('Hello World')
.then(f => {
console.log(f);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "calamars",
"version": "0.16.15",
"version": "0.17.6",
"description": "proto-framework",
"author": "Fabricio C Zuardi",
"license": "AGPL-3.0",
Expand Down
5 changes: 3 additions & 2 deletions src/lib/calamar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const isTelegramUpdate = update => (update && update.update_id);
// message update object format containing commonly used attributes.
const calamarMessageFormat = update => {
if (isFacebookMessengerMessagingItem(update)) {
const { message, timestamp, sender, recipient } = update;
const { mid, text } = message;
const { message = {}, timestamp, sender, recipient } = update;
const { mid = null, text = null, is_echo = false } = message; // eslint-disable-line
return {
text,
timestamp,
messageId: mid,
isEcho: is_echo,
senderId: sender.id,
recipientId: recipient.id,
chatId: sender.id,
Expand Down
17 changes: 13 additions & 4 deletions src/lib/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ class FacebookMessengerBot {
// - **callbackPath** - _string_ - The endpoint path of the setup [Callback URL][fbwebhook]
// - **verifyToken** - _string_ - The [Verify Token][fbwebhook]
// - **pageTokens** - _Array_ - A list of page tokens to subscribe to
// - **appSecret** - _string_ - The app secret of your facebook app, used
// to verify that the source of the updates is Facebook
// - **staticFiles** - _Array_ - List of specific static files to be served
// - **autoSubscribe** - _boolean_ - If true will attempt to subscribe
// your facebook app to all facebook pages in **pageTokens** using an API call
// by the same expressjs server that runs the bot. Each item of this array
// should be an object in the format
// ```{path: '\some_get_endpoit', file: 'path/to/static.file'}```
Expand All @@ -68,6 +72,7 @@ class FacebookMessengerBot {
pageTokens = [FB_PAGE_ACCESS_TOKEN],
appSecret = FB_APP_SECRET,
staticFiles = [],
autoSubscribe = false,
listeners = {}
} = options;

Expand All @@ -84,6 +89,7 @@ class FacebookMessengerBot {
this.listeners = listeners;
this.port = port;
this.pageTokens = pageTokens;
this.autoSubscribe = autoSubscribe;
this.server = {};
}

Expand All @@ -104,15 +110,18 @@ class FacebookMessengerBot {
start() {
const app = express();
this.setupExpressApp(app);
let bot = this;
const bot = this;
const launchPromise = new Promise(resolve => {
bot.server = app.listen(bot.port, () => {
if (!bot.autoSubscribe) {
return resolve(true);
}
const subscriptionPromises = bot.pageTokens.map(token =>
pageSubscribe(token)
);
Promise.all(subscriptionPromises).then(() => {
return resolve(true);
}).catch(e => console.error(e.message));
return Promise.all(subscriptionPromises)
.then(() => resolve(true))
.catch(e => console.error(e.message));
});
});
return launchPromise;
Expand Down
44 changes: 27 additions & 17 deletions src/lib/facebookGraphHelpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from 'request-promise';

const apiURL = 'https://graph.facebook.com/v2.6/';
const apiURL = 'https://graph.facebook.com/v2.7/';

// ## pageSubscribe(pageAccessToken)
// Subscribe your facebook app/bot to a facebook page's webhook updates.
Expand All @@ -17,14 +17,14 @@ const pageSubscribe = pageAccessToken => {
access_token: pageAccessToken
}
};
return new Promise(resolve => {
return request(requestOptions)
.then(result => resolve(result))
.catch(e => {
console.error(e.message);
return resolve(e.message);
});
});
return new Promise(resolve =>
request(requestOptions)
.then(result => resolve(result))
.catch(e => {
console.error(e.message);
return resolve(e.message);
})
);
};

// ## sendTextMessage(message, pageAccessToken)
Expand Down Expand Up @@ -171,14 +171,24 @@ const threadSettings = ({ type, state, text, cta }, pageId, pageAccessToken) =>
}
});
}
return request({
...requestOptions,
body: {
thread_state: state,
setting_type: type,
call_to_actions: cta
}
});
return new Promise(resolve =>
request({
...requestOptions,
body: {
thread_state: state,
setting_type: type,
call_to_actions: cta
}
})
.then(response => {
console.log('DEBUG: threadSettings response headers', response.headers);
return resolve(response);
})
.catch(error => {
console.log('DEBUG: threadSettings error headers', error.response.headers);
return resolve(error.message);
})
);
};

// ## setWelcomeMessage(message, pageId, pageAccessToken)
Expand Down
1 change: 1 addition & 0 deletions test/calamar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test('Translate Facebook Messenger messaging item to Calamar message format', t
text,
timestamp,
messageId: mid,
isEcho: false,
senderId,
recipientId,
chatId: senderId,
Expand Down

0 comments on commit 2728d94

Please sign in to comment.