DEV Community

Cover image for Telegram bot development: route group topics to bots and gate who can give orders
MatIntellect
MatIntellect

Posted on Edited on Originally published at matintellect.com AI-assisted

Telegram bot development: route group topics to bots and gate who can give orders

Problem: when several AI bots share one Telegram group, they answer in each other's threads and accept instructions from anyone in the group. Result: a small router that maps forum topics to bot roles, ignores everything outside those topics, refuses senders who are not approved, and a helper that builds decision cards with inline buttons. Stack: Python 3.10+ standard library (tested on 3.15.2); the dict shapes follow the Telegram Bot API fields message_thread_id and inline_keyboard. Boundaries: no network calls; plug the functions into whatever bot library you use

AI-generated illustration of Telegram bot development with topics

The setup this comes from

I run a Telegram group called "Factory" with topics for ideas, posts, design, copywriting, marketing, video, publishing and an archive. A team of 8 AI agents works there, each in its own topic, and they listen only to approved people. The marketer posts a brief each morning built from 20 live questions and 10 topics; over three months that made 80 daily briefs, and the designer drew 94 images, each after my yes. The content factory sends topic cards with Interesting and Skip buttons; 84 posts have gone out, none without approval

This article is a minimal, standalone version of two ideas from that setup: one bot per topic, and orders only from approved people. It is not the agents' production code

Why one bot per topic

Bots that answer everywhere quickly become noise. A bot that lives in its own topic does one job and stays out of other conversations, and people learn where to go for what. It also keeps permissions simple: the designer bot never needs to know what happens in the publishing topic, so a mistake in one role does not spread to the others

Input, processing, output

Input: a simplified update with the topic id and sender id; a table of topic to bot role; a set of approved user ids

Processing: if the topic is not a bot topic, ignore; if the sender is not approved, refuse; otherwise return the role that should handle it. Separately, build a message with one row of inline buttons

Output: a role and an action, or a reason to ignore; and a card ready to send

Working in a messenger

The example

User and topic ids are invented

APPROVED = {111, 222}          # user ids allowed to give instructions
BOTS = {101: "ideas", 102: "posts", 103: "design"}   # topic id -> bot role

def route(update: dict):
    """Decide which bot handles a group message, or ignore it."""
    topic = update.get("message_thread_id")
    role = BOTS.get(topic)
    if role is None:
        return None, "not a bot topic"
    if update["from_id"] not in APPROVED:
        return None, f"{role}: sender not approved"
    return role, "handle"

def card(text: str, buttons: list[str]) -> dict:
    """Inline keyboard: one row, callback data equals the button label."""
    return {"text": text, "reply_markup": {"inline_keyboard": [[{"text": b, "callback_data": b.lower()} for b in buttons]]}}

if __name__ == "__main__":
    for u in [{"message_thread_id": 101, "from_id": 111},
              {"message_thread_id": 103, "from_id": 999},
              {"message_thread_id": 7, "from_id": 111}]:
        print(u, "->", route(u))
    print(card("New topic: 5 social media mistakes", ["Interesting", "Skip"]))
Enter fullscreen mode Exit fullscreen mode

Output, exactly as printed when I ran it:

{'message_thread_id': 101, 'from_id': 111} -> ('ideas', 'handle')
{'message_thread_id': 103, 'from_id': 999} -> (None, 'design: sender not approved')
{'message_thread_id': 7, 'from_id': 111} -> (None, 'not a bot topic')
{'text': 'New topic: 5 social media mistakes', 'reply_markup': {'inline_keyboard': [[{'text': 'Interesting', 'callback_data': 'interesting'}, {'text': 'Skip', 'callback_data': 'skip'}]]}}
Enter fullscreen mode Exit fullscreen mode

Design notes

Topic first, sender second. Ignoring non-bot topics before checking the sender means ordinary team chat never reaches any bot, approved or not

Refuse quietly or loudly, but refuse. In my setup an unapproved request is ignored. Some teams prefer a short reply explaining who can give instructions; either is fine as long as nothing runs

Cards over free text for decisions. Approve, Rewrite and Skip as buttons make a decision take a second. callback_data should be short and stable, because your handler matches on it

Privacy mode matters. By default, bots in groups do not receive every message; that is a separate bot setting. If your router never sees updates, check that first

Topics are named after work, not people. When someone goes on leave, the work and its history stay in place

A team at work

Keep the tables in config, not code. Topic ids and approved users change when the team changes. Loading them from a small config file lets you add a topic or a person without a redeploy, and makes it easy to review who can give orders to which bot

Log refusals. A refused request from an unknown sender is cheap to record and useful later: it shows whether someone needs access or whether something is probing your bots

Telegram limits to plan around

A bot cannot start a conversation with someone who never wrote to it. Customers living in Instagram or WhatsApp will not move to Telegram for your convenience, so team work in Telegram and customer chat often run in different channels and need a bridge

Reproduce it

  1. Save the code as topics.py and run python3 topics.py
  2. Compare with the output above
  3. Add 999 to APPROVED and confirm the second update is handled by design
  4. Wire route() into your bot's update handler and card() into its send call

A team of agents with topics, roles and approval rules takes me about 2–3 weeks to set up. The non-technical version: Telegram bot development: business work in topics

This article was created with the help of AI.

Top comments (0)