Hubot-slack: Listening for reaction messages

Created on 11 Nov 2015  Â·  16Comments  Â·  Source: slackapi/hubot-slack

Currently, hubot-slack doesn't support listening for reaction messages, even using SlackRawMessage. Such messages are only emitted from the raw_messages event in the node-slack-client module, which hubot-slack doesn't subscribe to.

It seems like we might want to refactor SlackRawMessage to the use node-slack-client's raw_message event. I'd be interested in opening a pull request if this sounds like a good idea.

Most helpful comment

I discovered a simple workaround. First, register a listener on the node-slack-client raw_message event in hubot-slack/src/slack.coffee, probably around line 31:

# hubot-slack/src/slack.coffee

# Setup event handlers
@client.on 'userChange', @.userChange
@robot.brain.on 'loaded', @.brainLoaded

# add this line
@client.on 'raw_message', @.reaction

Then define an event handler. E.g., above line 103:

# hubot-slack/src/slack.coffee

# Handler for reaction event
reaction: (msg) => 
  # if message is a reaction
  if msg.type == "reaction_removed" or msg.type == "reaction_added"
    # get channel and user data
    user = @robot.brain.userForId msg.user
    channel = @client.getChannelGroupOrDMByID msg.item.channel if msg.item.channel
    # populate event data
    reaction = message: msg, user: user, channel: channel
    @robot.emit 'reaction', reaction

message: (msg) =>
  # etc...

Then to listen for the event, use robot.on:

# my_bot/scripts/example.coffee

module.exports = (robot) ->
  robot.on 'reaction', (reaction) -> 
    envelope = room: reaction.channel.id
    robot.send envelope, "Got your reaction message. :)"

All 16 comments

I discovered a simple workaround. First, register a listener on the node-slack-client raw_message event in hubot-slack/src/slack.coffee, probably around line 31:

# hubot-slack/src/slack.coffee

# Setup event handlers
@client.on 'userChange', @.userChange
@robot.brain.on 'loaded', @.brainLoaded

# add this line
@client.on 'raw_message', @.reaction

Then define an event handler. E.g., above line 103:

# hubot-slack/src/slack.coffee

# Handler for reaction event
reaction: (msg) => 
  # if message is a reaction
  if msg.type == "reaction_removed" or msg.type == "reaction_added"
    # get channel and user data
    user = @robot.brain.userForId msg.user
    channel = @client.getChannelGroupOrDMByID msg.item.channel if msg.item.channel
    # populate event data
    reaction = message: msg, user: user, channel: channel
    @robot.emit 'reaction', reaction

message: (msg) =>
  # etc...

Then to listen for the event, use robot.on:

# my_bot/scripts/example.coffee

module.exports = (robot) ->
  robot.on 'reaction', (reaction) -> 
    envelope = room: reaction.channel.id
    robot.send envelope, "Got your reaction message. :)"

I'm usually not a +1 guy, but here I am. I had a similar need, but with _channel_create_. I wanted to be able to catch that event and fire a DM to the creator telling them certain "housekeeping" rules of the instance. Without hooking raw_message this wasn't possible that I could see.

I piggy backed on @ctbailey approach but looked at it as a way to stub out the other msg.types that aren't being caught. Thinks like _channel_, reaction, and group__. So instead of just handing the reaction I decided to just create a rawMessage function and switch the types.

My approach, which includes a handler for the _channel_create_ event, can be found below. Other events could be added to the switch for use.

https://github.com/ehammersley/hubot-slack/commit/d2b3224cad66acd70ed37fec0aaef1b44f6b824f

This would give a way of capturing those events without having to modify the emits in node-slack-client. If it's something others are interested in I could submit a pull. My skills are not good enough to make that call on my own.

If anyone reading this would like to implement something similar you could leverage the above (https://github.com/ehammersley/hubot-slack/commit/d2b3224cad66acd70ed37fec0aaef1b44f6b824f) like so. This would send the creator of that channel a private message.

module.exports = (robot) ->
  robot.on 'channelCreated', (msg) ->
    envelope = room: msg.user.name
    robot.send envelope, "You just created a new channel."

Thanks to @ctbailey for the inspiration.

yah, i'm +1 too. i can see the reason for not having it since it will be slack specific (not interoperable with other hubot adapters), but for things like presence, it's really useful.

not sure if my problem is the same, but it seems hubot is not listening to what other "Bots" are saying.

We have CircleCI posting build success/failures in a CI/CD room and I have it listening for "Failed:", but it doesn't work anymore now that I moved from Hipchat to Slack...

screen shot 2016-01-28 at 4 52 16 pm

hubot-slack treats messages from a bot (msg.subtype = bot_message) differently than normal user generated messages. See the code below for where this is filtered out.

See here: https://github.com/slackhq/hubot-slack/blob/master/src/slack.coffee#L136

Bot messages are now squirreled away into a SlackBotMessage sub-class. There's also a listener for this, SlackBotListener, that can be leveraged in scripts to capture these types of messages. Normal .hear or .listen will not see bot_messages.

See this PR for details on the change, and discussion around it.

https://github.com/slackhq/hubot-slack/pull/133

thanks @ehammersley. I think some people could benefit from an example on how to implement it: https://github.com/slackhq/hubot-slack/issues/198#issuecomment-141161190

this is the only snippet I can find, and I'm not really sure how it works: https://github.com/gregmajor/tom-servo/blob/master/scripts/migration-tracker.coffee#L130

Any advice?

I've been unable to make that code work, and the fact that code's commented suggests it might not.

Also tried the code listed in #198 which appears to have been copied from a site written in a language I can't read, no joy there either.

Has anyone actually managed to get bot-on-bot listening to work and can document?

@piersb you have tried https://github.com/slackhq/hubot-slack/issues/198#issuecomment-185005987 and it didn't work? I am using almost exactly that and it responds to my CircleCI bot messages

HubotSlack = require 'hubot-slack'

module.exports = (robot) ->

    regex = /closes?.*#(\d*)/i

    robot.hear regex, (res) ->
        issueNumber = res.match[1]
        res.send "Close Issue signal detected; I think you're trying to close issue \##{issueNumber}"

    robot.listeners.push new HubotSlack.SlackBotListener robot, regex, (msg) ->
        msg.send "Close Issue signal detected; I think you're trying to close an issue."

Listening to people works; I can even cut and paste what the bot says and it'll fire. But it won't fire for the bot.

what about without the robot.hear block?

On Mar 15, 2016, at 11:59 AM, Piers Beckley [email protected] wrote:

`HubotSlack = require 'hubot-slack'

module.exports = (robot) ->

regex = /closes?._#(\d_)/i

robot.hear regex, (res) ->
issueNumber = res.match[1]
res.send "Close Issue signal detected; I think you're trying to close issue ##{issueNumber}"

robot.listeners.push new HubotSlack.SlackBotListener robot, regex, (msg) ->
msg.send "Close Issue signal detected; I think you're trying to close an issue."`
Listening to people works; I can even cut and paste what the bot says and it'll fire. But it won't fire for the bot.

—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
https://github.com/slackhq/hubot-slack/issues/251#issuecomment-196922598 https://github.com/slackhq/hubot-slack/issues/251#issuecomment-196922598

Export it separately? Will give it a pop.

No joy. :( Got to run now, will cut-and-paste code here when I get a chance.

This is a great idea, and I'll be looking into implementing this in the near future.

FYI: ReactionMessage is now available in v4.1.0 via #360.

Yup!! :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriel403 picture gabriel403  Â·  10Comments

jplindquist picture jplindquist  Â·  3Comments

DonEmil picture DonEmil  Â·  9Comments

tigris picture tigris  Â·  4Comments

rb1980 picture rb1980  Â·  6Comments