字节笔记本
2026年5月16日
Hermes Agent 脚本输出管道:将 Agent 输出接入外部工具
hermes send 是一个小巧、可脚本化的 CLI 工具,可以将消息推送到 Hermes 已配置的任何消息平台。你可以把它当作跨平台的 curl 通知工具——不需要运行中的 gateway、不需要 LLM,也不需要在每个脚本中重复粘贴 bot token。
适用场景:
- 系统监控(内存、磁盘、GPU 温度、长时间任务完成通知)
- CI/CD 通知(部署完成、测试失败)
- Cron 脚本需要将结果推送给你
- 终端中快速发送一次性消息
- 将任何工具的输出管道转发到任何地方(
make | hermes send --to slack:#builds)
该命令复用了 hermes gateway 已有的凭据和平台适配器,因此无需维护第二套配置。
快速开始
# 发送纯文本到平台的默认频道
hermes send --to telegram "deploy finished"
# 从任何命令的 stdout 管道输入
echo "RAM 92%" | hermes send --to telegram:-1001234567890
# 发送文件
hermes send --to discord:#ops --file /tmp/report.md
# 附加主题/标题行
hermes send --to slack:#eng --subject "[CI] build.log" --file build.log
# 发送到线程目标(Telegram topic、Discord thread)
hermes send --to telegram:-1001234567890:17585 "threaded reply"
# 列出所有已配置的目标
hermes send --list
# 按平台过滤
hermes send --list telegram参数参考
| 标志 | 说明 |
|---|---|
-t, --to TARGET | 目标地址。参见 目标格式。 |
message(位置参数) | 消息文本。省略则从 --file 或 stdin 读取。 |
-f, --file PATH | 从文件读取消息体。--file - 强制从 stdin 读取。 |
-s, --subject LINE | 在消息体前添加标题/主题行。 |
-l, --list | 列出可用目标。可选位置参数指定平台过滤。 |
-q, --quiet | 成功时不输出到 stdout(仅退出码——适合脚本使用)。 |
--json | 输出发送操作的原始 JSON 结果。 |
-h, --help | 显示内置帮助文本。 |
目标格式
| 格式 | 示例 | 含义 |
|---|---|---|
platform | telegram | 发送到平台配置的默认频道 |
platform:chat_id | telegram:-1001234567890 | 指定数字聊天 ID / 群组 / 用户 |
platform:chat_id:thread_id | telegram:-1001234567890:17585 | 指定线程或 Telegram 论坛主题 |
platform:#channel | discord:#ops | 人类可读的频道名称(根据频道目录解析) |
platform:+E164 | signal:+15551234567 | 电话号码寻址平台:Signal、SMS、WhatsApp |
Hermes 提供适配器的任何平台都可作为目标:
telegram、discord、slack、signal、sms、whatsapp、matrix、mattermost、feishu、dingtalk、wecom、weixin、email 等。
退出码
| 退出码 | 含义 |
|---|---|
0 | 发送(或列表)成功 |
1 | 平台层面投递失败(认证、权限、网络) |
2 | 用法 / 参数 / 配置错误 |
退出码遵循标准 Unix 惯例,你的脚本可以像处理 curl 或 grep 一样进行分支判断。
消息体解析顺序
hermes send 按以下顺序解析消息体:
- 位置参数 —
hermes send --to telegram "hi" --file PATH—hermes send --to telegram --file msg.txt- 管道 stdin —
echo hi | hermes send --to telegram
当 stdin 是 TTY(无管道)时,Hermes 不会等待输入——而是返回清晰的用法错误。这可以防止脚本因意外省略消息体而挂起。
实际应用示例
监控:内存 / 磁盘告警
用一条可移植的命令替换监控脚本中临时的 curl https://api.telegram.org/... 调用:
#!/usr/bin/env bash
ram_pct=$(free | awk '/^Mem:/ {printf "%d", $3 * 100 / $2}')
if [ "$ram_pct" -ge 85 ]; then
hermes send --to telegram --subject "⚠ MEMORY WARNING" \
"RAM ${ram_pct}% on $(hostname)"
fi由于 hermes send 复用了 Hermes 配置,同一脚本在安装了 Hermes 的任何主机上都能正常工作——无需手动将 bot token 导出到每台机器的环境变量中。
提示:不要通过 gateway 告警 gateway 自身的问题 对于可能在 gateway 自身出问题时触发的监控(OOM 告警、磁盘满告警),请继续使用最小化的
curl调用而非hermes send。如果 Python 解释器因系统资源耗尽而无法加载,你仍然需要那个告警能够发出。
CI / CD:构建和测试结果
# 在 .github/workflows/deploy.yml 或任何 CI 脚本中
if ./scripts/deploy.sh; then
hermes send --to slack:#deploys "✅ ${CI_COMMIT_SHA:0:7} deployed"
else
tail -n 100 deploy.log | hermes send \
--to slack:#deploys --subject "❌ deploy failed"
exit 1
fiCron:每日报告
# Crontab 条目
0 9 * * * /usr/local/bin/generate-metrics.sh \
| /home/me/.hermes/bin/hermes send \
--to telegram --subject "Daily metrics $(date +%Y-%m-%d)"长时间运行任务:完成时通知
./train.py --epochs 200 && \
hermes send --to telegram "training done" || \
hermes send --to telegram "training failed (exit $?)"使用 --json 和 --quiet 进行脚本编程
# 投递失败时脚本硬终止;成功时不污染日志
hermes send --to telegram --quiet "keepalive" || {
echo "Telegram delivery failed" >&2
exit 1
}
# 捕获消息 ID 用于后续编辑/线程化
msg_id=$(hermes send --to discord:#ops --json "build started" \
| jq -r .message_id)hermes send 是否需要 Gateway 运行?
通常不需要。 对于任何使用 bot token 的平台——Telegram、Discord、Slack、Signal、SMS、WhatsApp Cloud API 及大多数其他平台——hermes send 使用 ~/.hermes/.env 和 ~/.hermes/config.yaml 中的凭据直接调用平台的 REST 端点。它是一个独立的子进程,消息投递后即退出。
只有插件平台需要运行中的 gateway——这些平台依赖持久适配器连接(例如,保持长连接 WebSocket 的自定义插件)。在这种情况下,你会收到明确的错误提示指向 gateway;使用 hermes gateway start 启动后重试即可。
列出和发现目标
在发送到特定频道之前,你可以先查看可用目标:
# 所有已配置平台的所有目标
hermes send --list
# 仅 Telegram 目标
hermes send --list telegram
# 机器可读格式
hermes send --list --json目标列表基于 ~/.hermes/channel_directory.json 构建,gateway 运行期间每隔几分钟刷新一次。如果你看到 "no channels discovered yet",请先启动 gateway(hermes gateway start)让它填充缓存。
人类可读的名称(discord:#ops、slack:#engineering)在发送时根据此缓存解析,因此你无需记忆数字 ID。
与其他方案的对比
| 方案 | 多平台支持 | 复用 Hermes 凭据 | 需要 Gateway | 最适合场景 |
|---|---|---|---|---|
hermes send | ✅ | ✅ | 不需要(bot-token) | 以下所有场景 |
原始 curl 调用各平台 | 每个平台单独编写脚本 | 手动管理 | 不需要 | 关键监控告警 |
cron 任务使用 --deliver | ✅ | ✅ | 不需要 | 定时 agent 任务 |
send_message agent 工具 | ✅ | ✅ | 不需要 | agent 循环内部 |
hermes send 的设计理念是尽可能简单。如果你需要 agent 来决定发送什么内容,请在聊天或 cron 任务中使用 send_message 工具。如果你需要定时运行并生成 LLM 内容,使用 cronjob(action='create', prompt=...) 配合 deliver='telegram:...'。如果你只需要管道传递原始字符串,就用 hermes send。
相关文档
- 使用 Cron 自动化一切 — 定时任务,输出自动投递到任何平台。
- Gateway 内部机制 —
hermes send与 cron 投递共享的投递路由器。 - 消息平台配置 — 各平台的一次性配置指南。