A small, dependency-free Telegram bot that sends chat requests to an OpenAI-compatible API. It supports configurable models, bounded conversation history, long polling, retries, and Telegram-safe message splitting.
APIMART · multi-model AI API gateway
A runnable Telegram bot using one OpenAI-compatible API endpoint. Run a Telegram AI bot with one multi-model API key
Transparent disclosure: this repository is maintained by APIMART.
中文说明:README.zh-CN.md
- Pure Python standard library; no framework dependency
- OpenAI-compatible
/chat/completions /modelshows the active model/resetclears the current chat history- Per-chat history with a configurable message limit
- Retry with exponential backoff for transient API errors
- Splits replies to stay within Telegram's message limit
Requirements: Python 3.10+
cp .env.example .env
# Set TELEGRAM_BOT_TOKEN and APIMART_API_KEY in your shell.
python bot.pyPowerShell example:
$env:TELEGRAM_BOT_TOKEN = "123456:telegram-token"
$env:APIMART_API_KEY = "your-api-key"
$env:AI_MODEL = "gpt-4o-mini"
python .\bot.pyThe default API endpoint is https://api.apimart.ai/v1. Set AI_API_BASE_URL to use another OpenAI-compatible gateway.
/start— usage help/model— show the configured model/reset— clear this chat's history- Any other text — send it to the model
python -m unittest discover -s tests -vThe tests do not call Telegram or an AI API.
- Store tokens in a secret manager; never commit them.
- Use a dedicated Telegram bot token.
- Add allow-lists or authentication if the bot is private.
- Add persistent storage if conversations must survive restarts.
- Review rate limits and model pricing before opening the bot publicly.
MIT