05-sep: [proyecto2] bot Telegram para OpenCode en VPS (serve + bot + systemd, listo para token)

This commit is contained in:
juanjo
2026-09-05 20:16:31 +02:00
parent 6c4b0b2252
commit 326404f827
2 changed files with 657 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
# opencode_tg — Bot de Telegram para OpenCode (VPS)
Bot que permite usar OpenCode desde el móvil vía Telegram. Corre en el VPS
Contabo junto a `opencode serve` (headless). Sin librerías extra (solo
`requests`), long-polling, whitelist por chat_id.
## Despliegue
- Código: `/opt/opencode-tg/opencode_bot.py`
- `.env`: `/opt/opencode-tg/.env`
- Repo activo del serve: `/etc/opencode-tg/repo.env` (`REPO_DIR=...`)
- Servicios systemd:
- `opencode-serve.service` → `opencode serve --hostname 127.0.0.1 --port 4097`
dentro de `$REPO_DIR` (credenciales OPENCODE_SERVER_USERNAME/PASSWORD).
- `opencode-tg.service` → el bot.
Para cambiar de repo el bot reinicia `opencode-serve` (escribe `repo.env`).
Cada chat conserva su sesión y modelo en `estado.json`.
## Comandos del bot
```
/start /ayuda ayuda
/nueva [texto] sesión nueva (olvida el contexto)
/proyecto elegir repo de /home/Github (inline)
/modelo elegir modelo (inline)
/estado repo, sesión y modelo actuales
cualquier texto → lo ejecuta OpenCode en la sesión del chat
```
Entrega: texto como HTML (bloques ``` en <pre>), ficheros creados/editados
como documentos adjuntos (se leen de `state.metadata.files` del historial).
## PENDIENTE (único paso manual)
1. Crear el bot con @BotFather y copiar el token.
2. Rellenar en `/opt/opencode-tg/.env`:
```
TELEGRAM_BOT_TOKEN=<token>
TELEGRAM_CHAT_ID=<tu chat_id>
```
(`TELEGRAM_CHAT_ID` puede ser una lista separada por comas.)
3. Arrancar:
```
systemctl enable --now opencode-tg
```
API comprobada contra opencode 1.17.15: POST /session, POST /session/:id/message
(body `parts` + `model` con `providerID`/`modelID`), GET /session/:id/message,
GET /config/providers, GET /global/health, GET /project.