caseslead-radar

rudiscuss your taskmenuclose

Finding requests in Telegram groups · 

A database for finding requests in Telegram: 327,110 messages from 74 chats in 12 days

The system reads public Telegram groups around the clock and finds messages where someone is looking for a contractor, such as “need a website” or “who can build a bot.” What it finds arrives as a card in a Telegram bot. Everything the system sees and decides is stored in one PostgreSQL database on a small server. I designed that database so that no message is lost or processed twice, jobs survive restarts, and the data survives updates and moving to a new server.

327,110 Messages in 12 days September 2 to 14, 2026, from 74 chats

99.3% Filtered out before the AI the AI checks only what looks like a request; measured on September 12, 2026

138 Requests sent to the bot as cards in a Telegram bot, September 2 to 27, 2026

A database summary: PostgreSQL 16, 33 tables, 14 migrations, queues without Redis, a backup every night, and updates with rollback; next to it, 327,110 messages in 12 days of September 2026

The task

Requests for services often appear in Telegram groups rather than on websites: someone writes “looking for a developer,” and an hour later the message is buried in the feed. Nobody can read dozens of groups by hand. The system does it instead: it reads the groups, filters out the noise, shows the AI only what looks like a request, and sends what it finds to a bot with Take, Junk, and Later buttons.

That is a stream of tens of thousands of messages a day, and all of it rests on the database. The requirements were:

  • no message is lost or processed twice, or the client gets the same request twice;
  • jobs survive a server reboot and a crash of any process;
  • the database fits on a small server, a board with 4 GB of memory that also runs other services;
  • the data can be backed up, moved to another server, and restored.

What it delivered

Messages, job queues, and requests live in one database that blocks duplicates itself and survives crashes, and it saves a full archive every night. Each request reaches the bot once, and the installation can be restored or moved to another server with one command. The numbers and periods are in the “Results” section.

What I did

I put everything in one database, with no Redis and no extra services

The database has 33 tables: sources, messages, AI verdicts, requests, client settings, statistics, and job queues. The queues live right inside PostgreSQL: a worker takes a job in a way that guarantees no second worker takes the same one. No separate queue server was needed, and on a small server every extra service means more memory and one more thing that can fail.

The database schema: 33 tables in seven groups, with sources, messages, the AI filter, and leads following a message from left to right, and job queues, client settings, and tracking and reports below them
The database schema, drawn from the project code: 33 tables, with a message moving from left to right.

I made sure the database itself blocks duplicates

A message is stored once: the pair “chat + message number” is unique at the database level. If the author edits a message, the same row is updated, so the client doesn’t get a second request. One ad posted to several chats becomes one request with a list of every chat where it appeared. The database also refuses a repeated post from Threads.

Jobs aren’t lost when something crashes

Every job has a lease. If a worker crashes mid-job, the job goes back to the queue on its own, and the next worker picks it up. The number of attempts is capped so a broken job doesn’t spin forever, and a separate pass brings stuck jobs back into play. The AI request runs outside an open transaction, so no database connection is tied up while the model thinks.

The database itself enforces the data rules

Allowed statuses and values are locked in by constraints in the tables themselves, links between tables by foreign keys, and the database stamps the time a row changes. The client profile is exactly one row, and the database guarantees that too, not an agreement in the code. If the program makes a mistake, the database refuses to store bad data instead of saving it silently.

Old data goes, statistics stay

Messages older than 90 days are deleted automatically every six hours, so the database doesn’t keep growing. Meanwhile every step of a message’s path, from read to filtered to checked by the AI to sent, is written to a separate table that the cleanup doesn’t touch. It shows which chats bring requests, over any period.

I sped up queries, based on measurements of the working database

Using the database’s own statistics, I found two indexes that had never been used but were updated on every write, and removed them. Looking up an author by @username read the whole messages table, so I added an index for that query, and now the row is found right away.

A backup every night, and a check that it restores

Every night at 3:00 the system makes a full archive of the installation: the database dump, the settings, and the encryption keys. Next to it, it records how many rows the main tables hold. A separate command restores the database from the archive in a clean container and checks those numbers: a backup that has never been restored guarantees nothing.

If the data disk isn’t mounted after a reboot, the system won’t start, so it never creates an empty database on top of the real one.

Updates and moves with one command

An update starts with a full backup. If any step after that fails, the system rolls itself back: the previous version of the program and the database from that same backup. Installing on a new server and restoring from an archive take one command each too.

The new version went live on a fresh database on September 2, 2026. The Telegram account logins were moved over without a new SMS confirmation, and 48 chats were restored from the accounts’ dialog lists, without a single repeat join that Telegram might treat as suspicious.

Fits on a small server

The database runs on a board with 4 GB of memory alongside other services. Postgres gets no more than 200 MB of memory, the number of connections is sized for every process in the system, and unneeded buffers are trimmed. Bot tokens and Telegram account logins are stored in the database encrypted.

Covered by automated tests

The project has more than 4,000 automated tests. The ones that work with the database use a separate PostgreSQL test database, with a safeguard that stops a test from writing data to the working database by accident.

Results

Counted from September 2, 2026, when the new version went live on a fresh database.

How the system ran from September 2 to 27, 2026
WhatHow many
Chats the system read74, by September 14
Messages collected327,110 in 12 days, September 2 to 14
Messages a dayAbout 41,000 by the end of that period
Filtered out before the AI99.3%, by age, length, stop words, and key phrases; measured on September 12
AI checks a weekAbout 3,000, within the models’ free limits; the week up to September 14
Requests sent to the bot138, as cards in the Telegram bot, September 2 to 27

All of it on one database: 33 tables and 14 migrations.

The numbers come from the system’s own database: a snapshot from September 14, 2026, requests as of September 27, and the share filtered out, measured on September 12. They are counted from September 2, when the new version went live on a fresh database.

What this gives the business going forward

I need a database for my business’s requests. Will this work?

The approach is the same: requests from chats, the website, and messengers go into one database with no duplicates, with a report on where they come from and what happened to them. For a specific business’s task, there will be fewer tables, but the principles are the same.

Where is the data stored?

On the client’s server: one installation, one client, one server. Different clients’ data never mixes.

What if the server reboots or breaks?

After a reboot the system starts by itself and carries on where it stopped: unfinished jobs go back to the queue. If the disk breaks, the installation is restored from the nightly archive with one command.

How it works technically

PostgreSQL 16 in Docker on an Orange Pi 4 LTS ARM board; Python 3.12, SQLAlchemy 2 (async), and asyncpg; migrations with Alembic, 14 of them. The first is a baseline schema built from eight earlier revisions when the project moved to “one installation per client,” so a new installation never passes through intermediate tables.

The queues ai_jobs, delivery_jobs, and operator_notification_jobs: jobs are claimed with FOR UPDATE SKIP LOCKED, with a lease_expires_at lease, a max_attempts cap, a reaper for stuck jobs, and a revival counter for jobs that failed for good. Message uniqueness is UNIQUE (chat_id, tg_message_id); a lead links to every source chat through lead_sources; one decision row per lead and person uses uniqueness with NULLS NOT DISTINCT. Races are closed with advisory locks: pg_try_advisory_lock against parallel runs of a command, pg_advisory_xact_lock against a double tap on a button.

CHECK constraints are built from the value dictionaries in the code, and a test compares them with the database; triggers set updated_at and the profile version; the profile is one row with CHECK (id = 1). Retention runs every 6 hours with a 90-day window; funnel_events is never cleaned and feeds the reports. Indexes were chosen with pg_stat_user_indexes and EXPLAIN (ANALYZE, BUFFERS) on the working database: two unused ones were dropped, and username lookups got a partial functional index on lower(author_username), posted_at DESC.

The backup_all.sh backup at 03:00: pg_dump, the installation profile and keys, and a manifest with row counts for the five main tables; verify_backup.sh restores the dump in a throwaway postgres:16-alpine container and compares the counts. update.sh takes a full backup before updating and, if any step after it fails — from git pull to the post-start check — calls rollback.sh: the previous image, the database from that backup, and the previous code commit. Limits for the board: max_connections=20, shared_buffers=32MB, and a container memory limit. Secrets in the database are encrypted (Fernet). Threads was added to the same tables through a chats.platform column: CHECK constraints keep each platform’s row shape, and rolling back the migration removes only the Threads data.

Who did the work

It’s just me behind jw-dev.pro, not a studio or an agency. That means you talk directly to the person doing the work: no manager, no brief relayed through third parties, and no passing the project to another contractor partway through.

My time zone is UTC+7, the same as Bangkok and Hanoi. I build websites, Telegram bots, automation, and databases: a task rarely ends with a page, and it usually has to be connected to what you already have running.

As time goes on, the task sounds like “build a website” less and less often. One person needs a price to be changed in one place, not on twenty pages. Another needs the channel to run without them. A third needs the whole price list to sit inside Telegram, where their customers already go every day. That’s why the list of what I do is open-ended: if your task isn’t on it, that doesn’t necessarily mean it can’t be solved.

I write case studies so that they can be checked: what was there, what was done, and how it was measured. If there is analytics data, I give the numbers and the period they cover; if it is too early to measure, I say so. You won’t find “increased sales” here: that is a promise, not a result.

Need something similar?

Whether it’s a website, a bot, or something that is now done by hand, tell me what’s going on, and I’ll tell you whether it can be solved or not.

This kind of work is the service “Custom web scraping: data from websites, feeds, and chats.”

I have your request

I’ll read it myself and reply where you told me to.

Send another one

What you need you can pick several

In your own words: what you have now and what you want to get. If you have a website or a bot, paste the link.

I’ll reply myself, in writing.

If it’s easier, contact me directly: on Telegram, on WhatsApp, or by email.

Not sure what’s wrong with your website? Check it for free on Telegram: @jw_site_check_bot.

Who owns everything afterward and what happens if I disappear: answers on the home page.