💬 hippo: These are my real-world notes from setting up Claude Code Hooks. Getting from “it works” to “it’s actually reliable” involved plenty of pitfalls—hopefully this saves you from hitting the same ones.


1. Why Automate Notifications?

Sound familiar?

  • You kick off a long-running task in Claude Code (say, rewriting 28 blog posts)
  • You step away to do something else…
  • When you check back, the job finished ages ago—you just never knew

Wouldn’t it be great if it notified you automatically?

That is exactly what Hooks are for.


2. Final Result

Desktop Notification

Desktop notification preview

Displays a summary of the task and the total tool call count.

Feishu Notification

Feishu interactive card notification preview

Formatted as an interactive card showing:

  • Completion timestamp
  • Tool call count
  • Task summary

3. Implementation

3.1 Create the Notification Scripts

Desktop notification script .claude/hooks/notify.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# Claude Code desktop notification Hook

INPUT=$(cat)
TRANSCRIPT_PATH=$(echo "$INPUT" | jq -r '.transcript_path // ""')

# Extract the last user message from the transcript
get_last_user_message() {
if [ -f "$TRANSCRIPT_PATH" ]; then
grep '"type":"user"' "$TRANSCRIPT_PATH" 2>/dev/null | \
jq -c 'select(.message.content[0].tool_use_id == null) | select(.message.content[0].text | test("^<ide|system-reminder") | not)' 2>/dev/null | \
tail -1 | \
jq -r '.message.content[0].text' 2>/dev/null | \
cut -c1-80
fi
}

# Count tool invocations
get_tool_count() {
if [ -f "$TRANSCRIPT_PATH" ]; then
grep -c '"type":"tool_use"' "$TRANSCRIPT_PATH" 2>/dev/null || echo "0"
else
echo "0"
fi
}

LAST_MESSAGE=$(get_last_user_message)
if [ -z "$LAST_MESSAGE" ] || [ "$LAST_MESSAGE" = "null" ]; then
LAST_MESSAGE="Task complete"
fi

TOOL_COUNT=$(get_tool_count)

# macOS notification
if command -v osascript &> /dev/null; then
osascript -e "display notification \"$LAST_MESSAGE\nTool calls: $TOOL_COUNT\" with title \"✅ Claude Code Complete\"" 2>/dev/null
fi

# Linux notification (notify-send)
if command -v notify-send &> /dev/null; then
notify-send "✅ Claude Code Complete" "$LAST_MESSAGE\nTool calls: $TOOL_COUNT" 2>/dev/null
fi

exit 0

Feishu notification script .claude/hooks/feishu-notify.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# Claude Code Feishu notification Hook

INPUT=$(cat)
TRANSCRIPT_PATH=$(echo "$INPUT" | jq -r '.transcript_path // ""')

# Read webhook from environment variable (avoid hardcoding in the repo)
WEBHOOK_URL="${FEISHU_WEBHOOK_URL:-}"
[ -z "$WEBHOOK_URL" ] && exit 0

# Extract user message and tool call count (reusing functions from notify.sh)
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
LAST_MESSAGE="${LAST_MESSAGE:-Task complete}"
TOOL_COUNT="${TOOL_COUNT:-0}"

# Build JSON with jq to avoid request failures when messages contain quotes or newlines
PAYLOAD=$(jq -n \
--arg ts "$TIMESTAMP" \
--arg count "$TOOL_COUNT" \
--arg msg "$LAST_MESSAGE" \
'{
msg_type: "interactive",
card: {
config: { wide_screen_mode: true },
header: {
title: { tag: "plain_text", content: "✅ Claude Code Task Complete" },
template: "green"
},
elements: [
{
tag: "div",
fields: [
{ is_short: true, text: { tag: "lark_md", content: ("**Time**\n" + $ts) } },
{ is_short: true, text: { tag: "lark_md", content: ("**Tool Calls**\n" + $count + " calls") } }
]
},
{
tag: "div",
text: { tag: "lark_md", content: ("**Task**\n" + $msg) }
}
]
}
}')

curl -s -X POST "$WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" > /dev/null 2>&1

exit 0

3.2 Configure the Hooks

Add the following to .claude/settings.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"hooks": {
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/notify.sh"
},
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/feishu-notify.sh"
}
]
}
]
}
}

3.3 Get a Feishu Webhook URL

  1. Open your Feishu group chat and click Settings (top right) → Bots
  2. Click Add Bot → select Custom Bot
  3. Copy the generated Webhook URL

4. Pitfalls Encountered

Pitfall 1: Stop vs SessionEnd

I started with SessionEnd, which caused an unexpected problem:

  • Notifications only fired when the session ended (on exit or /clear)
  • Not after each completed task

The fix: Use the Stop event, which fires whenever Claude finishes a response.

Event Trigger Timing Use Case
Stop When Claude finishes responding ✅ Task completion notifications
SessionEnd When the session exits Cleanup tasks (1.5s timeout limit)

Pitfall 2: The Stop Event Has No reason Field

The official docs show a reason field on SessionEnd, but Stop does not include one.

At first I tried reading reason from the Stop payload and kept getting unknown.

The fix: The actual Stop event payload looks like this:

1
2
3
4
5
6
{
"hook_event_name": "Stop",
"session_id": "xxx",
"transcript_path": "/path/to/transcript.jsonl",
"stop_hook_active": false
}

Extract the task summary from transcript_path instead of looking for reason.

Pitfall 3: type: "user" Entries Aren’t All User Prompts

Records with type: "user" in the transcript JSONL include:

  • Actual user prompts
  • Tool outputs (tool_result)
  • IDE-injected messages (such as <ide_opened_file>)

The fix: Filter out non-prompt entries:

1
2
grep '"type":"user"' "$TRANSCRIPT_PATH" | \
jq -c 'select(.message.content[0].tool_use_id == null) | select(.message.content[0].text | test("^<ide|system-reminder") | not)'

Pitfall 4: Feishu Interactive Card Schema

Feishu custom bots support two message formats:

  • text: plain text—simple, but bare-bones
  • interactive: rich card—clean layout, but stricter schema

My first card payload failed to deliver because fields inside elements must be an array of objects.

Correct schema:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"msg_type": "interactive",
"card": {
"header": { ... },
"elements": [
{
"tag": "div",
"fields": [
{ "is_short": true, "text": { "tag": "lark_md", "content": "..." } }
]
}
]
}
}

Pitfall 5: Hook Changes Require a Session Restart

This is an intentional security mechanism in Claude Code:

  • A snapshot of your Hook config is captured at session startup
  • The running session executes against that snapshot
  • External edits do not take effect immediately

The fix: Restart your session after modifying Hook config, or run /hooks to verify the active setup.


5. Debugging Tips

Test the Script Manually

1
2
3
# Simulate Stop event input
echo '{"hook_event_name": "Stop", "transcript_path": "/path/to/transcript.jsonl"}' | \
bash .claude/hooks/notify.sh

Verify the Feishu Webhook

1
2
3
4
export FEISHU_WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/your-webhook-token"
curl -X POST "$FEISHU_WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d '{"msg_type":"text","content":{"text":"Test message"}}'

A response of {"StatusCode":0} confirms the webhook is working.


6. Key Takeaways

Three lessons stood out from building this:

  1. Hooks are deterministic: Claude can “forget” instructions written in CLAUDE.md, whereas a Hook runs 100% of the time
  2. Read the official docs closely: The difference between Stop and SessionEnd, along with their exact payload schemas, is documented right there
  3. Debug incrementally: The transcript format is complex—verify the output of each step one at a time


Last updated: 2026-03-25

Series Navigation