Open6
Claude CodeのHooksでSlack通知を入れる
What
Claude Codeのhooksを使ってSlackでの通知を入れたい。
Reference
通知のタイミング
以下の4種類。
- PreToolUse
- PostToolUse
- Notification
- Stop
今回はNotification
を使う。
Notification
以下のinputが与えられる。
{
"session_id": "abc123",
"transcript_path": "~/.claude/projects/.../00893aaf-19fa-41d2-8238-13269b9b3ca0.jsonl",
"message": "Task completed successfully",
"title": "Claude Code"
}
設定は以下のようになる。
{
"hooks": {
"EventName": [
{
"matcher": "ToolPattern",
"hooks": [
{
"type": "command",
"command": "your-command-here"
}
]
}
]
}
}
Pythonコードの作成
PythonでSlackのincoming Webhookを呼ぶようにする。
以下のようにClaude Codeで実装。
設定
Claude Code
で/hooks
としてinteractiveに設定するか、直接~/.claude/settings.json
を修正する。
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "python3 /Users/hosaka/.claude/hooks/slack_notifier.py"
}
]
}
],
}
テスト
以下のようにツールが来る。5秒くらいラグがあるような気がする。即時ではない。