2016-06-28 94 views
4

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)與主節點腳本一起工作。

任何和所有的幫助真的很感激。再次,只是尋找一些有關我失蹤或做錯的見解/指導。

在此先感謝!

+0

你能告訴我們什麼'bot.api'是什麼?我只是猜測基於變量名稱,但也許你正在使用[botkit](https://github.com/howdyai/botkit)? – smarx

+1

如果確實是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

+0

我正在使用botkit!我會試試看,謝謝你的建議! – dannyk

回答

7

由於Botkit似乎不支持發送multipart/form-data,所以看起來你不得不在Botkit的API之外。

這給一個嘗試,使用request直接(已在使用Botkit本身):

var request = require('request'); 

... 

request.post({ 
    url: 'https://slack.com/api/files.upload', 
    formData: { 
     token: bot.config.token, 
     title: "Image", 
     filename: "image.png", 
     filetype: "auto", 
     channels: filtered[0].id, 
     file: fs.createReadStream('test.png'), 
    }, 
}, function (err, response) { 
    console.log(JSON.parse(response.body)); 
}); 
+0

我無法感謝你的幫助。你的建議答案完美無瑕。非常感謝你,我真的很感激! – dannyk

+0

是否可以直接發送給用戶而不是頻道?我看到,文檔只討論發送到頻道 – dimirc

+0

FYI ...我剛剛獲得原始請求(使用bot.api.files.upload)來工作。看來在Botkit v0.4.1中有一個修復。 有關詳細信息,請參閱https://github.com/howdyai/botkit/commit/2bde51ff5326ccc44b8764ecc3110dac6696ef6c –

-1

我建議你使用nodejslack。 它使用承諾模式,由Bluebird供電。 有在其文檔上傳文件中的示例代碼,那就是:

var Slack = require('nodejslack'); 
    var fs = require('fs'); 
    var SLACK_TOKEN = process.env.SLACK_TOKEN || 'YOUR_GENERATED_SLACK_TOKEN'; 

    var slack = new Slack(SLACK_TOKEN); 

    var form = { 
     file: fs.createReadStream('test.csv'), // Optional, via multipart/form-data. If omitting this parameter, you MUST submit content 
     // content: 'Your text here', // Optional, File contents. If omitting this parameter, you must provide a `file` 
     filename: 'test.csv', // Required 
     fileType: 'post', // Optional, See more file types in https://api.slack.com/types/file#file_types 
     title: 'Title of your file!', // Optional 
     initial_comment: 'First comment about this file.', // Optional 
     channels: 'general' //Optional, If you want to put more than one channel, separate using comma, example: 'general,random' 
    }; 

    slack.fileUpload(form) 
    .then(function(response){ 

     // Slack sends a json with a boolean var ok. 
     // Error example : data = { ok: false, error: 'user_not_found'   } 
     // Error example : data = { ok: true, file: 'user_not_found' } 
     if(!response || !response.ok){ 
      return Promise.reject(new Error('Something wrong happened during the upload.')); 
     } 
     console.log('Uploaded Successfully:',response); 

     return Promise.resolve(response); 
    }) 
    .catch(function(err){ 
     return err; 
    });