tg-gemini
手机 Telegram ──→ tg-gemini (VPS) ──→ gemini CLI (本地)
↑ │
└──────────── 流式响应 ←───────────────────┘
前置条件
- Python 3.12+ 和 uv
- Gemini CLI 已安装并完成登录:
npm install -g @google/gemini-cli gemini # 首次运行完成认证
- Telegram Bot Token:通过 @BotFather 创建
安装
# pip
pip install tg-gemini
# uv(推荐,全局工具安装)
uv tool install tg-gemini
# 源码开发模式
git clone https://github.com/atticuszeller/tg-gemini
cd tg-gemini && uv sync --all-groups
快速开始
# 1. 创建配置目录
mkdir -p ~/.tg-gemini
# 2. 复制并编辑配置(详细注释见 config.example.toml)
cp config.example.toml ~/.tg-gemini/config.toml
# 编辑:至少填写 telegram.token
# [telegram]
# token = "123456:ABC-DEF..."
# 3. 启动
tg-gemini start
# 或指定配置文件
tg-gemini start --config /path/to/config.toml
Ctrl+C 优雅停止。
配置
配置文件为 TOML 格式,路径解析顺序:
--config参数- 当前目录
config.toml ~/.tg-gemini/config.toml(默认)
最小配置:
[telegram]
token = "YOUR_BOT_TOKEN"
完整选项见 config.example.toml 和 docs/configuration.md。
常用配置片段
# 限制只有自己能用(向 @userinfobot 查询 ID)
[telegram]
token = "..."
allow_from = "你的用户ID"
# 设置工作目录和模式
[gemini]
work_dir = "/path/to/your/project"
mode = "yolo" # 全自动,推荐服务端使用
Bot 命令
| 命令 | 功能 |
|---|---|
/new |
开启新 Gemini 会话 |
/list |
查看所有会话 |
/switch <目标> |
切换会话 |
/name <名称> |
重命名会话 |
/delete |
删除会话 |
/history |
查看对话历史 |
/status |
当前状态信息 |
/model [名称] |
查看/切换模型 |
/mode [模式] |
查看/切换工具模式 |
/lang [en|zh] |
切换界面语言 |
/quiet |
切换静音(隐藏工具通知) |
/stop |
终止当前 Agent |
/help |
帮助 |
完整说明见 docs/commands.md。
群聊支持
[telegram]
token = "..."
group_reply_all = false # 仅响应 @bot 或回复 bot(默认)
share_session_in_channel = false # 每人独立会话(默认)
默认行为:只有 @机器人、回复机器人的消息、以及 /command 会被处理。
作为服务运行
创建 /etc/systemd/system/tg-gemini.service:
[Unit]
Description=tg-gemini Telegram Bot
After=network.target
[Service]
Type=simple
User=ubuntu
ExecStart=/home/ubuntu/.local/bin/tg-gemini start
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now tg-gemini
sudo journalctl -u tg-gemini -f
架构
tg-gemini 由 7 个核心模块组成:
cli.py → engine.py → gemini.py 子进程 + JSONL 解析
↕
telegram_platform.py 长轮询 + 消息收发
streaming.py 流式预览节流
session.py 多会话 + 历史持久化
card.py 卡片 UI(InlineKeyboard)
详细架构图和数据流见 docs/architecture.md。
常见问题
Q: Gemini CLI 没有响应
gemini -p "hello" --output-format stream-json # 测试 CLI 是否正常
Q: 收不到 Telegram 消息
检查 allow_from 配置,查看日志:tg-gemini start 或 journalctl -u tg-gemini -f。
Q: 流式预览触发限流
增大 interval_ms:
[stream_preview]
interval_ms = 3000
Q: 如何让 Gemini 记住上下文
不要发 /new。服务重启后自动恢复到最近一次会话(通过 --resume 机制)。
文档
| 文档 | 内容 |
|---|---|
| docs/configuration.md | 完整配置项参考表 |
| docs/commands.md | 所有 Bot 命令详细说明 |
| docs/architecture.md | 组件架构、数据流、设计决策 |
| docs/development.md | 开发环境、测试、发布流程 |
| docs/internals.md | stream-json 协议、Markdown 转换、流式预览实现 |
| config.example.toml | 带注释的完整配置示例 |
开发
git clone https://github.com/atticuszeller/tg-gemini
cd tg-gemini && uv sync --all-groups
bash dev.sh check # format → lint → test → pre-commit
bash dev.sh test # 运行测试(662 个,98.95% 覆盖率)
bash dev.sh lint # ty + ruff
License
MIT
Metadata
Release files for tg-gemini 0.0.25
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tg_gemini-0.0.25.tar.gz | 58.8 kB | Without provenance | Details for tg_gemini-0.0.25.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tg_gemini-0.0.25-py3-none-any.whl | Python 3 | none | any | Details for tg_gemini-0.0.25-py3-none-any.whl |
Total release size: 122.7 kB
Release files / tg_gemini-0.0.25.tar.gz
| Download URL | tg_gemini-0.0.25.tar.gz |
|---|---|
| Size | 58.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c79f212b0f1e7791ed5349292cb363725e5bec392c73dada4be9602c0e05f1b
|
|
BLAKE2b-256 checksum How to use checksums |
b74bbe87a79e997ec5da995b168e8067ba9f4fca846ecd8a1dc71a3a0fa268cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / tg_gemini-0.0.25-py3-none-any.whl
| Download URL | tg_gemini-0.0.25-py3-none-any.whl |
|---|---|
| Size | 63.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
34d35cb5b4778915b2752664a9691c2cb11c31006b5855af27a44a692949d6a8
|
|
BLAKE2b-256 checksum How to use checksums |
c296cd98099333a4e9ee58063ea624259c56a7869b5e0d44700bdaf71ccdc3dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for tg-gemini 0.0.25
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.