ByteNoteByteNote

字节笔记本

2026年6月21日

hermes教程-将脚本输出管道传输到消息平台

API中转
¥120

快速开始

hermes send 是一个小巧、可脚本化的 CLI 工具,用于将消息推送到 Hermes 已配置的任何消息平台。可以把它看作跨平台的 curl 通知工具——你不需要运行网关,不需要 LLM,也不需要将机器人令牌重新粘贴到每个脚本中。

使用场景:

  • 系统监控(内存、磁盘、GPU 温度、长时间运行任务完成)
  • CI/CD 通知(部署完成、测试失败)
  • 需要将结果通知你的 Cron 脚本
  • 从终端快速发送一次性消息
  • 将任何工具的输出管道传输到任何地方(make | hermes send --to slack:#builds

该命令复用 hermes gateway 已使用的相同凭据和平台适配器,因此无需维护第二套配置。


快速开始

bash
## 向平台的主频道发送纯文本
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 话题,Discord 线程)
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显示内置帮助文本。

目标格式

格式示例含义
platformtelegram发送到平台配置的主频道
platform:chat_idtelegram:-1001234567890特定的数字聊天/群组/用户
platform:chat_id:thread_idtelegram:-1001234567890:17585特定线程或 Telegram 论坛话题
platform:#channeldiscord:#ops人类友好的频道名称(根据频道目录解析)
platform:+E164signal:+15551234567基于电话号码的平台:Signal、SMS、WhatsApp

Hermes 提供适配器的任何平台都可以作为目标:telegramdiscordslacksignalsmswhatsappmatrixmattermostfeishudingtalkwecomweixinemail 等。

退出码

含义
0发送(或列出)成功
1在平台级别投递失败(认证、权限、网络)
2用法/参数/配置错误

退出码遵循标准 Unix 约定,因此你的脚本可以像对待 curlgrep 一样根据它们进行分支。


消息正文解析

hermes send 按以下顺序解析消息正文:

  1. 位置参数hermes send --to telegram "hi"
  2. --file PATHhermes send --to telegram --file msg.txt
  3. 管道 stdinecho hi | hermes send --to telegram

当 stdin 是 TTY(无管道)时,Hermes 不会等待输入——而是会给出清晰的用法错误。这可以防止脚本因意外省略正文而挂起。


实际示例

监控:内存/磁盘告警

在你的看门狗中替换临时的 curl https://api.telegram.org/... 调用,使用一行可移植的命令:

bash
#!/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 的主机上运行——无需手动将机器人令牌导出到每台机器的环境中。

提示——不要向网关自身发送告警

对于可能在网关本身出现问题时触发的看门狗(OOM 告警、磁盘满告警),请继续使用最小的 curl 调用而不是 hermes send。如果 Python 解释器因系统抖动而无法加载,你仍然希望告警能够发出。

CI/CD:构建和测试结果

bash
## 在 .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
fi

Cron:每日报告

bash
## Crontab 条目
0 9 * * * /usr/local/bin/generate-metrics.sh \
  | /home/me/.hermes/bin/hermes send \
      --to telegram --subject "Daily metrics $(date +%Y-%m-%d)"

长时间运行任务:完成后通知

bash
./train.py --epochs 200 && \
  hermes send --to telegram "training done" || \
  hermes send --to telegram "training failed (exit $?)"

使用 --json--quiet 编写脚本

bash
## 如果投递失败则硬性失败脚本;成功时不输出日志
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 需要网关运行吗?

通常不需要。 对于任何基于机器人令牌的平台——Telegram、Discord、Slack、Signal、SMS、WhatsApp Cloud API 以及大多数其他平台——hermes send 直接使用 ~/.hermes/.env~/.hermes/config.yaml 中的凭据调用平台的 REST 端点。它是一个独立的子进程,消息投递后立即退出。

仅当使用依赖持久适配器连接的插件平台(例如,保持长连接 WebSocket 的自定义插件)时,才需要运行中的网关。在这种情况下,你会收到指向网关的明确错误;使用 hermes gateway start 启动网关,然后重试。


列出和发现目标

在发送到特定频道之前,你可以检查可用的目标:

bash
## 所有已配置平台的所有目标
hermes send --list
## 仅 Telegram 目标
hermes send --list telegram
## 机器可读格式
hermes send --list --json

列表来自 ~/.hermes/channel_directory.json,网关在运行时每隔几分钟刷新一次。如果你看到“尚未发现频道”,请启动一次网关(hermes gateway start),以便它填充缓存。

人类友好的名称(discord:#opsslack:#engineering)在发送时根据此缓存解析,因此你无需记住数字 ID。


与其他方法的比较

方法多平台复用 Hermes 凭据需要网关最适合
hermes send否(机器人令牌)以下所有场景
对每个平台使用原始 curl每个单独编写脚本手动关键看门狗
使用 --delivercron 任务定时代理任务
send_message 代理工具代理循环内部

hermes send 故意设计为最简单的接口。如果你需要代理决定说什么,请在聊天或 cron 任务中使用 send_message 工具。如果你需要定时运行并生成 LLM 内容,请使用 cronjob(action='create', prompt=...) 并设置 deliver='telegram:...'。如果你只需要管道传输原始字符串,请使用 hermes send


相关



分享: