[英]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.