Telegram LLM chat with memory: telebot handlers → in-memory dialogue per chat_id → OpenAI-compatible API (OpenAI / ProxyAPI / GenAPI) → usage footer + stdout logs.
Clone → configure
.env→python bot.py→ message your bot →/resetorclear contextto wipe history.
Small Python bot for practicing multi-turn dialogue, provider switching, and generation parameters (temperature, max_tokens).
- Long polling via pyTelegramBotAPI
- Thread-safe in-RAM history (no database)
- Chat Completions through the official OpenAI Python client + custom
base_url - Request params and token usage logged to the console (secrets excluded)
| Provider | Env key | Default base URL |
|---|---|---|
| OpenAI | OPENAI_API_KEY |
https://api.openai.com/v1 |
| ProxyAPI | PROXYAPI_KEY |
https://openai.api.proxyapi.ru/v1 |
| GenAPI | GENAPI_KEY |
set API_BASE_URL manually |
Set API_PROVIDER to openai, genapi, or proxyapi. Explicit API_BASE_URL overrides the default.
| Feature | Description |
|---|---|
| Contextual chat | Stores user/assistant turns per Telegram chat_id |
/reset |
Clears history for the current chat |
| Text reset | clear context, reset, clear history |
| Multi-provider | API_PROVIDER + one of the API keys |
| Usage footer | prompt / completion / total tokens + rough USD estimate |
| Structured logs | Model, temperature, max_tokens, message count, user preview |
- Python 3.11+
- pyTelegramBotAPI — handlers, long polling
- openai — Chat Completions client
- python-dotenv —
.envconfiguration
contextBot/
├── bot.py # /start, /reset, chat handler, token footer
├── config.py # Settings, provider, keys, base URL
├── context_manager.py # In-memory store per chat_id
├── api_client.py # API calls + logging
├── requirements.txt
├── .env.example
└── README.md
git clone https://github.com/nifontovoleg/contextBot.git
cd contextBot
python -m venv .venv
# Windows PowerShell
.\.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activate
pip install -r requirements.txt
copy .env.example .env # Windows — use cp on UnixMinimal .env (ProxyAPI example):
BOT_TOKEN=your_telegram_bot_token
API_PROVIDER=proxyapi
PROXYAPI_KEY=your_proxyapi_key
API_MODEL=gpt-4o-mini
TEMPERATURE=0.7
MAX_TOKENS=512| Variable | Description |
|---|---|
BOT_TOKEN |
From @BotFather |
API_PROVIDER |
openai · genapi · proxyapi |
OPENAI_API_KEY / GENAPI_KEY / PROXYAPI_KEY |
One key is enough |
API_BASE_URL |
Optional override for the provider default |
API_MODEL |
Model id (ProxyAPI: e.g. anthropic/claude-…) |
TEMPERATURE, MAX_TOKENS |
Generation settings |
SYSTEM_PROMPT |
Optional system message |
Run:
python bot.pyIn Telegram: /start, then send messages. History is lost on bot restart (RAM only).
| Input | Action |
|---|---|
/start, /help |
Welcome and hints |
/reset |
Clear context for this chat |
clear context, reset, clear history |
Same as /reset |
| Any other text | LLM reply with prior turns |
Stdout includes:
- API request summary:
model,temperature,max_tokens,messages_count,last_user_preview - Usage:
prompt,completion,totaltokens - Exceptions from the API client and message handler
BOT_TOKEN and API keys are never printed.
Change API_MODEL, TEMPERATURE, and MAX_TOKENS in .env, restart, and rerun the same script. Fill the table from the reply footer or logs.
| Model | temperature | max_tokens | Run # | Effect (concise / creative) | Tokens (in / out / total) | Approx. cost |
|---|---|---|---|---|---|---|
| gpt-4o-mini | 0.3 | 256 | 1 | Very concise | — / — / — | ~$… |
| gpt-4o-mini | 0.7 | 512 | 2 | Balanced dialogue | — / — / — | ~$… |
| gpt-4o-mini | 1.0 | 512 | 3 | More creative / verbose | — / — / — | ~$… |
| gpt-4o | 0.7 | 512 | 4 | Stronger on hard prompts | — / — / — | ~$… |
| your model | T | N | k | notes | from logs | from footer |
OpenAI list prices (USD / 1M tokens): gpt-4o-mini $0.15 in / $0.60 out; gpt-4o $2.50 in / $10 out. ProxyAPI / GenAPI: use provider rates → in × price_in + out × price_out.
- “Explain RAG in two sentences.”
- “How does that relate to my Telegram bot?” (memory check)
- “Shorten your previous answer to one sentence.”
Run /reset before changing model or temperature between experiments.
Two bot processes share the same BOT_TOKEN. Stop extra instances, then start one python bot.py.
- Check the matching key for
API_PROVIDER - Confirm
API_BASE_URLandAPI_MODELfor ProxyAPI / GenAPI - Read the traceback in the terminal (not sent to Telegram)
| Goal | Outcome |
|---|---|
| Telegram bots | Handlers and long polling with telebot |
| LLM integration | OpenAI-compatible Chat Completions |
| Context | Multi-turn memory without a DB |
| Experiments | Compare temperature, max_tokens, and models |
MIT — free for personal and learning use.
Built with ☕ for Telegram bots and LLM context
@nifontovoleg · nifontovv.ru