-
Notifications
You must be signed in to change notification settings - Fork 47
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -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'); | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return this.sendMessage(outgoingMessage, arguments[3]); | ||
} | ||
|
||
|
@@ -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; | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -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) { | ||
|
@@ -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; | ||
}); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thetwitter-dm
package