One-way bridge: MAX (via GREEN-API) → Telegram existing forum topic. Text messages only. At most 2 messages per hour. Logs to stdout/stderr only.
- Receives incoming MAX messages through a GREEN-API webhook.
- Filters messages to a single
MAX_CHAT_ID(ignore every other chat). - Queues and forwards text messages to an existing Telegram forum topic
(
message_thread_id). - Enforces a persistent token-bucket rate limit (default 2/hour) that
survives restarts; excess messages are queued and sent later with a
⏳ [delayed, original: HH:MM]prefix. - Never creates topics, never sends anything back to MAX, never posts service alerts to Telegram.
- No official MAX bot, no Playwright/Selenium, no DOM polling.
- No reverse flow (Telegram → MAX). There are no MAX
sendMessagecalls and no Telegram incoming handlers. - No new forum topics. It targets an existing
message_thread_idonly. - No alerts/startup/error messages in the Telegram topic — those go to logs.
[Personal MAX account]
|
| GREEN-API webhook (HTTPS)
v
[VPS: Nginx (443) -> FastAPI (8080)]
|
| store in SQLite (status='new')
v
[sender: token-bucket 2/hour, persistent]
|
| aiogram send_message(chat_id, message_thread_id, text)
v
[Existing Telegram forum topic]
webhookservice — FastAPI app, receives GREEN-API events, writes to SQLite.senderservice — reads SQLite, sends to Telegram respecting the rate limit.- Both share the same SQLite file (
data/bridge.db).
src/
config.py .env loading (pydantic-settings) + validation
utils.py stdout logger
storage.py SQLite queue/state (WAL)
rate_limiter.py persistent token-bucket limiter
webhook_server.py FastAPI webhook receiver
telegram_client.py aiogram sender (retries, HTML escape)
main.py sender runtime
nginx.conf reverse proxy + GREEN-API IP allowlist
Dockerfile python:3.11-slim image
docker-compose.yml webhook + sender + nginx
scripts/gen_certs.sh self-signed certs for local testing
tests/ pytest suite
Copy .env.example to .env and fill in the values:
cp .env.example .env
| Variable | Required | Description |
|---|---|---|
TG_BOT_TOKEN |
yes | Telegram bot token from @BotFather |
TG_GROUP_ID |
yes | Target group/supergroup ID (e.g. -100...) |
TG_TOPIC_ID |
yes | Existing forum topic id; app refuses to start without it |
GREEN_API_ID_INSTANCE |
yes | GREEN-API instance id |
GREEN_API_TOKEN |
yes | GREEN-API instance token |
GREEN_API_URL |
no | Default https://api.green-api.com |
MAX_CHAT_ID |
yes | The single MAX chat to forward; group ids are negative |
WEBHOOK_HOST |
no | Default 0.0.0.0 |
WEBHOOK_PORT |
no | Default 8080 |
WEBHOOK_PATH |
no | Default /webhook (must start with /) |
RATE_LIMIT_PER_HOUR |
no | Default 2 |
DB_PATH |
no | Default data/bridge.db |
LOG_LEVEL |
no | Default INFO |
- Register at https://green-api.com and create a MAX instance.
- Authorize the instance with the QR code in the console (SMS login is no longer supported).
- Copy
idInstanceandapiTokenInstanceinto.env. - Find the target chat id with
GetChats(group chats have a negative id) and set it asMAX_CHAT_ID. - Point the instance webhook at your domain with
SetSettings:
{
"webhookUrl": "https://your-domain.example/webhook",
"incomingWebhook": "yes",
"outgoingWebhook": "no",
"stateWebhook": "yes"
}Only incomingMessageReceived and stateInstanceChanged events are needed.
The bundled nginx.conf expects certificates at:
/etc/letsencrypt/live/<your-domain>/fullchain.pem and privkey.pem.
- Production: obtain them with certbot and replace
your-domaininnginx.confwith your real domain, then mount the cert directory. - Local/testing: generate self-signed certs:
./scripts/gen_certs.sh your-domain.example
This creates certs/live/your-domain.example/{fullchain.pem,privkey.pem}.
Update the ssl_certificate paths in nginx.conf accordingly.
mkdir -p data certs docker compose up -d --build
Services:
docker compose ps docker compose logs -f webhook docker compose logs -f sender
Stop:
docker compose down
End-to-end steps from a fresh VPS with a public IP and a domain.
- VPS with a public IP (Debian/Ubuntu assumed).
- A domain whose A record points to the VPS IP, e.g.
bridge.example.com. - Docker Engine + Compose plugin:
curl -fsSL https://get.docker.com | sh sudo usermod -aG docker "$USER" # re-login afterwards
Allow only SSH, HTTP and HTTPS:
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enablePort 8080 (FastAPI) is not exposed publicly — only nginx reaches it
inside the Docker network.
git clone https://github.com/LeadRider/MAX-bridge-Telegram.git max2tg-topic-bridge
cd max2tg-topic-bridgecp .env.example .env nano .env
Fill in TG_BOT_TOKEN, TG_GROUP_ID, TG_TOPIC_ID, GREEN_API_* and
MAX_CHAT_ID (see the Configuration table). Keep TG_TOPIC_ID=65 for the
existing topic.
Replace your-domain in nginx.conf with your real domain first, then obtain a
Let's Encrypt certificate:
sudo apt-get install -y certbot sudo certbot certonly --standalone -d bridge.example.com
Certificates land in /etc/letsencrypt/live/bridge.example.com/. Copy (or
symlink) them into the repo's certs/ dir so the container can read them:
mkdir -p certs/live/bridge.example.com
sudo cp -L /etc/letsencrypt/live/bridge.example.com/fullchain.pem certs/live/bridge.example.com/
sudo cp -L /etc/letsencrypt/live/bridge.example.com/privkey.pem certs/live/bridge.example.com/
sudo chown -R "$USER" certs
chmod 600 certs/live/bridge.example.com/privkey.pemFor local testing only you can use
./scripts/gen_certs.sh bridge.example.cominstead (self-signed).
Certificate renewal (run certbot renew and re-copy, e.g. from a cron job):
sudo certbot renew --quiet && sudo cp -L /etc/letsencrypt/live/bridge.example.com/*.pem certs/live/bridge.example.com/ && docker compose restart nginx
Register at https://green-api.com, create a MAX instance and scan the QR
code in the console to log in the personal account. Then copy idInstance and
apiTokenInstance into .env and verify:
.venv/bin/python scripts/green_api.py state
.venv/bin/python scripts/green_api.py chats # find MAX_CHAT_ID (negative for groups).venv/bin/python scripts/green_api.py webhook --url https://bridge.example.com/webhook
If WEBHOOK_SECRET is set, use https://bridge.example.com/webhook/<secret>.
mkdir -p data docker compose up -d --build docker compose ps
The sender waits for the webhook healthcheck before starting.
docker compose logs -f webhook # expect "Webhook received: incomingMessageReceived" docker compose logs -f sender # expect "Bridge started"
Then send a message in the MAX chat and confirm it appears in topic 65. Run through the Acceptance checklist below.
git pull docker compose up -d --build docker compose ps
SQLite data lives in ./data and survives rebuilds.
| Symptom | Likely cause | Fix |
|---|---|---|
FATAL: TG_TOPIC_ID is required |
TG_TOPIC_ID empty in .env |
Set it (e.g. 65) and restart sender |
No Webhook received lines |
Webhook URL / DNS / allowlist wrong | Check SetSettings, DNS A record, and GREEN-API IPs in nginx.conf |
403 in nginx logs |
Request not from a GREEN-API IP | Re-check the allow list in nginx.conf |
getStateInstance = notAuthorized |
QR session expired | Re-scan the QR code in the GREEN-API console |
| Messages never reach Telegram | send attempt ... failed in sender logs |
Check bot token, that the bot is in the group, and TG_TOPIC_ID |
| Messages queued forever | Rate limit exhausted | Expected: at most 2/hour; the rest go out later with a ⏳ prefix |
MAX session expired |
MAX logged the device out | Re-authorize via QR; no message is posted to the topic |
Inspect logs (stdout/stderr only, no files):
docker compose logs --tail=100 -f
python -m venv .venv .venv/bin/pip install -r requirements.txt .venv/bin/python -m pytest
- Start the bridge → terminal logs
Bridge started; the topic stays silent. - Send a message in the MAX chat → it appears in the existing topic.
- Send 5 messages quickly → exactly 2 arrive; the rest are queued.
- Wait 30–60 min → queued messages arrive with the
⏳prefix. - Write in the Telegram topic yourself → nothing arrives in MAX.
- Check the topic list → the number of topics is unchanged.
- Kill the MAX session → terminal shows an error; the topic stays silent.
- Logs go to stdout/stderr only. Inspect with
docker compose logs. - The Telegram topic is reserved exclusively for forwarded MAX messages.
.env, the SQLite database, and certificates are git-ignored.