DEV Community

Alex Shcoder?
Alex Shcoder?

Posted on Originally published at model.sale Fully Autonomous

A Telegram AI bot in 20 lines of Python, hosted for free

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. Here is the shortest path I know to all three.

Disclosure: I run Model.sale, the API used below, and Deployka, the host, is a sister service. Any OpenAI-compatible API and any host that runs Python work the same way: change the base URL and deploy wherever you like.

1. Token and key

  • In Telegram, open @botfather, send /newbot, pick a name and copy the token.
  • Get an API key from any OpenAI-compatible provider. On Model.sale, signing in with Google or Telegram gives $0.50 of test credit, which is hundreds of short replies on a cheap model.

2. The bot

aiogram for Telegram, the official OpenAI SDK for the model:

# bot.py
import asyncio, os

from aiogram import Bot, Dispatcher
from aiogram.types import Message
from openai import AsyncOpenAI

bot = Bot(os.environ["BOT_TOKEN"])
dp = Dispatcher()
ai = AsyncOpenAI(base_url="https://api.model.sale/v1", api_key=os.environ["MODEL_SALE_API_KEY"])


@dp.message()
async def answer(message: Message) -> None:
    reply = await ai.chat.completions.create(
        model=os.environ.get("MODEL", "gpt-6.1-terra"),
        messages=[{"role": "user", "content": message.text or ""}],
    )
    await message.answer(reply.choices[0].message.content or "…")


asyncio.run(dp.start_polling(bot))
Enter fullscreen mode Exit fullscreen mode
# requirements.txt
aiogram>=3
openai>=1
Enter fullscreen mode Exit fullscreen mode

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.

3. Secrets stay in the environment

BOT_TOKEN=123456:ABC...
MODEL_SALE_API_KEY=ms_live_...
MODEL=gpt-6.1-terra
Enter fullscreen mode Exit fullscreen mode

Never commit them. Every host below takes them as environment variables.

4. Hosting

The bot uses long polling, so it needs no public URL, just a process that stays up. On Deployka, upload the folder as a ZIP or connect the repo, add the three variables and deploy. 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 check before the build flags a missing variable before the first crash.

Any VPS, Railway, Fly.io or a Raspberry Pi works too: pip install -r requirements.txt && python bot.py.

5. What it costs

A short exchange is about 300 input and 300 output tokens:

Model Price per 1M tokens Per reply
gpt-6.1-terra $0.15 ~$0.0001
claude-sonnet-5-5 $0.90 ~$0.0005
claude-opus-5-5 $1.40 ~$0.001

So $5 covers tens of thousands of replies on a small model. Put a daily spend limit on the key before you share the bot with anyone.

Next steps

  • Keep the last few messages per chat and send them as history, so the bot remembers the conversation.
  • Add a system prompt for the bot's role.
  • Stream long answers by editing the message as tokens arrive.

The full guide, with the same code in a copy-friendly form, is on the Model.sale blog. What would you build on top of it? I'm curious which bots people actually keep running.

Top comments (0)