Slack チャンネル自動作成 + 招待
:LiTarget: 用途
Slack API でチャンネル新規作成 → ユーザー招待 → 初期メッセージ投稿。案件発生時の自動化。
:LiSparkle: 特徴
- conversations.create
- conversations.invite(複数ID対応)
- 初期投稿
- 命名規則パターン
:LiCode: コード(コピペ用)
// Slack チャンネル自動作成 + 招待
async function createSlackChannel(opts: {
token: string;
name: string; // 例: "case-2026-001-acme"
userIds: string[]; // 招待する Slack ユーザーID
initialMessage?: string;
}): Promise<string> {
// 1. チャンネル作成
const cr = await fetch('https://slack.com/api/conversations.create', {
method: 'POST',
headers: { Authorization: `Bearer ${opts.token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ name: opts.name.toLowerCase().replace(/[^a-z0-9-]/g, '-') }),
}).then(r => r.json());
if (!cr.ok) throw new Error('Channel create failed: ' + cr.error);
const channelId = cr.channel.id;
// 2. 招待
if (opts.userIds.length > 0) {
await fetch('https://slack.com/api/conversations.invite', {
method: 'POST',
headers: { Authorization: `Bearer ${opts.token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ channel: channelId, users: opts.userIds.join(',') }),
});
}
// 3. 初期メッセージ
if (opts.initialMessage) {
await fetch('https://slack.com/api/chat.postMessage', {
method: 'POST',
headers: { Authorization: `Bearer ${opts.token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ channel: channelId, text: opts.initialMessage }),
});
}
return channelId;
}
// 使い方:
// const channelId = await createSlackChannel({
// token: env.SLACK_BOT_TOKEN,
// name: `case-${caseId}-${clientName}`,
// userIds: [pmId, fsId, ceoId],
// initialMessage: `📋 案件「${clientName}」始動。詳細: ${url}`,
// });
:LiHandPointer: 使い方
対象プロジェクトに該当ファイルをコピーして、props を流し込むだけ。
:LiAlertCircle: 注意事項
- 依存パッケージを忘れず追加