This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[Slack] 👻 Add Ephemeral Message Support #958
Merged
Merged
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ var slackWebApi = require(__dirname + '/Slack_web_api.js'); | |
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var Back = require('back'); | ||
|
||
|
||
module.exports = function(botkit, config) { | ||
var bot = { | ||
type: 'slack', | ||
|
@@ -338,6 +337,10 @@ module.exports = function(botkit, config) { | |
}; | ||
|
||
bot.send = function(message, cb) { | ||
if (message.ephemeral) { | ||
bot.sendEphemeral(message, cb) | ||
return | ||
} | ||
botkit.debug('SAY', message); | ||
|
||
/** | ||
|
@@ -413,6 +416,39 @@ module.exports = function(botkit, config) { | |
cb && cb(err); | ||
} | ||
} | ||
} | ||
bot.sendEphemeral = function (message, cb) { | ||
botkit.debug('SAY EPHEMERAL', message); | ||
|
||
/** | ||
* Construct a valid slack message. | ||
*/ | ||
var slack_message = { | ||
type: message.type || 'message', | ||
channel: message.channel, | ||
text: message.text || null, | ||
user: message.user, | ||
as_user: message.as_user || false, | ||
parse: message.parse || null, | ||
link_names: message.link_names || null, | ||
attachments: message.attachments ? | ||
JSON.stringify(message.attachments) : null, | ||
}; | ||
console.log({message}) | ||
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. Why not use 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. No good reason, ty for catching it |
||
console.log({slack_message}) | ||
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. Same here, is better use |
||
bot.msgcount++; | ||
|
||
if (!bot.config.token) { | ||
throw new Error('Cannot use web API to send messages.'); | ||
} | ||
|
||
bot.api.chat.postEphemeral(slack_message, function(err, res) { | ||
if (err) { | ||
cb && cb(err); | ||
} else { | ||
cb && cb(null, res); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
|
@@ -615,6 +651,27 @@ module.exports = function(botkit, config) { | |
if (src.thread_ts) { | ||
msg.thread_ts = src.thread_ts; | ||
} | ||
if (msg.ephemeral && ! msg.user) { | ||
msg.user = src.user | ||
msg.as_user = true | ||
} | ||
|
||
bot.say(msg, cb); | ||
}; | ||
|
||
bot.whisper = function(src, resp, cb) { | ||
var msg = {}; | ||
|
||
if (typeof(resp) == 'string') { | ||
msg.text = resp; | ||
} else { | ||
msg = resp; | ||
} | ||
|
||
msg.channel = src.channel; | ||
msg.user = src.user; | ||
msg.as_user = true | ||
msg.ephemeral = true | ||
|
||
bot.say(msg, cb); | ||
}; | ||
|
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.
I chose to retain a single send function, and just check if the message should be ephemeral or not
Using send like this also preserves access to these messages from send middlewares!