diff --git a/src/bot.coffee b/src/bot.coffee index 14a5d9ed..43474796 100644 --- a/src/bot.coffee +++ b/src/bot.coffee @@ -2,6 +2,7 @@ {SlackTextMessage, ReactionMessage, PresenceMessage, FileSharedMessage} = require "./message" SlackClient = require "./client" pkg = require "../package" +Promise = require("bluebird"); class SlackBot extends Adapter @@ -82,10 +83,14 @@ class SlackBot extends Adapter # @param {...(string|Object)} messages - fully documented in SlackClient ### send: (envelope, messages...) -> - # TODO: if the sender is interested in the completion, the last item in `messages` will be a function - for message in messages + callback = -> + if typeof(messages[messages.length - 1]) == "function" + callback = messages.pop() + messagePromises = messages.map (message) => + return Promise.resolve() if typeof(message) is "function" # NOTE: perhaps do envelope manipulation here instead of in the client (separation of concerns) @client.send(envelope, message) unless message is "" + Promise.all(messagePromises).then(callback.bind(null, null), callback) ###* # Hubot is replying to a Slack message @@ -94,12 +99,16 @@ class SlackBot extends Adapter # @param {...(string|Object)} messages - fully documented in SlackClient ### reply: (envelope, messages...) -> - # TODO: if the sender is interested in the completion, the last item in `messages` will be a function - for message in messages + callback = -> + if typeof(messages[messages.length - 1]) == "function" + callback = messages.pop() + messagePromises = messages.map (message) => + return Promise.resolve() if typeof(message) is "function" if message isnt "" # TODO: channel prefix matching should be removed message = "<@#{envelope.user.id}>: #{message}" unless envelope.room[0] is "D" @client.send envelope, message + Promise.all(messagePromises).then(callback.bind(null, null), callback) ###* # Hubot is setting the Slack conversation topic diff --git a/test/bot.coffee b/test/bot.coffee index 8cbc8c52..8e688d60 100644 --- a/test/bot.coffee +++ b/test/bot.coffee @@ -112,6 +112,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) -> + @slackbot.send {room: @stubs.channel.id}, 'message', done + @stubs._sendCount.should.equal 1 + @stubs._msg.should.equal 'message' describe 'Client sending message', -> it 'Should append as_user = true', -> @@ -142,6 +146,11 @@ describe 'Reply to Messages', -> @stubs._sendCount.should.equal 1 @stubs._dmmsg.should.equal "message" + it 'Should call the callback', (done) -> + @slackbot.reply {user: @stubs.user, room: @stubs.channel.id}, 'message', done + @stubs._sendCount.should.equal 1 + @stubs._msg.should.equal "<@#{@stubs.user.id}>: message" + describe 'Setting the channel topic', -> it 'Should set the topic in channels', (done) ->