跳到主要内容

将脚本输出管道传输至消息平台

hermes send 是一个小巧、可脚本化的命令行工具(CLI),用于向 Hermes 已配置的任何消息平台推送消息。你可以将其视为用于通知的跨平台 curl——你无需运行网关,无需大型语言模型(LLM),也无需在每个脚本中重新粘贴机器人令牌。

适用场景包括:

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

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


快速开始

# Plain text to the home channel for a platform
hermes send --to telegram "deploy finished"

# Pipe in stdout from anything
echo "RAM 92%" | hermes send --to telegram:-1001234567890

# Send a file
hermes send --to discord:#ops --file /tmp/report.md

# Attach a subject/header line
hermes send --to slack:#eng --subject "[CI] build.log" --file build.log

# Thread target (Telegram topic, Discord thread)
hermes send --to telegram:-1001234567890:17585 "threaded reply"

# List every configured target
hermes send --list

# Filter by platform
hermes send --list telegram

参数参考

标志描述
-t, --to TARGET目标。参见 目标格式
message(位置参数)消息文本。省略此项以从 --file 或标准输入读取。
-f, --file PATH从文件读取正文。--file - 强制使用标准输入。
-s, --subject LINE在正文前 prepend 一个标题/主题行。
-l, --list列出可用目标。可选的位置参数平台过滤器。
-q, --quiet成功时不输出 stdout(仅返回退出码——非常适合脚本)。
--json输出发送操作的原始 JSON 结果。
-h, --help显示内置帮助文本。

目标格式

格式示例含义
platformtelegram发送到平台配置的默认频道
platform:chat_idtelegram:-1001234567890特定的数字聊天 ID / 群组 / 用户
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. 管道标准输入echo hi | hermes send --to telegram

当标准输入是 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 的任何主机上运行——无需手动将机器人令牌导出到每台机器的环境变量中。

不要通过网关报警其自身状态

对于可能在网关本身陷入困境时触发的看门狗(如 OOM 警报、磁盘满警报),请继续使用最小化的 curl 调用而非 hermes send。如果因为系统负载过高导致 Python 解释器无法加载,你仍然希望该警报能够发出。

CI / CD:构建和测试结果

# In .github/workflows/deploy.yml or any CI script
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:每日报告

# Crontab entry
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 进行脚本编写

# Hard-fail a script if delivery fails; don't clutter logs on success
hermes send --to telegram --quiet "keepalive" || {
echo "Telegram delivery failed" >&2
exit 1
}

# Capture the message ID for later editing / threading
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 启动网关并重试。


列出和发现目标

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

# Every target across every configured platform
hermes send --list

# Just Telegram targets
hermes send --list telegram

# Machine-readable
hermes send --list --json

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

人性化名称(discord:#opsslack:#engineering)会在发送时对此缓存进行解析,因此你无需记住数字 ID。


与其他方法的比较

方法多平台支持复用 Hermes 凭证需要网关最佳适用场景
hermes send否(bot-token)以下所有场景
向每个平台发送原始 curl 请求需分别编写脚本手动配置关键监控任务
带有 --delivercron 任务定时代理任务
send_message 代理工具在代理循环内部使用

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