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

Add callback support to bot::{reply,send} #440

Closed
wants to merge 2 commits into from
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"dependencies": {
"@slack/client": "^3.4.0",
"lodash": "^3.10.1"
"lodash": "^3.10.1",
"es6-promise": "~4.1.1"
},
"devDependencies": {
"coffee-coverage": "~1.0.1",
Expand Down
10 changes: 10 additions & 0 deletions src/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SlackClient = require './client'
ReactionMessage = require './reaction-message'
SlackTextMessage = require './slack-message'
Promise = global.Promise || require('es6-promise');

# Public: Adds a Listener for ReactionMessages with the provided matcher,
# options, and callback
Expand Down Expand Up @@ -126,23 +127,32 @@ class SlackBot extends Adapter
Hubot is sending a message to Slack
###
send: (envelope, messages...) ->
callback = ->
if typeof(messages[messages.length - 1]) == 'function'
callback = messages.pop()

sent_messages = []
for message in messages
if message isnt ''
sent_messages.push @client.send(envelope, message)
Promise.all(messages).then(callback.bind(null, null), callback)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this supposed to be Promise.all(sent_messages)...?

return sent_messages


###
Hubot is replying to a Slack message
###
reply: (envelope, messages...) ->
callback = ->
if typeof(messages[messages.length - 1]) == 'function'
callback = messages.pop()
sent_messages = []
for message in messages
if message isnt ''
message = "<@#{envelope.user.id}>: #{message}" unless envelope.room[0] is 'D'
@robot.logger.debug "Sending to #{envelope.room}: #{message}"
sent_messages.push @client.send(envelope, message)
Promise.all(messages).then(callback.bind(null, null), callback)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto to above

return sent_messages


Expand Down
9 changes: 9 additions & 0 deletions test/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ describe 'Send Messages', ->
@stubs._dmmsg.should.eql 'message'
@stubs._room.should.eql @stubs.user.id

it 'Should send a message with a callback', (done) ->
sentMessages = @slackbot.send {room: 'general'}, 'message', done
sentMessages.length.should.equal 1
sentMessages[0].should.equal 'message'

describe 'Client sending message', ->
it 'Should append as_user = true', ->
Expand Down Expand Up @@ -96,6 +100,11 @@ describe 'Reply to Messages', ->
sentMessages.length.should.equal 1
sentMessages[0].should.equal "message"

it 'Should call the callback', (done) ->
sentMessages = @slackbot.reply {user: @stubs.user, room: @stubs.channel.id}, 'message', done
sentMessages.length.should.equal 1
sentMessages[0].should.equal "<@#{@stubs.user.id}>: message"

describe 'Setting the channel topic', ->
it 'Should set the topic in channels', ->
@slackbot.setTopic {room: @stubs.channel.id}, 'channel'
Expand Down