我正在尝试使用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
僵尸程序连接到松弛,从松弛接收消息,解析它们并将图像恢复到本地目的地,但不能将其发布到松弛,日志中也没有错误。
[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
答案 0 :(得分:0)
您可以使用curl命令,该命令可以通过child_process调用以在通道中上传文件。
curl -F文件=@dramacat.gif -F频道= C024BE91L,#general -F令牌= xxxx-xxxxxxxxx-xxxx https://slack.com/api/files.upload
答案 1 :(得分:0)
您的 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,访问该频道时,您可以在浏览器地址栏中看到它。