Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit 5f4592c

Browse files
author
Don Goodman-Wilson
authored
Merge pull request #360 from mbland/reaction-added-v3
Handle reaction_added messages
2 parents 4ab2c63 + 533682b commit 5f4592c

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

src/bot.coffee

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, Message, CatchAllMessage} = require.main.require 'hubot'
22

33
SlackClient = require './client'
4+
ReactionMessage = require './reaction-message'
45

56
class SlackBot extends Adapter
67

@@ -20,6 +21,8 @@ class SlackBot extends Adapter
2021
@client.on 'close', @close
2122
@client.on 'error', @error
2223
@client.on 'message', @message
24+
@client.on 'reaction_added', @reaction
25+
@client.on 'reaction_removed', @reaction
2326
@client.on 'authenticated', @authenticated
2427

2528
# Start logging in
@@ -157,6 +160,16 @@ class SlackBot extends Adapter
157160
message.user = user
158161
@receive new CatchAllMessage(message)
159162

163+
###
164+
Reaction added/removed event received from Slack
165+
###
166+
reaction: (message) =>
167+
{type, user, reaction, item_user, item, event_ts} = message
168+
return if (user == @self.id) || (user == @self.bot_id) #Ignore anything we sent
160169

170+
user = @client.rtm.dataStore.getUserById(user)
171+
user.room = item.channel
172+
item_user = @client.rtm.dataStore.getUserById(item_user)
173+
@receive new ReactionMessage(type, user, reaction, item_user, item, event_ts)
161174

162-
module.exports = SlackBot
175+
module.exports = SlackBot

src/reaction-message.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{Message} = require.main.require 'hubot'
2+
3+
class ReactionMessage extends Message
4+
# Represents a message generated by an emoji reaction event
5+
#
6+
# type - A String indicating 'reaction_added' or 'reaction_removed'
7+
# user - A User instance that reacted to the item.
8+
# reaction - A String identifying the emoji reaction.
9+
# item_user - A String indicating the user that posted the item.
10+
# item - An Object identifying the target message, file, or comment item.
11+
# event_ts - A String of the reaction event timestamp.
12+
constructor: (@type, @user, @reaction, @item_user, @item, @event_ts) ->
13+
super @user
14+
@type = @type.replace('reaction_', '')
15+
16+
module.exports = ReactionMessage

test/bot.coffee

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
should = require 'should'
22
{Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, Message, CatchAllMessage} = require.main.require 'hubot'
3+
ReactionMessage = require '../src/reaction-message'
34

45
describe 'Adapter', ->
56
it 'Should initialize with a robot', ->
@@ -144,6 +145,36 @@ describe 'Handling incoming messages', ->
144145
should.equal (@stubs._received instanceof TopicMessage), true
145146
@stubs._received.user.id.should.equal @stubs.user.id
146147

148+
it 'Should handle reaction_added events as envisioned', ->
149+
reactionMessage = {
150+
type: 'reaction_added', user: @stubs.user.id, item_user: @stubs.self.id
151+
item: { type: 'message', channel: @stubs.channel.id, ts: '1360782804.083113'
152+
},
153+
reaction: 'thumbsup', event_ts: '1360782804.083113'
154+
}
155+
@slackbot.reaction reactionMessage
156+
should.equal (@stubs._received instanceof ReactionMessage), true
157+
should.equal @stubs._received.user.id, @stubs.user.id
158+
should.equal @stubs._received.user.room, @stubs.channel.id
159+
should.equal @stubs._received.item_user.id, @stubs.self.id
160+
should.equal @stubs._received.type, 'added'
161+
should.equal @stubs._received.reaction, 'thumbsup'
162+
163+
it 'Should handle reaction_removed events as envisioned', ->
164+
reactionMessage = {
165+
type: 'reaction_removed', user: @stubs.user.id, item_user: @stubs.self.id
166+
item: { type: 'message', channel: @stubs.channel.id, ts: '1360782804.083113'
167+
},
168+
reaction: 'thumbsup', event_ts: '1360782804.083113'
169+
}
170+
@slackbot.reaction reactionMessage
171+
should.equal (@stubs._received instanceof ReactionMessage), true
172+
should.equal @stubs._received.user.id, @stubs.user.id
173+
should.equal @stubs._received.user.room, @stubs.channel.id
174+
should.equal @stubs._received.item_user.id, @stubs.self.id
175+
should.equal @stubs._received.type, 'removed'
176+
should.equal @stubs._received.reaction, 'thumbsup'
177+
147178
it 'Should handle unknown events as catchalls', ->
148179
@slackbot.message {subtype: 'hidey_ho', user: @stubs.user, channel: @stubs.channel}
149180
should.equal (@stubs._received instanceof CatchAllMessage), true
@@ -159,3 +190,13 @@ describe 'Handling incoming messages', ->
159190
it 'Should ignore messages it sent itself, if sent as a botuser', ->
160191
@slackbot.message { subtype: 'bot_message', bot: @stubs.self_bot, channel: @stubs.channel, text: 'Ignore me' }
161192
should.equal @stubs._received, undefined
193+
194+
it 'Should ignore reaction events that it generated itself', ->
195+
reactionMessage = { type: 'reaction_removed', user: @stubs.self.id, reaction: 'thumbsup', event_ts: '1360782804.083113' }
196+
@slackbot.reaction reactionMessage
197+
should.equal @stubs._received, undefined
198+
199+
it 'Should ignore reaction events that it generated itself as a botuser', ->
200+
reactionMessage = { type: 'reaction_added', user: @stubs.self_bot.id, reaction: 'thumbsup', event_ts: '1360782804.083113' }
201+
@slackbot.reaction reactionMessage
202+
should.equal @stubs._received, undefined

test/stubs.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ beforeEach ->
108108
sendMessage: (msg, room) =>
109109
@stubs.send room, msg
110110
dataStore:
111+
getUserById: (id) =>
112+
switch id
113+
when @stubs.user.id then @stubs.user
114+
when @stubs.bot.id then @stubs.bot
115+
when @stubs.self.id then @stubs.self
116+
when @stubs.self_bot.id then @stubs.self_bot
117+
else undefined
111118
getChannelByName: (name) =>
112119
switch name
113120
when 'known_room' then {id: 'C00000004'}

0 commit comments

Comments
 (0)