Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required changes for twitter quick reply feature #43

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 81 additions & 64 deletions lib/base_bot.js
Original file line number Diff line number Diff line change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rawMessage object should contain enough information for you to know that this is a quick_reply in the twitter-dm package

})
.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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.type is also available in the botmaster-twitter-dm BotClass object. No need to include it here. addQuickReplies should have a different behaviour, this should be done by overriding the method in the TwitterDMBot class. But it doesn't look necessary here.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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