DEV Community

Apex
Apex

Posted on • Originally published at apexnexus.site

How to Build a Telegram AI Bot That Summarizes Links for Free (Full Code)

The Problem Nobody Warns You About Group chats are where information goes to die. Someone drops a 3,000-word article, everyone says "saving this for later", and nobody ever reads it. I got tired of it, so I built a Telegram bot that does one thing well: you send it a link, it replies with a clean summary. No webhooks from third-party services, no paid API keys, no server. Here's the full code and the exact steps, all free. ## What You Need - A Telegram account and the @botfather bot to create your bot token (2 minutes)

  • Python 3.9+
  • A free Groq API key (they have a generous free tier for LLM calls: I used their small model for this because summaries don't need a flagship)
  • python-telegram-bot and httpx (or requests) ## Step 1: Create the Bot Open @botfather in Telegram, send /newbot, pick a name and username. You get a token that looks like: 7123456789:AAHf0xJ9kLmNopQrStUvWxYz Keep it secret. It's the key to your bot. ## Step 2: The Summarizer The core is simple: fetch the link, strip the HTML, truncate, and ask the LLM to summarize. I use Groq's free tier because it's fast and the free limit is generous. python import httpx GROQ_KEY = "your-groq-key" MODEL = "llama-3.1-8b-instant" # small + fast, plenty for summaries def summarize(url: str) -> str: # Fetch the page html = httpx.get(url, follow_redirects=True, timeout=30).text # Crude but effective HTML stripping import re text = re.sub(r"<script.*?</script>|<style.*?</style>", "", html, flags=re.S) text = re.sub(r"<[^>]+>", " ", text) text = re.sub(r"\s+", " ", text).strip()[:4000] resp = httpx.post( "https://api.groq.com/openai/v1/chat/completions", headers={"Authorization": f"Bearer {GROQ_KEY}"}, json={ "model": MODEL, "messages": [ {"role": "system", "content": "Summarize the article in 4 bullet points. Plain language, no filler."}, {"role": "user", "content": text}, ], "temperature": 0.3, }, timeout=60, ) return resp.json()["choices"][0]["message"]["content"] That's the whole intelligence layer. Four bullets, plain language, done. ## Step 3: Wire It Into Telegram python from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, filters TOKEN = "your-bot-token" async def start(update: Update, _): await update.message.reply_text("Send me any link and I'll summarize it.") async def handle(update: Update, _): url = update.message.text.strip() if not url.startswith(("http://", "https://")): await update.message.reply_text("That doesn't look like a link. Send me a URL.") return await update.message.reply_text("Summarizing.") try: await update.message.reply_text(await summarize(url)) except Exception as e: await update.message.reply_text(f"Failed: {e}") app = Application.builder().token(TOKEN).build() app.add_handler(CommandHandler("start", start)) app.add_handler(MessageHandler(filters.TEXT, handle)) print("Bot running.") app.run_polling() ## Step 4: Run It (Two Options) Option A: your laptop. python bot.py and it works while the terminal is open. Option B: free forever. Deploy to a free tier that supports long-running processes (a free cloud VM or a free PaaS with a sleep-free plan). Polling means no webhook config needed, just a process that stays alive. ## The Gotchas I Hit 1. HTML stripping is never perfect. Some pages are JS-rendered and return almost nothing. For those, I fall back to the page's meta description, which is better than nothing.
  • Rate limits are real. Groq's free tier allows a few thousand requests per day, which is fine for a group bot. If you exceed it, add a tiny delay or cache repeated links in a dict.
  • Don't let the bot reply in huge threads. Summaries should be short. If a page is enormous, the 4,000-char truncation keeps responses snappy.
  • Never log the full URL contents. Some links contain private data. Summarize, don't store. ## Make It Yours Once the basics work, the upgrades are obvious: summarize YouTube transcripts, accept article text pasted directly, or have it tag the summary with a category. Each one is a few lines on top of this core. The pattern here is the same one I use across my whole automation stack: fetch, transform, deliver. Boring, reliable, free. If you want more patterns like this, the free guides on my site cover news bots, automation stacks, and prompt systems that run for $0. --- This bot is a 30-line version of the kind of pipeline I document in full on the free Apex Nexus learning hub. Built with the same philosophy: cheap models, simple code, no unnecessary moving parts.

🌐 More free guides: apexnexus.site - the free AI Nexus learning hub (guides, cheat sheets, roadmaps)

Enjoying this guide? Support the free AI Nexus learning hub: buy us a coffee

💬 Join the AI Nexus Discord (free community for AI automation learners): https://discord.gg/E5vuXxRtu9

🚀 Membership is coming soon - free guides + Discord stay open until then.

Top comments (0)