Hubot-slack: Listening for image upload (file_created)

Created on 31 Jan 2017  路  6Comments  路  Source: slackapi/hubot-slack

As titled. Is this supported?

enhancement

Most helpful comment

I ran into the same issue where I couldn't find a way to listen to an image upload. The approach that finally worked for me was getting every raw output from hubot, and parse it to find which one is an image upload.

First, you have to add a listener for raw_messages under your bot src code:

node_modules/hubot-slack/src/bot.coffee

My listener was:

@client.on 'raw_message', @upload

Then, create the function _upload_ and check if the raw_message is indeed a picture

  upload: (msg) =>
    @robot.logger.info 'Raw message received'
    @robot.logger.info msg
    rawMsg = JSON.parse(msg)
    @robot.logger.info rawMsg.type
    @robot.logger.info rawMsg.subtype
    if (rawMsg.type == 'message') && (rawMsg.subtype == 'file_share') then @robot.emit 'upload', rawMsg.first_name

And from there you can just create a script that checks for upload, mine started like:

module.exports = (robot) ->
    robot.on 'upload', (msg) ->
        (code goes here)

Here's my code in case you would like to check it:

https://github.com/DavidIsrawi/JarvisHubot/blob/dc279d8fbd2868e058f9fbfefb566ddb7214ec9a/node_modules/hubot-slack/src/bot.coffee

All 6 comments

there is no support for the "file_created" RTM API event in this adapter.

can you describe your use case? i'd be open to hearing a proposal for an API. it would probably look something like Robot::react() that is implemented currently, because it goes beyond the standard Hubot interface and set of Message classes.

I ran into the same issue where I couldn't find a way to listen to an image upload. The approach that finally worked for me was getting every raw output from hubot, and parse it to find which one is an image upload.

First, you have to add a listener for raw_messages under your bot src code:

node_modules/hubot-slack/src/bot.coffee

My listener was:

@client.on 'raw_message', @upload

Then, create the function _upload_ and check if the raw_message is indeed a picture

  upload: (msg) =>
    @robot.logger.info 'Raw message received'
    @robot.logger.info msg
    rawMsg = JSON.parse(msg)
    @robot.logger.info rawMsg.type
    @robot.logger.info rawMsg.subtype
    if (rawMsg.type == 'message') && (rawMsg.subtype == 'file_share') then @robot.emit 'upload', rawMsg.first_name

And from there you can just create a script that checks for upload, mine started like:

module.exports = (robot) ->
    robot.on 'upload', (msg) ->
        (code goes here)

Here's my code in case you would like to check it:

https://github.com/DavidIsrawi/JarvisHubot/blob/dc279d8fbd2868e058f9fbfefb566ddb7214ec9a/node_modules/hubot-slack/src/bot.coffee

+1 I have a similar use case, to listen to and run code snippets that are posted.

@aoberoi is your preference still a custom extension in extensions.coffee? If so, I have a prototype I can clean up and push.

External interface would be:

robot.fileShared (res) ->

Where res would contain the file id of the shared file.

thanks for the PR, it looks really good! i left some feedback there for you. happy to help get that across the finish line 馃槃

Addressed with #546

Was this page helpful?
0 / 5 - 0 ratings