Skip to content

Instantly share code, notes, and snippets.

@shokai
Last active April 16, 2022 15:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shokai/d23607d91ea7885a8df7 to your computer and use it in GitHub Desktop.
Save shokai/d23607d91ea7885a8df7 to your computer and use it in GitHub Desktop.
notify "reaction_added" event for slack.com
# Description:
# notify "reaction_added" event for slack.com
# http://shokai.org/blog/archives/10344
#
# Author:
# @shokai <hashimoto@shokai.org>
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'
return
debug msg
return if msg.item.type isnt 'message'
return unless channel = robot.adapter.client.getChannelByID msg.item.channel
return unless user = robot.adapter.client.getUserByID msg.user
url = "https://#{robot.adapter.client.team.domain}.slack.com/archives/#{channel.name}/p#{msg.item.ts.replace('.','')}"
switch msg.type
when 'reaction_added'
reactions.add url, user.name
users = reactions.get url
text = [0...users.length].map(-> ":#{msg.reaction}:").join ''
_users =
if users.length is 1
user.name
else
"#{user.name} and #{users.filter((i) -> i isnt user.name).join ', '}"
text += " #{url} by #{_users}"
debug text
robot.send {room: config.room}, text
when 'reaction_removed'
reactions.remove url, user.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment