DEV Community

tteerr
tteerr

Posted on

Building an Asynchronous Python Telegram Bot for Automated AI Chat Moderation Using ChatGPT

If you manage a large group chat or a gaming server community, you already know that traditional, rule-based moderation bots are fundamentally broken. They fail completely at detecting sarcasm, hidden toxic behavior, and coordinated flame wars because they only scan for explicit banned keywords.To fix this, a robust solution was designed by Thr0n38: an asynchronous Python bot driven by ChatGPT via the OpenAI API that analyzes the actual context of a conversation in real time and automatically enforces short cool-down restrictions on disruptive users.Here is a practical guide and a basic logic architecture to build this system yourself.The Logic and SetupThe bot functions as a silent gatekeeper. It intercepts every text message sent to the group chat, passes it to the AI for evaluation, and immediately handles the output.First, you need to set up a standard project environment and install the necessary asynchronous dependencies to handle Telegram webhooks and OpenAI API communication. Next, you need to configure the connection using a Telegram bot token from BotFather and your unique OpenAI secret API key from the developer dashboard.Core Architecture and Prompt EngineeringThe system relies on monitoring chat events and evaluating text context in the background. The main logic is built around prompt engineering. You must pass a strict instruction to the model behind the scenes, forcing it to act as an impartial chat compliance officer.The instruction tells ChatGPT to evaluate the user text for personal conflicts, heavy toxicity, or harassment. Instead of letting the AI write a long text response, the instruction explicitly commands it to reply with exactly one word: BAN if the message is toxic, or OK if the message is safe.Auto-Mute and Clean-Up ActionsIf ChatGPT evaluates a message and returns the word BAN, the system instantly triggers two consecutive actions using native Telegram group management features:First, it executes a message deletion command. The bot removes the toxic text from the chat history so other users do not see it. In most cases, this happens under 400 milliseconds, keeping the chat clean.Second, it restricts the chat member. Instead of banning the user from the group completely, the bot applies a temporary restriction, stripping away their permission to send messages for a short duration, like 15 minutes. This automatic time-out breaks the rhythm of an argument and forces angry users to take a breather without destroying your active member count.Production TipsThe key to keeping API operational latency low and costs at near-zero is the strict system prompt constraint. By forcing the model to choose strictly between two words, you save massive amounts of compute tokens and simplify production runtime execution.Once deployed on any basic cloud hosting infrastructure, ensure the bot is added to your target Telegram group and granted administrative rights to delete messages and restrict members. Without these admin rights, the bot will read the chat but will not be able to enforce any mutes.

Top comments (0)