DEV Community

brian austin
brian austin

Posted on

Build a Telegram bot powered by Claude AI for $2/month (full code)

Build a Telegram bot powered by Claude AI for $2/month (full code)

I've been paying $20/month for ChatGPT Plus to power a few internal Telegram bots. Last month I switched to SimplyLouie — a flat-rate Claude API wrapper at $2/month — and the bots still run fine.

Here's the full working code. Copy-paste and go.

What you'll need

Install dependencies

pip install python-telegram-bot httpx
Enter fullscreen mode Exit fullscreen mode

The full bot (60 lines)

import os
import httpx
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes

TELEGRAM_TOKEN = os.environ["TELEGRAM_TOKEN"]
SIMPLYLOUIE_API_KEY = os.environ["SIMPLYLOUIE_API_KEY"]
API_URL = "https://simplylouie.com/api/chat"

# Store conversation history per user
conversations = {}

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text(
        "Hi! I'm a Claude-powered bot. Ask me anything."
    )

async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_id = update.effective_user.id
    user_message = update.message.text

    # Initialize conversation history
    if user_id not in conversations:
        conversations[user_id] = []

    # Add user message to history
    conversations[user_id].append({
        "role": "user",
        "content": user_message
    })

    # Keep last 10 messages to avoid context bloat
    if len(conversations[user_id]) > 10:
        conversations[user_id] = conversations[user_id][-10:]

    # Call SimplyLouie API
    async with httpx.AsyncClient() as client:
        response = await client.post(
            API_URL,
            headers={
                "Authorization": f"Bearer {SIMPLYLOUIE_API_KEY}",
                "Content-Type": "application/json"
            },
            json={
                "messages": conversations[user_id],
                "system": "You are a helpful assistant. Be concise — this is a Telegram bot."
            },
            timeout=30.0
        )

        data = response.json()
        reply = data["content"][0]["text"]

    # Add assistant reply to history
    conversations[user_id].append({
        "role": "assistant",
        "content": reply
    })

    await update.message.reply_text(reply)

def main():
    app = Application.builder().token(TELEGRAM_TOKEN).build()
    app.add_handler(CommandHandler("start", start))
    app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, chat))
    app.run_polling()

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Run it

export TELEGRAM_TOKEN="your-bot-token-here"
export SIMPLYLOUIE_API_KEY="your-api-key-here"
python bot.py
Enter fullscreen mode Exit fullscreen mode

That's it. The bot is live.

What it does

  • Multi-turn conversations — each user gets their own conversation history
  • Context window management — keeps last 10 messages, drops older ones automatically
  • Async throughout — handles multiple users simultaneously without blocking
  • System prompt — tells the model it's a Telegram bot so answers stay concise

Deploy it for free (or near-free)

Railway (free tier):

# Push to GitHub, connect repo to Railway
# Add env vars in Railway dashboard
# Done — bot runs 24/7
Enter fullscreen mode Exit fullscreen mode

Fly.io (free tier):

fly launch
fly secrets set TELEGRAM_TOKEN=your-token SIMPLYLOUIE_API_KEY=your-key
fly deploy
Enter fullscreen mode Exit fullscreen mode

VPS (cheapest option for Nigeria/Philippines/Kenya/India):

# On a $4/month Hetzner or $5/month DigitalOcean VPS
nohup python bot.py &
Enter fullscreen mode Exit fullscreen mode

Add a custom system prompt for your use case

Change the system parameter to specialize the bot:

# Customer support bot
"system": "You are a support agent for AcmeCo. Only answer questions about our products. If you don't know, say so."

# Code review bot
"system": "You are a senior Python developer. Review code, identify bugs, suggest improvements. Be direct."

# Language learning bot (great for Tagalog/Swahili/Hausa/Urdu practice)
"system": "You are a language tutor. The user is learning Tagalog. Respond in English first, then give the Tagalog equivalent. Correct their mistakes gently."
Enter fullscreen mode Exit fullscreen mode

The math

Service Monthly cost API calls/month
ChatGPT Plus $20 Unlimited (Claude 3.5 Sonnet)
Anthropic direct Pay-per-token ~1,000 calls at $3-15
SimplyLouie $2 Unlimited

For a personal bot or small team bot: $2/month flat beats per-token billing almost every time.

For developers outside the US

This bot costs:

  • Nigeria: N3,200/month (vs N32,000+ for ChatGPT)
  • Philippines: P112/month (vs P1,120+ for ChatGPT)
  • Kenya: KSh260/month (vs KSh2,600+ for ChatGPT)
  • India: Rs165/month (vs Rs1,600+ for ChatGPT)
  • Indonesia: Rp32,000/month (vs Rp320,000+ for ChatGPT)

Same Claude model. Flat rate. No per-token billing anxiety.

What are you building?

Drop your use case in the comments — I'm curious what people are actually using Claude-powered bots for. Customer support? Language learning? WhatsApp/Telegram automation for local markets?

Full API docs: simplylouie.com/developers

Get your key (7-day free trial): simplylouie.com

Top comments (0)