跳到主要内容

设置团队 Telegram 助手

本教程带你逐步设置一个由 Hermes Agent 驱动的 Telegram 机器人,供多名团队成员使用。完成后,你的团队将拥有一个共享的 AI 助手,可以向它发消息寻求代码、研究、系统管理等方面的帮助——并通过按用户授权保障安全。

我们要构建什么

一个 Telegram 机器人,能够:

  • 任何授权的团队成员都可以私信寻求帮助——代码审查、研究、Shell 命令、调试
  • 在你的服务器上运行,具有完整的工具访问权限——终端、文件编辑、网络搜索、代码执行
  • 按用户会话——每个人都有自己的对话上下文
  • 默认安全——只有经过批准的用户才能交互,提供两种授权方法
  • 定时任务——每日站会、健康检查和提醒投递到团队频道

前提条件

开始之前,确保你有:

  • 在服务器或 VPS 上安装了 Hermes Agent(不是你的笔记本电脑——机器人需要持续运行)。如果还没有,请遵循安装指南
  • 一个 Telegram 账号(机器人所有者账号)
  • 已配置 LLM 提供商——至少在 ~/.hermes/.env 中有 OpenAI、Anthropic 或其他支持的提供商的 API 密钥
提示

每月 5 美元的 VPS 就足够运行网关了。Hermes 本身很轻量——花钱的是 LLM API 调用,那些是远程发生的。


步骤 1:创建 Telegram 机器人

每个 Telegram 机器人都从 @BotFather 开始——Telegram 官方的机器人创建工具。

  1. 打开 Telegram 并搜索 @BotFather,或前往 t.me/BotFather

  2. 发送 /newbot — BotFather 会询问两件事:

    • 显示名称 — 用户看到的名字(例如 Team Hermes Assistant
    • 用户名 — 必须以 bot 结尾(例如 myteam_hermes_bot
  3. 复制机器人令牌 — BotFather 会回复类似以下内容:

    Use this token to access the HTTP API:
    7123456789:AAH1bGciOiJSUzI1NiIsInR5cCI6Ikp...

    保存这个令牌——下一步需要用到。

  4. 设置描述(可选但推荐):

    /setdescription

    选择你的机器人,然后输入类似以下内容:

    Team AI assistant powered by Hermes Agent. DM me for help with code, research, debugging, and more.
  5. 设置机器人命令(可选——给用户一个命令菜单):

    /setcommands

    选择你的机器人,然后粘贴:

    new - Start a fresh conversation
    model - Show or change the AI model
    status - Show session info
    help - Show available commands
    stop - Stop the current task
注意

保持机器人令牌私密。任何拥有该令牌的人都可以控制机器人。如果泄露,在 BotFather 中使用 /revoke 生成新令牌。


步骤 2:配置网关

你有两个选项:交互式设置向导(推荐)或手动配置。

选项 A:交互式设置(推荐)

hermes gateway setup

这会通过方向键选择引导你完成所有设置。选择 Telegram,粘贴你的机器人令牌,并在提示时输入你的用户 ID。

选项 B:手动配置

~/.hermes/.env 中添加以下行:

# Telegram bot token from BotFather
TELEGRAM_BOT_TOKEN=7123456789:AAH1bGciOiJSUzI1NiIsInR5cCI6Ikp...

# Your Telegram user ID (numeric)
TELEGRAM_ALLOWED_USERS=123456789

查找你的用户 ID

你的 Telegram 用户 ID 是一个数值(不是你的用户名)。查找方法:

  1. 在 Telegram 上发消息给 @userinfobot
  2. 它立即回复你的数字用户 ID
  3. 将该数字复制到 TELEGRAM_ALLOWED_USERS
信息

Telegram 用户 ID 是类似 123456789 的永久数字。它们不同于你可能变更的 @username。始终使用数字 ID 添加到白名单。


步骤 3:启动网关

快速测试

先在前台运行网关,确保一切正常:

hermes gateway

你应该看到类似以下的输出:

[Gateway] Starting Hermes Gateway...
[Gateway] Telegram adapter connected
[Gateway] Cron scheduler started (tick every 60s)

打开 Telegram,找到你的机器人,发送一条消息。如果它回复了,你就成功了。按 Ctrl+C 停止。

生产环境:作为服务安装

要实现重启后持久运行的部署:

hermes gateway install
sudo hermes gateway install --system # Linux only: boot-time system service

这会创建一个后台服务:Linux 上默认是用户级 systemd 服务,macOS 上是 launchd 服务,如果传入 --system 则是 Linux 启动级系统服务。

# Linux — manage the default user service
hermes gateway start
hermes gateway stop
hermes gateway status

# View live logs
journalctl --user -u hermes-gateway -f

# Keep running after SSH logout
sudo loginctl enable-linger $USER

# Linux servers — explicit system-service commands
sudo hermes gateway start --system
sudo hermes gateway status --system
journalctl -u hermes-gateway -f
# macOS — manage the service
hermes gateway start
hermes gateway stop
tail -f ~/.hermes/logs/gateway.log
macOS PATH

launchd plist 在安装时捕获你的 Shell PATH,这样网关子进程可以找到 Node.js 和 ffmpeg 等工具。如果之后安装了新工具,重新运行 hermes gateway install 来更新 plist。

验证正在运行

hermes gateway status

然后在 Telegram 上给你的机器人发送测试消息。几秒钟内应该得到响应。


步骤 4:设置团队访问

现在让你的团队成员获得访问权限。有两种方法。

方法 A:静态白名单

收集每位团队成员的 Telegram 用户 ID(让他们发消息给 @userinfobot),并以逗号分隔列表形式添加:

# In ~/.hermes/.env
TELEGRAM_ALLOWED_USERS=123456789,987654321,555555555

更改后重启网关:

hermes gateway stop && hermes gateway start

方法 B:DM 配对(团队推荐)

DM 配对更灵活——你不需要预先收集用户 ID。工作方式如下:

  1. 团队成员私信机器人 — 由于他们不在白名单上,机器人回复一个一次性配对码:

    🔐 Pairing code: XKGH5N7P
    Send this code to the bot owner for approval.
  2. 团队成员将代码发给你(通过任何渠道——Slack、邮件、当面)

  3. 你在服务器上批准

    hermes pairing approve telegram XKGH5N7P
  4. 他们已加入 — 机器人立即开始响应他们的消息

管理已配对用户:

# See all pending and approved users
hermes pairing list

# Revoke someone's access
hermes pairing revoke telegram 987654321

# Clear expired pending codes
hermes pairing clear-pending
提示

DM 配对非常适合团队,因为添加新用户时不需要重启网关。批准立即生效。

安全注意事项

  • 永远不要在有终端访问权限的机器人上设置 GATEWAY_ALLOW_ALL_USERS=true — 任何找到你机器人的人都可能在服务器上执行命令
  • 配对码在 1 小时后过期,并使用密码学随机性
  • 速率限制防止暴力破解:每用户每 10 分钟 1 个请求,每平台最多 3 个待处理代码
  • 经过 5 次失败的批准尝试后,平台进入 1 小时锁定
  • 所有配对数据以 chmod 0600 权限存储

步骤 5:配置机器人

设置主频道

主频道是机器人投递 Cron 任务结果和主动消息的地方。没有主频道,定时任务就没有地方发送输出。

选项 1: 在机器人是成员的任何 Telegram 群组或聊天中使用 /sethome 命令。

选项 2:~/.hermes/.env 中手动设置:

TELEGRAM_HOME_CHANNEL=-1001234567890
TELEGRAM_HOME_CHANNEL_NAME="Team Updates"

要查找频道 ID,将 @userinfobot 添加到群组——它会报告群组的聊天 ID。

配置工具进度显示

控制机器人在使用工具时显示多少详情。在 ~/.hermes/config.yaml 中:

display:
tool_progress: new # off | new | all | verbose
模式你看到的内容
off仅显示清洁响应——无工具活动
new每次新工具调用的简短状态(推荐用于消息平台)
all每次工具调用及其详情
verbose包括命令结果的完整工具输出

用户也可以在聊天中通过 /verbose 命令按会话修改。

使用 SOUL.md 设置个性

通过编辑 ~/.hermes/SOUL.md 自定义机器人的沟通方式:

完整指南请参阅在 Hermes 中使用 SOUL.md

# Soul
You are a helpful team assistant. Be concise and technical.
Use code blocks for any code. Skip pleasantries — the team
values directness. When debugging, always ask for error logs
before guessing at solutions.

添加项目上下文

如果你的团队在特定项目上工作,创建上下文文件让机器人了解你的技术栈:

<!-- ~/.hermes/AGENTS.md -->
# Team Context
- We use Python 3.12 with FastAPI and SQLAlchemy
- Frontend is React with TypeScript
- CI/CD runs on GitHub Actions
- Production deploys to AWS ECS
- Always suggest writing tests for new code
信息

上下文文件会注入到每个会话的系统提示中。保持简洁——每个字符都计入你的 token 预算。


步骤 6:设置定时任务

网关运行后,你可以安排定期任务,将结果投递到团队频道。

每日站会摘要

在 Telegram 上给机器人发消息:

Every weekday at 9am, check the GitHub repository at
github.com/myorg/myproject for:
1. Pull requests opened/merged in the last 24 hours
2. Issues created or closed
3. Any CI/CD failures on the main branch
Format as a brief standup-style summary.

Agent 会自动创建 Cron 任务,并将结果投递到你提问的聊天(或主频道)。

服务器健康检查

Every 6 hours, check disk usage with 'df -h', memory with 'free -h',
and Docker container status with 'docker ps'. Report anything unusual —
partitions above 80%, containers that have restarted, or high memory usage.

管理定时任务

# From the CLI
hermes cron list # View all scheduled jobs
hermes cron status # Check if scheduler is running

# From Telegram chat
/cron list # View jobs
/cron remove <job_id> # Remove a job
注意

Cron 任务提示词在完全新鲜的会话中运行,不记得之前的对话。确保每个提示词包含 Agent 需要的所有 上下文——文件路径、URL、服务器地址以及清晰的指令。


生产环境技巧

使用 Docker 保障安全

在共享团队机器人上,使用 Docker 作为终端后端,这样 Agent 命令在容器中而不是在宿主机上运行:

# In ~/.hermes/.env
TERMINAL_BACKEND=docker
TERMINAL_DOCKER_IMAGE=nikolaik/python-nodejs:python3.11-nodejs20

或在 ~/.hermes/config.yaml 中:

terminal:
backend: docker
container_cpu: 1
container_memory: 5120
container_persistent: true

这样,即使有人要求机器人运行破坏性操作,你的宿主系统也是受保护的。

监控网关

# Check if the gateway is running
hermes gateway status

# Watch live logs (Linux)
journalctl --user -u hermes-gateway -f

# Watch live logs (macOS)
tail -f ~/.hermes/logs/gateway.log

保持 Hermes 更新

在 Telegram 上向机器人发送 /update——它会拉取最新版本并重启。或者从服务器:

hermes update
hermes gateway stop && hermes gateway start

日志位置

内容位置
网关日志journalctl --user -u hermes-gateway(Linux)或 ~/.hermes/logs/gateway.log(macOS)
Cron 任务输出~/.hermes/cron/output/{job_id}/{timestamp}.md
Cron 任务定义~/.hermes/cron/jobs.json
配对数据~/.hermes/pairing/
会话历史~/.hermes/sessions/

进一步探索

你现在有了一个可用的团队 Telegram 助手。以下是一些后续步骤:

  • 安全指南 — 深入了解授权、容器隔离和命令批准
  • 消息网关 — 网关架构、会话管理和聊天命令的完整参考
  • Telegram 设置 — 平台特定详情,包括语音消息和 TTS
  • 定时任务 — 带投递选项和 Cron 表达式的高级 Cron 调度
  • 上下文文件 — 用于项目知识的 AGENTS.md、SOUL.md 和 .cursorrules
  • 个性 — 内置个性预设和自定义角色定义
  • 添加更多平台 — 同一网关可以同时运行 DiscordSlackWhatsApp

有问题?在 GitHub 上提 issue——欢迎社区贡献。