EDITED鬆弛API(files.upload)使用的NodeJS
我試圖通過結構鬆弛所提供的files.upload() API但我有一個很難理解的正確格式。目前,我可以使用API上傳文本文件,但無法爲我的生活弄清楚如何上傳圖像。
這是我的問題:我的開發服務器上有一張圖片,我們稱之爲image.png。我想使用files.upload()API將該圖像發佈到#general Slack頻道。下面是我已成功生成圖像的代碼,但目前只是發送文本:
var myBarChart = new Chart(ctx).Bar(barChartData, barChartOptions);
var myBarChartDataURL = leaderboardBarChart.toBase64Image();
canvas.toBuffer(function(err, buf) {
if (err) throw err;
fs.writeFile(__dirname + "/leaderboard.png", buf);
});
bot.api.files.upload({
token: process.env.token,
title: "Image",
filename: "image.png",
filetype: "auto",
//content: "Posted with files.upload API",
file: fs.createReadStream("path/to/image_file.png"),
channels: filtered[0].id
}, function(err, response) {
if (err) {
console.log("Error (files.upload) " + err);
} else {
console.log("Success (files.upload) " + response);
};
});
當我運行代碼,我得到了下面的錯誤之一:
「invalid_array_arg」這斯萊克詳細信息爲:「該方法已通過PHP風格的數組參數(例如,名稱爲foo [7]),這些從未在Slack API中生效。」
我不完全確定該如何做出這個錯誤,因爲我沒有使用PHP,也沒有任何可以識別出類似於PHP的東西。我使用了幾種不同的方法來包含文件路徑,無論是使用'fs'模塊,將它存儲在一個變量中,還是僅僅引用它的絕對路徑(甚至相對路徑)。我有點失落,只是尋找一些指導。
我知道這個特定的API使用multipart/form-data,但是我沒有表單。這個應用程序嚴格是一個NodeJS應用程序。沒有框架(如Express)與主節點腳本一起工作。
任何和所有的幫助真的很感激。再次,只是尋找一些有關我失蹤或做錯的見解/指導。
在此先感謝!
你能告訴我們什麼'bot.api'是什麼?我只是猜測基於變量名稱,但也許你正在使用[botkit](https://github.com/howdyai/botkit)? – smarx
如果確實是botkit,我認爲'file:fs.createReadStream(「path/to/image/file」)'可能會訣竅。 (Botkit使用[request](https://github.com/request/request),它通過[form-data]處理'multipart/form-data'(https://github.com/form-data/form-數據))。 – smarx
我正在使用botkit!我會試試看,謝謝你的建議! – dannyk