简体   繁体   中英

unable to upload an image file to slack through hubot

I am trying to get an image of a web page using pageres package, And post the image to slack using hubot. I am able to get the image, but for some reason i am not able to post it to slack using slack upload api. Here is my code, can you tell me what could be wrong? (not a coffee lint issue)

fs = require("fs")
Pageres = require('pageres') 
util = require("util")
request = require("request")
module.exports = (robot) ->
  robot.respond /screenshot page (\S*)?( at )?(\S*)?/i, (msg) ->
    pageres = new Pageres({delay: 30})
    domain = msg.match[1].replace("http://", "")
    if msg.match[3] == undefined
      size = '960x1024'
    else
      size = msg.match[3]
    dest = './screenshots'
    msg.send "Acquiring screenshot of #{domain}"
    pageres.src(domain, [size]).dest(dest)
    pageres.run (err) ->
      if err
    robot.logger.error err
       msg.send "Um..., you better check the log"
    else
    opts = {
      method: 'POST',
      uri: 'https://slack.com/api/files.upload',
      formData: {
        channels: process.env.HUBOT_SCREENSHOT_SLACK_CHANNEL,
        initial_comment: "Screenshot of #{domain}",
        token: process.env.HUBOT_SLACK_TOKEN,
        file: fs.createReadStream("#{dest}/#{domain}.png")
      }
    }

    request.post opts, (error, response, body) ->
      if error
        robot.logger.error error
      else
        robot.logger.debug 'screenshot posted to slack'
  return

The bot is connected to slack, and receiving messages from slack, parsing them fine and getting the image back to the local destination, but not able to post it to slack, There are no errors as well in the log.

[Wed Apr 11 2018 16:16:47 GMT+0000 (UTC)] DEBUG Received message: '@hubot screenshot page http://www.google.com' in channel: ****, from: ******
[Wed Apr 11 2018 16:16:47 GMT+0000 (UTC)] DEBUG Message '@hubot screenshot page http://www.google.com' matched regex //^\s*[@]?hubot[:,]?\s*(?:screenshot page (\S*)?( at )?(\S*)?)/i/; listener.options = { id: null }
[Wed Apr 11 2018 16:16:47 GMT+0000 (UTC)] DEBUG Executing listener callback for Message '@hubot screenshot page http://www.google.com'
[Wed Apr 11 2018 16:16:47 GMT+0000 (UTC)] DEBUG Sending to *****: Acquiring screenshot of www.google.com

You can use curl command which can be called using child_process to upload a file in the channel.

curl -F file=@dramacat.gif -F channels=C024BE91L,#general -F token=xxxx-xxxxxxxxx-xxxx https://slack.com/api/files.upload

It seems the formData property in your opts variable should be slightly different like this:

formData: {
    token: process.env.HUBOT_SLACK_TOKEN,
    title: "Screenshot of #{domain}",
    filename: "image.png",
    filetype: "auto",
    channels: channel_id,
    file: fs.createReadStream("path_to_your_image"),
}

The channel_id is your slack channel id which you can see in the browser address bar when you access the channel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM