Skip to content

Commit

Permalink
Merge 0204807 into 24c378c
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekchotaliya committed Jul 5, 2018
2 parents 24c378c + 0204807 commit 6c9a3de
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 75 deletions.
145 changes: 81 additions & 64 deletions lib/base_bot.js
Expand Up @@ -246,44 +246,49 @@ class BaseBot extends EventEmitter {
const responseBody = {};
return this.__validateSendOptions(sendOptions)

.then(() => {
let outgoingMiddlewarePromise;
if (this.master && !sendOptions.ignoreMiddleware) {
outgoingMiddlewarePromise = this.master.middleware.__runOutgoingMiddleware(
this, this.__associatedUpdate, outgoingMessage);
} else {
// don't actually go through middleware
outgoingMiddlewarePromise = Promise.resolve(outgoingMessage);
}
return outgoingMiddlewarePromise;
})
.then(() => {
responseBody.sentOutgoingMessage = outgoingMessage;
return this.__formatOutgoingMessage(outgoingMessage, sendOptions);
})
.then((rawMessage) => {
responseBody.sentRawMessage = rawMessage;
return this.__sendMessage(rawMessage, sendOptions);
})
.then((rawBody) => {
responseBody.raw = rawBody;
return this.__createStandardBodyResponseComponents(
responseBody.sentOutgoingMessage,
responseBody.sentRawMessage,
responseBody.raw);
})
.then((StandardBodyResponseComponents) => {
responseBody.recipient_id = StandardBodyResponseComponents.recipient_id;
responseBody.message_id = StandardBodyResponseComponents.message_id;
return responseBody;
})
.catch((err) => {
if (err === 'cancel') {
return 'cancelled';
}
.then(() => {
let outgoingMiddlewarePromise;
if (this.master && !sendOptions.ignoreMiddleware) {
outgoingMiddlewarePromise = this.master.middleware.__runOutgoingMiddleware(
this, this.__associatedUpdate, outgoingMessage);
} else {
// don't actually go through middleware
outgoingMiddlewarePromise = Promise.resolve(outgoingMessage);
}
return outgoingMiddlewarePromise;
})
.then(() => {
responseBody.sentOutgoingMessage = outgoingMessage;
return this.__formatOutgoingMessage(outgoingMessage, sendOptions);
})
.then((rawMessage) => {
responseBody.sentRawMessage = rawMessage;
// twitter_qr is a flag to identify if message is for twitter platform
let twitter_qr = false
if (outgoingMessage.quick_replies) {
twitter_qr = true
}
return this.__sendMessage(rawMessage, sendOptions, twitter_qr);
})
.then((rawBody) => {
responseBody.raw = rawBody;
return this.__createStandardBodyResponseComponents(
responseBody.sentOutgoingMessage,
responseBody.sentRawMessage,
responseBody.raw);
})
.then((StandardBodyResponseComponents) => {
responseBody.recipient_id = StandardBodyResponseComponents.recipient_id;
responseBody.message_id = StandardBodyResponseComponents.message_id;
return responseBody;
})
.catch((err) => {
if (err === 'cancel') {
return 'cancelled';
}

throw err;
});
throw err;
});
}

/**
Expand Down Expand Up @@ -518,7 +523,7 @@ class BaseBot extends EventEmitter {
}
} else {
err = new TypeError('third argument must be a "String", an ' +
'attachment "Object" or absent');
'attachment "Object" or absent');
}
}

Expand Down Expand Up @@ -548,14 +553,26 @@ class BaseBot extends EventEmitter {
}

const quickReplies = [];
for (const buttonTitle of buttonTitles) {
quickReplies.push({
content_type: 'text',
title: buttonTitle,
payload: buttonTitle, // indeed, in default mode payload is buttonTitle
});

// create quick reply option based on bot type
// default - else type is facebook messenger
if (this.type === "twitter-dm") {
for (const buttonTitle of buttonTitles) {
quickReplies.push({
label: buttonTitle,
metadata: buttonTitle,
});
}
} else {
for (const buttonTitle of buttonTitles) {
quickReplies.push({
content_type: 'text',
title: buttonTitle,
payload: buttonTitle, // indeed, in default mode payload is buttonTitle
});
}
}
outgoingMessage.addQuickReplies(quickReplies);
outgoingMessage.addQuickReplies(this.type, quickReplies);
return this.sendMessage(outgoingMessage, arguments[3]);
}

Expand Down Expand Up @@ -636,11 +653,11 @@ class BaseBot extends EventEmitter {

return promiseCascade

.then((body) => {
// add last body and deal with potential callback
returnedBodies.push(body);
return returnedBodies;
});
.then((body) => {
// add last body and deal with potential callback
returnedBodies.push(body);
return returnedBodies;
});
}

/**
Expand Down Expand Up @@ -704,7 +721,7 @@ class BaseBot extends EventEmitter {
'with callback functions is no longer supported in botmaster 3. ');
} else if (typeof sendOptions !== 'object') {
err = new TypeError('sendOptions must be of type ' +
`object. Got ${typeof sendOptions} instead`);
`object. Got ${typeof sendOptions} instead`);
}

if (err) {
Expand All @@ -726,23 +743,23 @@ class BaseBot extends EventEmitter {
__emitUpdate(update) {
if (!this.master) {
return Promise.reject(new Error('bot needs to be added to a botmaster ' +
'instance in order to emit received updates'));
'instance in order to emit received updates'));
}

return this.master.middleware.__runIncomingMiddleware(this, update)
.catch((err) => {
// doing this, to make sure all errors (even ones rejected from
// promises within incoming middleware) can be retrieved somewhere;
if (err === 'cancel') {
return 'cancelled';
}
if (err && err.message) {
err.message = `"${err.message}". This is most probably on your end.`;
}
.catch((err) => {
// doing this, to make sure all errors (even ones rejected from
// promises within incoming middleware) can be retrieved somewhere;
if (err === 'cancel') {
return 'cancelled';
}
if (err && err.message) {
err.message = `"${err.message}". This is most probably on your end.`;
}

this.emit('error', err || 'empty error object', update);
return err;
});
this.emit('error', err || 'empty error object', update);
return err;
});
}

/**
Expand Down
18 changes: 9 additions & 9 deletions lib/botmaster.js
Expand Up @@ -161,8 +161,8 @@ class Botmaster extends EventEmitter {
*/
getBot(options) {
if (!options ||
(!options.type && !options.id) ||
(options.type && options.id)) {
(!options.type && !options.id) ||
(options.type && options.id)) {
throw new Error('\'getBot\' needs exactly one of type or id');
}

Expand All @@ -173,13 +173,13 @@ class Botmaster extends EventEmitter {
return find(this.bots, { type: options.type });
}

/**
* Extract all bots of given type.
*
* @param {string} botType (there can be multiple bots of a same type)
*
* @return {Array} Array of bots found
*/
/**
* Extract all bots of given type.
*
* @param {string} botType (there can be multiple bots of a same type)
*
* @return {Array} Array of bots found
*/
getBots(botType) {
if (typeof botType !== 'string' && !(botType instanceof String)) {
throw new Error('\'getBots\' takes in a string as only parameter');
Expand Down
10 changes: 8 additions & 2 deletions lib/outgoing_message.js
Expand Up @@ -172,8 +172,14 @@ class OutgoingMessage {
* OutgoingMessage
* @return {OutgoinMessage} returns this object to allow for chaining of methods.
*/
addQuickReplies(quickReplies) {
return this.__addProperty('message.quick_replies', 'quick_replies', quickReplies);
addQuickReplies(type, quickReplies) {
// create quick replies for twitter
// default else type is facebook messenger
if (type === "twitter-dm") {
return this.__addProperty('quick_replies', 'twitter quick_reply', quickReplies);
} else {
return this.__addProperty('message.quick_replies', 'quick_replies', quickReplies);
}
}

/**
Expand Down

0 comments on commit 6c9a3de

Please sign in to comment.