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 SlackBot::{send,reply} #540

Merged
merged 12 commits into from
Apr 24, 2020
17 changes: 13 additions & 4 deletions src/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{SlackTextMessage, ReactionMessage, PresenceMessage} = require "./message"
SlackClient = require "./client"
pkg = require "../package"
Promise = require("bluebird");

class SlackBot extends Adapter

Expand Down Expand Up @@ -80,10 +81,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'
rtlechow marked this conversation as resolved.
Show resolved Hide resolved
# 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
Expand All @@ -92,12 +97,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'
rtlechow marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
9 changes: 9 additions & 0 deletions test/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,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', ->
Expand Down Expand Up @@ -136,6 +140,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) ->
Expand Down