将脚本输出管道至消息平台
hermes send 是一个轻量级、可脚本化的命令行工具,可将消息推送到 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 | 在正文前添加标题/主题行。 |
-l, --list | 列出所有可用目标。可选的位置参数用于过滤特定平台。 |
-q, --quiet | 成功时不输出标准输出(仅返回退出码 —— 适合脚本使用)。 |
--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、短信、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 - 管道输入 —
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、短信、WhatsApp Cloud API 等),hermes send 会直接调用平台的 REST 接口,使用来自 ~/.hermes/.env 和 ~/.hermes/config.yaml 的凭证。它是一个独立的子进程,在消息发送完成后立即退出。
只有在使用插件类平台时才需要运行网关,这些平台依赖持久连接适配器(例如某个保持长连接 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:#ops、slack:#engineering)会在发送时根据此缓存进行解析,因此无需记忆数字 ID。
与其他方法对比
| 方法 | 多平台支持 | 复用 Hermes 凭据 | 需要网关 | 最佳适用场景 |
|---|---|---|---|---|
hermes send | ✅ | ✅ | 否(基于机器人令牌) | 下述所有场景 |
为每个平台分别编写原始 curl | 逐个脚本实现 | 手动管理 | 否 | 关键性监控任务 |
cron 作业 + --deliver | ✅ | ✅ | 否 | 定期执行的任务 |
send_message 代理工具 | ✅ | ✅ | 否 | 在代理循环内部使用 |
hermes send 的设计初衷是尽可能简单。如果你需要代理决定说什么内容,请在聊天或 Cron 任务中使用 send_message 工具。若需要定时运行并由 LLM 生成内容,请结合使用 cronjob(action='create', prompt=...) 与 deliver='telegram:...'。若只需传递一段原始字符串,直接使用 hermes send 即可。
相关内容
- 用 Cron 自动化任何事 —— 可自动将输出推送到任意平台的定时任务。
- 网关内部机制 ——
hermes send与 Cron 推送共享的分发路由。 - 消息平台设置 —— 每个平台的一次性配置指南。