From 75e275e64f7b42445108853de5f410c0ff339951 Mon Sep 17 00:00:00 2001 From: Farid Neshat Date: Wed, 27 Sep 2017 00:12:43 +0200 Subject: [PATCH] Add callback support to SlackBot::{reply,send} --- package.json | 3 ++- src/bot.coffee | 10 ++++++++++ test/bot.coffee | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 304f4cca..bd69994a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/bot.coffee b/src/bot.coffee index f99096b1..ea730b63 100644 --- a/src/bot.coffee +++ b/src/bot.coffee @@ -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 @@ -126,10 +127,15 @@ 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) return sent_messages @@ -137,12 +143,16 @@ class SlackBot extends Adapter 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) return sent_messages diff --git a/test/bot.coffee b/test/bot.coffee index cf54e719..d6e7ea02 100644 --- a/test/bot.coffee +++ b/test/bot.coffee @@ -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', -> @@ -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'