gora.
AI / communication automation

Telegram auto-reply: AI answers for you 24/7

Worker listens to your DMs, AI replies by rules, Admin Bot governs, AI moderator scans your channel and kills spam. Five services on one Postgres.

Region
VPS3 (Poland) · personal use
Role
Solo: architecture, 5 services, AI moderation
Period
2024–2026
Industry
AI / communication automation

This is a personal tool. Contact names and message bodies are not published. Architecture and stack are described without revealing individual users.

Summary. Below — why five services instead of one, how the AI moderator learns, and where the complexity is hidden.

Why five services. A monolith is simpler but bad at partial failure. Worker dies → Collector keeps recording history, Admin Bot stays alive, state is still visible via Telegram. Moderator dies → auto-reply still works. Core API falls → the others aren't affected. Each service owns its systemd unit, its log, its restart policy.

Worker — brain of the auto-reply. A Telethon session on behalf of the user (not a bot) listens to every incoming DM. Each message triggers a cascade: auto-reply globally enabled? contact in the Personal folder or covered by a rule? reply mode not off? daily limit not exhausted? minimum interval since the last reply elapsed? If every check passes — the reply is generated via AI (Claude → Grok → Local fallback) and sent through the same Telethon session. If AI fails entirely, a template is sent.

Admin Bot — the console. An aiogram bot the owner uses for everything: toggle auto-reply, add/remove rules per contact, view conversation history, edit the prompt, change limits, switch mode. From the admin bot you can restart the worker (systemctl restart worker) — no SSH needed.

Moderator — the self-learning channel moderator. The most interesting piece. The /scan command reads the last 500 channel posts via Telethon. AI (Claude) analyses and produces a summary: what the channel is about, the tone, what's on and off-topic. The summary lives in the DB. When a new message appears in the discussion group, AI compares it against the summary → outputs OK / SPAM / HATE / OFFTOPIC. SPAM / HATE / OFFTOPIC are auto-deleted; OK stays.

Without /scan the moderator runs on a default "general moderation" prompt; after /scan it adapts to the specific channel: on a fishing channel, talk about tax strategies is OFFTOPIC; on an investing channel — OK.

Antiflood. Tracked in memory, not in DB (faster). Each user_id → list of recent timestamps. If more than M messages in N seconds → mute for K minutes. Limits configurable via Admin Bot.

Database. PostgreSQL 14, database ai_tg_core, user aiuser. Seven tables: peers (contacts with in_personal flag), messages (history), auto_reply_rules (per-contact rules), auto_reply_state (reply state), settings, reply_counts, chat_triggers (per-chat moderator triggers).

Open source. Repository: https://github.com/georgegoldman48-svg/tg-auto-reply — public, full architecture documented in README.md.

What's inside

  • Five services in one codebase: Worker (v2.13), Admin Bot (v3.5), Collector (v1.0), Core API (v1.0), Moderator (v1.0). Each one is its own systemd unit.
  • Worker listens to every incoming DM via a user Telethon session. AI replies by rules: contact is in the Personal folder or has an individual rule → reply; otherwise ignore.
  • Layered pre-reply checks: global toggle, per-contact mode, daily limit, new-contact limit, interval since the last reply.
  • Self-learning AI channel moderator: `/scan` reads the last 500 posts via Telethon, AI generates a summary (topic, tone, what's on/off-topic), and when a new message arrives in the discussion group it's classified as OK / SPAM / HATE / OFFTOPIC and auto-deleted.
  • Three moderator modes: moderate_only (delete only), chat (AI replies on owner's behalf as a persona), both (moderate + chat).
  • Antiflood in memory: user_id → timestamps tracking, configurable limits, auto-mute on overflow.
  • PostgreSQL backend (`ai_tg_core` database): peers, messages, auto_reply_rules, auto_reply_state, settings, reply_counts, chat_triggers.
  • Multi-AI fallback: if Claude fails, the bot tries Grok, then a local model. No reply is lost on a single provider's outage.
  • Core API (FastAPI on port 8000) — for external integrations: poll state from webhooks or an admin dashboard.
  • Settings API on 8085 — separate endpoint for hot-reconfiguring the worker without a restart.

Stack

  • Telethon (user-session listener)
  • aiogram 3 (admin bot)
  • FastAPI (core REST API)
  • PostgreSQL 14
  • Anthropic Claude SDK
  • xAI Grok API
  • Local AI fallback
  • systemd × 5 units
  • Python 3.12 + venv
Three narratives

One project — three angles

    Telegram auto-reply: AI answers for you 24/7 · hiregora.com