DEV Community

uDa4a100
uDa4a100

Posted on

I Got Tired of Writing Telegram AI Bots From Scratch — So I Built a Boilerplate

Every side project starts the same way: "I'll just write a quick Telegram bot." Two hours later you're still debugging long polling, fighting with database schemas, and reinventing message chunking.

If you've ever built a Telegram bot connected to an LLM, you know the routine:

  1. Set up long polling or webhooks
  2. Handle message length limits (4096 chars)
  3. Create a SQLite schema for chat history
  4. Wire up an OpenAI-compatible client
  5. Add error handling so the bot doesn't die at 3 AM

None of this is hard. All of it is boring. And you rewrite it every single time.

The Solution: AgentKit

I packaged everything into a minimal Python boilerplate that gets you from git clone to working AI assistant in about 5 minutes.

GitHub: https://github.com/uDa4a100/agent-kit

The core design decisions:

1. Zero heavy dependencies

No aiogram, no python-telegram-bot, no openai SDK. Just urllib from the standard library and python-dotenv. Why? Because bot frameworks come and go, but the Telegram Bot API is just HTTP POST requests. Here's the entire API wrapper:

def _api(method, params):
    url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
    data = urllib.parse.urlencode(params).encode()
    req = urllib.request.Request(url, data=data)
    try:
        with urllib.request.urlopen(req, timeout=40) as r:
            return json.loads(r.read().decode("utf-8"))
    except Exception as e:
        LOG.error("Telegram API error: %s", e)
        return None
Enter fullscreen mode Exit fullscreen mode

That's it. Long polling is just calling getUpdates in a loop with a timeout parameter.

2. Any OpenAI-compatible backend

The config is one environment variable:

AI_BASE_URL=http://localhost:11434/v1   # Ollama
# or
AI_BASE_URL=https://openrouter.ai/api/v1  # OpenRouter free tier
Enter fullscreen mode Exit fullscreen mode

Swap backends without touching code. Run a local model for privacy, or point it at a cloud API for power.

3. Message history in SQLite from day one

Every conversation is stored with timestamps:

CREATE TABLE IF NOT EXISTS messages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    chat_id INTEGER NOT NULL,
    role TEXT NOT NULL,
    text TEXT NOT NULL,
    ts TEXT NOT NULL
);
Enter fullscreen mode Exit fullscreen mode

This matters more than people think. Once your messages are in SQLite, you get full-text search, analytics, and context injection for free later.

4. Non-blocking message handling

Each incoming message spawns its own thread:

threading.Thread(
    target=_handle,
    args=(chat_id, msg.get("text", "")),
    daemon=True,
).start()
Enter fullscreen mode Exit fullscreen mode

A slow LLM response doesn't block other users' messages.

Getting Started

git clone https://github.com/uDa4a100/agent-kit.git
cd agent-kit
cp .env.example .env
pip install -r requirements.txt
python main.py
Enter fullscreen mode Exit fullscreen mode

Edit .env with your bot token from @botfather, pick an AI backend, done.

What's Next for This Project

The roadmap includes task scheduling (/add weather min:60 "check forecast"), FTS5-powered history search, and multi-user admin controls.


Links:

What would you add to a personal AI assistant? Comments welcome.

Top comments (0)