全部指南

把 Codex CLI 通知发送到 Telegram、Slack 或 Discord

更新于 2026年11月19日 · 阅读约 5 分钟

要把 Codex CLI 通知发送到 Telegram、Slack 或 Discord,先在对应的 app 中创建一个 bot 或 incoming webhook,然后添加一个用 curl 向它发送简短消息的 Codex hook。Stop hook 负责“Codex 已完成”,PermissionRequest hook 负责“Codex 需要批准”。Telegram 需要 bot token 和你的 chat ID;Slack 和 Discord 只需要一个 webhook URL。用 /hooks 把 hook 信任一次即可。同一个脚本在 Mac 和 Linux 上都能用,Windows 则有一个简短的 PowerShell 版本。

01获取 token 或 webhook

Telegram

  1. 在 Telegram 中给 @BotFather 发消息,发送 /newbot 并按步骤操作。它会给你一个类似 123456:ABC... 的 bot token。
  2. 给你新建的 bot 发任意一条消息。
  3. 在浏览器中打开 https://api.telegram.org/bot<TOKEN>/getUpdates,找到 "chat":{"id":...}。这个数字就是你的 chat ID。

Slack

  1. 为你的 workspace 创建一个 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 则把 body 换成 @{ 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 在等待,但无法把你的回复带回会话。要实现这一点(一个接收你回复的 bot,加上一个等待回复的 hook)是可行的,但本身就是一个项目;在手机上批准介绍了其中涉及的工作。Notifier for Codex 是现成的双向方案:免费的 codex-notifier 工具支持 Mac、Windows 和 Linux,配合 iPhone 或 Android app,你可以在上面批准、拒绝、回答和发送后续指令。它是独立产品,与 OpenAI 无关联。

06常见问题

三者中哪个设置最快?

Discord 和 Slack:一个 webhook URL 就搞定。Telegram 需要 bot 和 chat 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 完成任务或需要你时,手机会收到 push,你可以直接在手机上批准、拒绝或回复。codex-notifier 命令行工具在 Mac、Windows 和 Linux 上免费使用。