2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

みなさんはSlackを利用していますか?
Slackって気づいたらめちゃくちゃチャンネルが乱立しているんですよね。。。
チャンネルの整理をしようと思っても、Slackは契約プランによってSlackが用意しているチャンネル管理ツールが使えない場合があります(というかビジネスプランとEnterprise Grid プランでないと使えない。。。)

ということで、自分が所属しているチャンネルのみという条件にはなりますが、チャンネル一覧を出力できるツールをNode.jsで作ってみました。

注意
プライベートチャンネルは自分が所属しているもののみ取得できます。
自分が入っていないプライベートチャンネルは取得できませんの注意してください。

それでは以下より、手順を説明します。

Slackアプリを作成

上記のApp管理画面より「Create an App」で新規アプリを作成する。

スクリーンショット 2024-06-14 17.35.01.png

「From an app manifest」を選ぶ。

スクリーンショット 2024-06-14 17.39.16.png

追加するワークスペースを選択する。

スクリーンショット 2024-06-14 17.40.12.png

必要に応じてname(アプリ名)を変更する。(後でも変更できます)
今回はDemo Appのまま進みます。

スクリーンショット 2024-06-14 17.41.23.png

確認。問題なければ「Create」をクリックしてAppを作成します。

スクリーンショット 2024-06-14 17.43.56.png

アプリ権限の付与

「Permissions」をクリックして権限設定ページを表示。

スクリーンショット 2024-06-14 17.45.23.png

ページを下にスクロールして、Scopesメニューを表示。
User Token Scopesの欄で、「Add an OAuth Scope」を追加する。追加する権限は以下の3つ。
users:read channels:read groups:read

スクリーンショット 2024-06-14 17.49.30.png

同ページの上の方へ戻り、「Install to Workspace」をクリックしてアプリをワークスペースに追加する。

スクリーンショット 2024-06-14 17.53.27.png

確認画面が表示されるので問題なければ「許可する」をクリック。

スクリーンショット 2024-06-14 17.54.37.png

アプリがワークスペースに追加されると、User OAuth Tokenが表示されるのでコピーする。

注意
User OAuth Tokenは外部に漏れないように管理してください。

スクリーンショット 2024-06-14 17.55.34.png

Node.jsアプリの作成

「.env」を作成して先ほどコピーしたUser OAuth Tokenを設定する

SLACK_USER_OAUTH_TOKEN=

以下の「index.js」を作成する

const { WebClient } = require("@slack/web-api");
const dayjs = require("dayjs");
const fs = require("node:fs");
const { Parser } = require("json2csv");

require("dotenv").config();

const client = new WebClient(process.env.SLACK_USER_OAUTH_TOKEN);

async function getMembers() {
  const result = await client.users.list({ limit: 1000 });
  const members = new Map();
  result.members.forEach(member => {
    members.set(member.id, { name: member.real_name || member.name });
  });
  return members;
}

async function getChannels() {
  const result = await client.conversations.list({
    exclude_archived: "true",
    types: "public_channel,private_channel",
    limit: 1000
  });

  return result.channels;
}

async function generateCsvReport() {
  const members = await getMembers();
  const channels = await getChannels();

  const data = channels.map(channel => {
    const creator = members.get(channel.creator);
    return {
      name: channel.name,
      purpose: channel.purpose?.value || "",
      connected: channel.is_ext_shared ? "" : "",
      private: channel.is_private ? "" : "",
      creator: creator?.name || "",
      created: dayjs.unix(channel.created).format("YYYY-MM-DD")
    };
  });

  const fields = ["name", "purpose", "connected", "private", "creator", "created"];
  const parser = new Parser({ fields, header: true });
  const csv = parser.parse(data);

  fs.writeFileSync("./channels.csv", csv, "UTF-8");
}

generateCsvReport().catch(console.error);

モジュールのインストール

npm install @slack/web-api dayjs json2csv dotenv

実行

node index.js

index.jsを実行した場所に channels.csv ができます。

以上です。

この記事は個人の見解であり所属する組織を代表しません。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?