これまでの一般的な方法
普通にググると大抵はこの channels.list
を使っているサンプルに行き着く。
https://api.slack.com/methods/channels.list
ただし、最近気づいた下記の文言、
Don't use this method. Use
conversations.list
instead.
他にも移行が必須なケースがあって、例えば channels.list
はShared channelsを返してくれない。ここは場合によっては結構困るのでconversations.list
へ移行した。
サンプル
conversations.list
ではpaginationを意識する必要があるが、node-slack-sdkのサンプルを参照すると下記のようになる。
https://github.com/slackapi/node-slack-sdk/blob/master/docs/_pages/web_client.md#pagination
const { WebClient } = require('@slack/client');
const token = process.env.SLACK_BOT_TOKEN;
const web = new WebClient(token);
function getChannels() {
const param = {
exclude_archived: true,
types: 'public_channel',
limit: 200
};
let channels = [];
function pageLoaded(res) {
// 今回はチャンネル名だけ欲しかったので "name" だけ。
res.channels.forEach(c => channels.push(c.name));
if (res.response_metadata && res.response_metadata.next_cursor && res.response_metadata.next_cursor !== '') {
param.cursor = res.response_metadata.next_cursor;
return web.conversations.list(param).then(pageLoaded);
}
return channels;
}
return web.conversations.list(param).then(pageLoaded);
}
getChannels()
.then((d) => console.log(JSON.stringify(d)))
.catch(console.error);
conversations.list
に渡すことのできるArgumentsはいくつかあるのでドキュメントを参照のこと。上記の例では、archive済みのチャンネルは省略、Publicチャンネルのみ、1ページ毎に200個まで、と指定している。limit: 200
の理由は、一応ドキュメントに下記のように記載があるため。
To begin pagination, specify a limit value under
1000
. We recommend no more than200
results at a time.
普通にwhileで回してもいいとは思う。
https://blog.frame.ai/migrating-to-the-slack-conversations-api-89692b016eea