Telegram AI Bot in 20 Lines of Python, Hosted for Free
说明与用途
使用20行Python代码创建的Telegram AI机器人,免费部署在Deployka上,凭证通过环境变量管理,单条回复成本低于$0.01。
问题
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.
结果
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.
工作流
- 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
证据
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.
- 记录日期: 2026-11-02T02:29:04.741Z