Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
notify "reaction_added" event for
# Description:
# notify "reaction_added" event for
# Author:
# @shokai <>
debug = require('debug')('hubot:slack-reaction')
_ = require 'lodash'
config =
room: 'reactions'
module.exports = (robot) ->
reactions =
prefix: "reaction"
get: (url) ->
robot.brain.get("#{@prefix}_#{url}") or []
set: (url, users) ->
return unless users instanceof Array
robot.brain.set "#{@prefix}_#{url}", users
add: (url, user) ->
users = @get url
users.push user
@set url, _.uniq(users)
remove: (url, user) ->
users = @get url
users.splice(users.indexOf(user), 1)
@set url, users
robot.adapter.client?.on? 'raw_message', (msg) ->
if msg.type isnt 'reaction_added' and
msg.type isnt 'reaction_removed'
debug msg
return if msg.item.type isnt 'message'
return unless channel = robot.adapter.client.getChannelByID
return unless user = robot.adapter.client.getUserByID msg.user
url = "https://#{}{}/p#{msg.item.ts.replace('.','')}"
switch msg.type
when 'reaction_added'
reactions.add url,
users = reactions.get url
text = [0...users.length].map(-> ":#{msg.reaction}:").join ''
_users =
if users.length is 1
"#{} and #{users.filter((i) -> i isnt ', '}"
text += " #{url} by #{_users}"
debug text
robot.send {room:}, text
when 'reaction_removed'
reactions.remove url,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment