所有指南

把 Codex CLI 通知傳到 Telegram、Slack 或 Discord

更新於 2026年11月19日 · 閱讀時間 5 分鐘

要把 Codex CLI 通知傳到 Telegram、Slack 或 Discord,先在該 App 中建立機器人或傳入 webhook,再加入一個用 curl 發送簡短訊息的 Codex hook。Stop hook 負責「Codex 已完成」,PermissionRequest hook 負責「Codex 需要核准」。Telegram 需要機器人 token 和你的聊天 ID;Slack 和 Discord 只需要 webhook URL。用 /hooks 信任 hook 一次即可。同一支腳本在 Mac 和 Linux 上都能用,Windows 則有一個簡短的 PowerShell 版本。

01取得 token 或 webhook

Telegram

  1. 在 Telegram 中傳訊息給 @BotFather,送出 /newbot 並依照步驟操作。它會給你一個像 123456:ABC... 的機器人 token。
  2. 隨便傳一則訊息給你的新機器人。
  3. 在瀏覽器中開啟 https://api.telegram.org/bot<TOKEN>/getUpdates,找到 "chat":{"id":...}。那個數字就是你的聊天 ID。

Slack

  1. 為你的工作區建立一個 Slack App,開啟 Incoming Webhooks,並為某個頻道新增 webhook(私人頻道或傳給自己的私訊都很適合)。
  2. 複製 webhook URL(https://hooks.slack.com/services/...)。

Discord

  1. 在你管理的伺服器中,打開頻道設定 > 整合 > Webhook > 新 Webhook。
  2. 複製 webhook URL(https://discord.com/api/webhooks/...)。

把這些都當成密碼看待。任何拿到 URL 或 token 的人都能在你的頻道發文。

02三種服務共用一支腳本

存成 ~/.codex/hooks/chat-notify.sh,填入你使用的服務,並執行 chmod +x:

#!/bin/sh
# Posts a Codex alert to Telegram, Slack or Discord. Prints nothing.
SERVICE="slack"                       # telegram | slack | discord
TELEGRAM_TOKEN="" TELEGRAM_CHAT=""    # for telegram
WEBHOOK_URL=""                        # for slack or discord

input=$(cat)
text=$(printf '%s' "$input" | python3 -c '
import json, os, sys
e = json.load(sys.stdin)
project = os.path.basename(e.get("cwd", "")) or "Codex"
if e.get("hook_event_name") == "Stop":
    msg = (e.get("last_assistant_message") or "Turn complete")[:300]
    print(f"Codex finished in {project}: {msg}")
elif e.get("hook_event_name") == "PermissionRequest":
    cmd = ((e.get("tool_input") or {}).get("command") or "an action")[:300]
    print(f"Codex wants approval in {project}: {cmd}")
')
[ -z "$text" ] && exit 0
json=$(printf '%s' "$text" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')

case "$SERVICE" in
  telegram) curl -s -o /dev/null --max-time 10 \
              --data-urlencode "chat_id=$TELEGRAM_CHAT" --data-urlencode "text=$text" \
              "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" ;;
  slack)    curl -s -o /dev/null --max-time 10 -H 'Content-Type: application/json' \
              -d "{\"text\": $json}" "$WEBHOOK_URL" ;;
  discord)  curl -s -o /dev/null --max-time 10 -H 'Content-Type: application/json' \
              -d "{\"content\": $json}" "$WEBHOOK_URL" ;;
esac
exit 0

接著在 ~/.codex/hooks.json 中:

{
  "hooks": {
    "Stop": [
      { "hooks": [ { "type": "command", "command": "~/.codex/hooks/chat-notify.sh", "timeout": 15 } ] }
    ],
    "PermissionRequest": [
      { "hooks": [ { "type": "command", "command": "~/.codex/hooks/chat-notify.sh", "timeout": 15 } ] }
    ]
  }
}

啟動 codex,執行 /hooks 並信任這兩個項目。

兩個 Codex 特有的細節:

  • 不要輸出任何東西。Codex 會讀取 Stop hook 的輸出,純文字屬於無效輸出。所以每個 curl 都加上了 -s -o /dev/null。
  • 不要做任何決定。PermissionRequest hook 不輸出決定,所以 Codex 仍會在終端機顯示核准提示。訊息只是告訴你有提示在等。

如果網路很慢,請在每個 handler 中加上 "async": true,讓 Codex 不用等訊息送出。

03Windows

PowerShell 的做法相同,這裡以 Slack 為例(Discord 請把內容換成 @{ content = $text })。存成 %USERPROFILE%\.codex\hooks\chat-notify.ps1:

$webhook = "https://hooks.slack.com/services/..."
$e = [Console]::In.ReadToEnd() | ConvertFrom-Json
$project = Split-Path -Leaf $e.cwd
if ($e.hook_event_name -eq "Stop") { $text = "Codex finished in ${project}: $($e.last_assistant_message)" }
elseif ($e.hook_event_name -eq "PermissionRequest") { $text = "Codex wants approval in ${project}: $($e.tool_input.command)" }
else { exit 0 }
$body = @{ text = $text } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri $webhook -ContentType 'application/json' -Body $body | Out-Null

在兩個 hook 中都使用 "command": "powershell -NoProfile -ExecutionPolicy Bypass -File C:\\Users\\you\\.codex\\hooks\\chat-notify.ps1"。

04注意發送的內容

訊息包含專案名稱、Codex 的最後一則訊息,以及它想執行的指令。在共用的 Slack 或 Discord 頻道中,所有成員都看得到。請使用私人頻道或私訊,如果工作內容敏感,就把訊息縮短。

05只能單向

聊天訊息能告訴你 Codex 在等你,但無法把你的回答帶回工作階段。要做到這點(一個接收你回覆的機器人,加上一個等待回覆的 hook)是可行的,但本身就是一個專案;用手機核准說明了其中涉及的工作。Notifier for Codex 是現成的雙向選擇:免費的 codex-notifier 工具(支援 Mac、Windows 和 Linux)加上 iPhone 或 Android App,讓你核准、拒絕、回答和送出後續指示。它是獨立 App,與 OpenAI 無關。

06常見問題

這三種哪個設定最快?

Discord 和 Slack:一個 webhook URL 就搞定。Telegram 需要機器人和聊天 ID,但它的手機通知非常可靠。

可以用 Codex 的 notify 設定取代 hook 嗎?

只要「已完成」的話可以。notify 是以參數傳入回合的 JSON,而不是透過 stdin,所以要把腳本改成讀取 sys.argv[-1]。請見 notify 指南。

為什麼什麼都沒收到?

確認 hook 已在 /hooks 中信任,再手動執行腳本:echo '{"hook_event_name":"Stop","cwd":"/tmp/demo","last_assistant_message":"test"}' | ~/.codex/hooks/chat-notify.sh。測試時先把 curl 那行的 -s -o /dev/null 拿掉,才能看到服務的回應。

可以把提醒發送到團隊頻道嗎?

可以,但要想想訊息會透露什麼。你專案中的指令和檔案路徑,頻道裡的每個人都看得到。

Notifier for Codex Codex 完成或需要你時,手機立即收到推播,直接在手機上核准、拒絕或回答。codex-notifier 命令列工具在 Mac、Windows 和 Linux 上皆可免費使用。