Skip to content

Commit

Permalink
Allow sending multiple responses for FB
Browse files Browse the repository at this point in the history
  • Loading branch information
stojanovic committed Jun 18, 2016
1 parent 2b09213 commit 6e03c76
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/bot-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fbSetup = require('./facebook/setup');
const slackSetup = require('./slack/setup');
const telegramSetup = require('./telegram/setup');
const skypeSetup = require('./skype/setup');
const formatFbMessage = require('./facebook/format-message');
const fbTemplate = require('./facebook/format-message');

let logError = function (err) {
console.error(err);
Expand All @@ -29,4 +29,4 @@ module.exports = function botBuilder(messageHandler, optionalLogError) {
return api;
};

module.exports.formatFbMessage = formatFbMessage;
module.exports.fbTemplate = fbTemplate;
15 changes: 13 additions & 2 deletions lib/facebook/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,21 @@ module.exports = function fbReply(recipient, message, fbAccessToken) {
return sendSingle(messages.shift()).then(sendAll);
}
},
messages;
messages = [];

function breakTextAndReturnFormatted(message) {
return breakText(message, 320).map(m => ({ text: m }));
}

if (typeof message === 'string') {
messages = breakText(message, 320).map(m => ({ text: m }));
messages = breakTextAndReturnFormatted(message);
} else if (Array.isArray(message)) {
message.forEach(msg => {
if (typeof msg === 'string')
return messages.concat(breakTextAndReturnFormatted(msg));

messages.push(msg);
});
} else {
messages = [message];
}
Expand Down
10 changes: 10 additions & 0 deletions spec/facebook/facebook-reply-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var reply = require('../../lib/facebook/reply'),
https = require('https');
describe('Facebook Reply', () => {
'use strict';
beforeEach(() =>{
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
});
Expand Down Expand Up @@ -72,6 +73,15 @@ describe('Facebook Reply', () => {

reply('user123', fiveHundred, 'ACCESS123');
});
it('sends multiple messages in sequence if array is passed', () => {
let answers = ['foo', 'bar'];
https.request.pipe(() => {
Promise.resolve().then(() => {
expect(https.request.calls.length).toEqual(2);
});
});
reply('user123', answers, 'ACCESS123');
});
it('sends complex messages without transforming into a text object', done => {
https.request.pipe(callOptions => {
expect(JSON.parse(callOptions.body)).toEqual({
Expand Down

0 comments on commit 6e03c76

Please sign in to comment.