Skip to main content

Lightweight bidirectional Telegram Bot Gateway — raw HTTP, no heavy SDK, any LLM backend

Project description

🤖 tg-gateway

Lightweight bidirectional Telegram Bot Gateway — raw HTTP, no heavy SDK, instant startup.

Turn any OpenAI-compatible LLM into a Telegram bot in seconds.

pip install tg-gateway-bot

Features

  • Long-polling — no public IP, no webhook, no ngrok
  • Bidirectional — users message the bot → LLM processes → replies stream back
  • Streaming — progressively edits messages as the agent generates text
  • Access control — whitelist of allowed Telegram user IDs
  • Multi-turn history — per-user conversation context (up to 20 messages)
  • Message splitting — auto-splits responses > 4096 chars
  • Typing indicator — shows "typing..." while processing
  • Photo & document support — sends captions to the LLM
  • Proxy support — HTTP/SOCKS5
  • Graceful shutdown — handles SIGTERM/SIGINT cleanly

Quick Start

1. Install

pip install tg-gateway

2. Create a Telegram Bot

Talk to @BotFather on Telegram and create a new bot. Save the token.

3. Configure

tg-gateway-bot --config config.json

Or create config.json:

{
  "bot_token": "123456:ABCdef...",
  "allow_from": ["your_telegram_user_id"],
  "llm_api_url": "https://api.openai.com/v1/chat/completions",
  "llm_api_key": "sk-...",
  "llm_model": "gpt-4o-mini"
}

How to get your Telegram user ID? Message @userinfobot on Telegram.

4. Run

tg-gateway

Or with a custom config path:

tg-gateway-bot --config /path/to/config.json

5. Message Your Bot

Open Telegram, find your bot, and send it a message! 🎉

Environment Variables

All config fields can be overridden via environment variables:

Variable Description
TG_BOT_TOKEN Telegram Bot API token
TG_ALLOW_FROM Comma-separated allowed user IDs
TG_PROXY Proxy URL (e.g. socks5://host:port)
TG_STREAMING Enable/disable streaming (true/false)
TG_USE_MARKDOWN_V2 Use MarkdownV2 formatting
TG_DEBUG Enable debug logging

Configuration

Field Default Description
base_url https://api.telegram.org Custom Bot API URL (self-hosted)
streaming true Progressive message editing
typing_indicator true Show typing while processing
max_message_length 4096 Max chars per message
use_markdown_v2 false MarkdownV2 parse mode
system_prompt (see config example) LLM system prompt
max_history 20 Multi-turn context window

Deployment

For 24/7 operation, run on a VPS or Raspberry Pi:

nohup tg-gateway-bot > gateway.log 2>&1 &

Or use systemd / supervisor / screen / tmux.

Python API

import asyncio
from tg_gateway import TelegramGateway, load_config

async def main():
    cfg = load_config("config.json")
    gateway = TelegramGateway(cfg)
    await gateway.start()

asyncio.run(main())

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tg_gateway_bot-0.2.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tg_gateway_bot-0.2.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file tg_gateway_bot-0.2.0.tar.gz.

File metadata

  • Download URL: tg_gateway_bot-0.2.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for tg_gateway_bot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7f5dd98cb9b0f7efe608fb24b0af370f5481c8e8ca69e52f14b1078412230f29
MD5 cd54dfd71392cedcb6de44695f72c231
BLAKE2b-256 7dd33a77273744634ef57c2193b5217701d662b0f20c3205480e0f993897b9d3

See more details on using hashes here.

File details

Details for the file tg_gateway_bot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tg_gateway_bot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for tg_gateway_bot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 813d13843b12b2c49216b25a62175c49d97593fa693b2aab4ada9f35a5adaa00
MD5 21672ca3a8f40b70feaf98eb8547c81c
BLAKE2b-256 99b07e9751d20328d8358647190fad4a1f4c662187bd375b92aba27390d584b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page