LoginSignup
0
0

More than 5 years have passed since last update.

Slackbotのアプリでファイル受け取りができなくなった時の対応 [botkit使用時]

Last updated at Posted at 2018-08-14

内容

Slackでファイル受け取りのアプリケーションを動かしていたが、突然ファイル受け取りが使えなくなってしまった。
その時したこと。

SlackAPIの「file_share」が使えなくなった

いままでNode.jsのBotKitフレームワークを使ってSlackのBot開発をしていたが、
2018年7月23日より、ファイル取得のAPI(file_share)が廃止されてしまって、アプリが動かなくなる事象が発生してしまった。Slack Recent updates

Events API changes
Some events in both the Events API and RTM APIs will be discontinued; they are going away because what they describe is going away.

For best results, track our supported top-level events like file_shared and file_created instead of "message subtypes" like file_share.

If you're used to file_share and use the Events API, you'll be happy to know we'll continue dispatching this message subtype until further notice. It will no longer be served over RTM.

These retained events, however, are still different in a single way: instead of coming with a file attribute, you'll find a files array containing one or more leaner file objects inside.

※まじか。。。

アプリの簡単な構成

Slackからbotへ写真を送ると処理を行い、メッセージを返却してくれるアプリケーション

修正対応

file_shareメソッドからfile_sharedメソッドへ

まずは、記述の通り、「file_share」→「file_shared」へ変更を試してみる。

イベント取得

// modules
var Botkit = require('botkit');
var controller = Botkit.slackbot();


//controller.on('file_share', function (bot, message) { ←廃止
controller.on('file_shared', function (bot, message) {
   // 処理
});

これでとりあえず、イベントは取得できるようになった。

ファイルURLの取得

file_share時代は、そこからurlのリクエストを取得すればいいだけなのだが、file_sharedさんはそうさせてくれない。
そのかわり、ファイルID(file_id)を返してくれるので、ファイルIDから「files.info」APIを叩いて、urlをゲットする必要があるので、そのメソッドを作成した。

file_share時代
// いままではレスポンスの「message」にファイルのurl情報もふくまれていたのだが
content = yield download(message.file.url_private_download);

file_sharedから
// ファイルIDを引数にもう一度別の(files.info)APIを投げる必要がでてきた。
var url = yield getUrl(bot, message.file_id);
message.channel = channel
//リプライするチャンネル情報をmassageへ格納
content = yield download(url);

...

/**
 * Slackファイルアクション時、ファイルURLを取得し返却します。
 * ※このメソッドでチャンネルを識別してchannelへ値を格納している。
 * @param {object} bot 
 * @param {string} file_id 
 */
function getUrl(bot, file_id) {
    return new Promise(function (resolve, reject) {
        const messageObj = {
            token: FILE_ACCESS_TOKEN,
            file: file_id
        };
        bot.api.files.info(messageObj, function (err, res) {
            if (err) {
                console.log(err);
                reject(err);
            }
            var url = res.file.url_private_download
            res.file.ims.length >= 1 ? channel = res.file.ims[0] : channel = res.file.groups[0] //チャンネルをimsかgroupか判定
            resolve(url);
        });
    });
}


※チャンネルを変数に格納しているのは、file_sharedさんが、チャンネルを認識できない為、ここでどのチャンネルから来た写真なのかを判断する必要があるため。

ポイントは2点

- file_sharedの場合、チャンネル情報が取得されないので、リプライする場合チャンネル情報を付与する必要がある

- files.infoでファイルIDからファイルURLを取得する必要がある。

0
0
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
0
0