繁体   English   中英

无法通过Hubot将图像文件上传到松弛状态

[英]unable to upload an image file to slack through hubot

我正在尝试使用pageres包获取网页的图像,然后使用hubot将图像发布到松弛状态。 我能够获取图像,但是由于某种原因,我无法使用松弛上传API将其发布到松弛状态。 这是我的代码,您能告诉我什么地方可能出问题吗? (不是咖啡绒的问题)

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

机器人已连接到Slack,并从Slack接收消息,对其进行了很好的解析,并将图像返回到本地目标,但无法将其发布到Slack,日志中也没有错误。

[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

您可以使用curl命令,可以使用child_process调用该命令以在通道中上传文件。

curl -F文件=@dramacat.gif -F通道= C024BE91L,#general -F令牌= xxxx-xxxxxxxxx-xxxx https://slack.com/api/files.upload

看来您的opts变量中的formData属性应该略有不同,如下所示:

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"),
}

channel_id是您的闲置频道ID,您在访问该频道时可以在浏览器地址栏中看到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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