Telegram AI Bot in 20 Lines of Python, Hosted for Free
Description and use
A Telegram bot powered by an LLM, written in 20 lines of Python, deployed for free on Deployka, with secrets managed via environment variables and costing under $0.01 per reply.
Problem
Deploying a Telegram bot that uses an LLM requires a bot token, an API key, and a persistent runtime, which can be costly or complex.
Outcome
The bot runs continuously for up to 30 days on Deployka’s free tier, costing less than $0.01 per reply on a cheap model, and can be scaled or switched to more powerful models via a single environment variable.
Workflow
- Create a Telegram bot via @botfather and copy the token
- Obtain an API key from an OpenAI‑compatible provider (e.g., Model.sale)
- Write a 20‑line Python script using aiogram and openai SDK
- Store BOT_TOKEN, MODEL_SALE_API_KEY, and MODEL as environment variables
- Deploy the code to Deployka (or any VPS) with the three variables
- The bot starts long‑polling and replies to messages using the chosen LLM
Evidence
A Telegram bot that answers with a real LLM needs three things: a bot token, an API key and a place where the code runs all the time.
Because the client only needs a base URL, switching models is one environment variable: gpt-6.1-terra for cheap everyday answers, claude-sonnet-5-5 or claude-opus-5-5 when the questions get hard.
The free plan needs no card and keeps a small bot like this running for up to 30 days (the slot is reclaimed after 7 idle days); the paid one is $1.09 per 30 days.
A short exchange is about 300 input and 300 output tokens: … So $5 covers tens of thousands of replies on a small model.
- Record date: 2026-11-02T02:29:04.741Z