GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - LeadRider/MAX-bridge-Telegram: MAX → Telegram Bridge

BranchesTags
Open more actions menu

Latest commit

 

History

23 Commits
23 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
certs
certs
 
 
data
data
 
 
scripts
scripts
 
 
src
src
 
 
tests
tests
 
 
.dockerignore
.dockerignore
 
 
.env.example
.env.example
 
 
.gitignore
.gitignore
 
 
Dockerfile
Dockerfile
 
 
README.md
README.md
 
 
docker-compose.yml
docker-compose.yml
 
 
nginx.conf
nginx.conf
 
 
pyproject.toml
pyproject.toml
 
 
pytest.ini
pytest.ini
 
 
requirements.txt
requirements.txt
 
 

Repository files navigation

max2tg-topic-bridge

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.


What it does

  • 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.

What it does NOT do

  • No official MAX bot, no Playwright/Selenium, no DOM polling.
  • No reverse flow (Telegram → MAX). There are no MAX sendMessage calls and no Telegram incoming handlers.
  • No new forum topics. It targets an existing message_thread_id only.
  • No alerts/startup/error messages in the Telegram topic — those go to logs.

Architecture

[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]
  • webhook service — FastAPI app, receives GREEN-API events, writes to SQLite.
  • sender service — reads SQLite, sends to Telegram respecting the rate limit.
  • Both share the same SQLite file (data/bridge.db).

Project layout

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

Configuration

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

GREEN-API setup

  1. Register at https://green-api.com and create a MAX instance.
  2. Authorize the instance with the QR code in the console (SMS login is no longer supported).
  3. Copy idInstance and apiTokenInstance into .env.
  4. Find the target chat id with GetChats (group chats have a negative id) and set it as MAX_CHAT_ID.
  5. 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.


TLS certificates

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-domain in nginx.conf with 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.


Run

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

Deployment (VPS)

End-to-end steps from a fresh VPS with a public IP and a domain.

1. Prerequisites

  • 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

2. Firewall

Allow only SSH, HTTP and HTTPS:

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Port 8080 (FastAPI) is not exposed publicly — only nginx reaches it inside the Docker network.

3. Get the code

git clone https://github.com/LeadRider/MAX-bridge-Telegram.git max2tg-topic-bridge
cd max2tg-topic-bridge

4. Configure .env

cp .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.

5. TLS certificate

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.pem

For local testing only you can use ./scripts/gen_certs.sh bridge.example.com instead (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

6. Authorize the MAX instance

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)

7. Point the GREEN-API webhook at the VPS

.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>.

8. Start

mkdir -p data
docker compose up -d --build
docker compose ps

The sender waits for the webhook healthcheck before starting.

9. Verify

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.


Updating

git pull
docker compose up -d --build
docker compose ps

SQLite data lives in ./data and survives rebuilds.


Troubleshooting

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

Tests

python -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/python -m pytest

Acceptance checklist

  1. Start the bridge → terminal logs Bridge started; the topic stays silent.
  2. Send a message in the MAX chat → it appears in the existing topic.
  3. Send 5 messages quickly → exactly 2 arrive; the rest are queued.
  4. Wait 30–60 min → queued messages arrive with the ⏳ prefix.
  5. Write in the Telegram topic yourself → nothing arrives in MAX.
  6. Check the topic list → the number of topics is unchanged.
  7. Kill the MAX session → terminal shows an error; the topic stays silent.

Notes

  • 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.

About

MAX → Telegram Bridge

Topics

bridgemaxtelegram

Resources

Readme
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.