LoginSignup
4
6

More than 5 years have passed since last update.

Botkitで画像ファイルをslackにアップロードする

Last updated at Posted at 2016-11-27

botkitでのファイルのアップロードには
bot.api.files.upload を用いる。

fs.readFile('hoge.png', function(err,data){
  if(err) throw err;

  bot.api.files.upload({
                    content: data,
                    filename: 'hoge.png',
                    channels: message.channel
                },function(err,res) {
                    if (err) console.log(err)
                })
});

ただし上記の方法で画像ファイルをアップロードしても
plain text としてアップロードされてしまう。

画像ファイルでは以下のように content ではなく file にstream を
渡してあげる必要がある。

bot.api.files.upload({
                    file: fs.createReadStream('hoge.png'),
                    filename: 'hoge.png',
                    channels: message.channel
                },function(err,res) {
                    if (err) console.log(err)
                })

下記コミットにて修正された模様。
https://github.com/howdyai/botkit/commit/2bde51ff5326ccc44b8764ecc3110dac6696ef6c

contentにデータを設定すると通常のformとしてapiを叩くが、
fileにデータを設定するとMultipart form としてapiを叩くようにしているよう。

参考

SlackのBotkitでファイルをアップロードする方法

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6