bot_warnings — Telegram notifications for Wildberries warnings

A pair of Telegram bots built in Python on a common engine: they monitor Wildberries warnings in PostgreSQL and automatically send new ones to subscribers — one for FBO|FBS warnings, the other for fines and measurements.

bot_warnings — Telegram notifications for Wildberries warnings — фото 1
click to view full size

Slide 1 of 3

DESCRIPTION

A notification system for marketplace warnings, implemented as a single engine in two instances with different filters: bot_warnings selects records marked as 'FBO | FBS' (excluding fines and measurements), while bot_warnings_dimensions selects records starting with 'Fine' or 'Measurement'. Each instance operates on its own service table (bots.bot_warnings and bots.bot_warnings_dimensions). The bots are built on pure requests with their own long polling getUpdates loop; access to PostgreSQL is via psycopg2 with DictCursor and configured TCP keepalive.

Architecture and data source

The architecture is multithreaded: a background daemon thread polls the database, while the main thread processes commands (/start, /stop, /warnings). Subscribers are stored in memory in a set under threading.Lock. The data source is the parser.warnings table; the selection for the current day takes into account the time zone (date filter with created_at converted to Europe/Moscow) and is deduplicated by a composite key (WB article, seller article, warning text, barcode).

Duplicate-send protection

Protection against resending is implemented through a dedicated table: new records are compared with those already sent, saved in batches (execute_values with ON CONFLICT DO NOTHING), and cleared at midnight Moscow time (pytz) with protection against repeated clearing in the same minute. Upon startup, the bot updates the schema to the current state: idempotent migration through anonymous DO $$ blocks adds missing columns, removes outdated UNIQUE constraints, and creates the current state by checking metadata in information_schema and pg_constraint.

TOOLS USED

  • Python
  • Telegram Bot API
  • requests
  • PostgreSQL
  • psycopg2
  • SQL (DO blocks)
  • information_schema/pg_constraint
  • pytz
  • threading
  • Git

RESULT

  • A unified WB warning notification engine has been built, deployed in two instances with different filters (FBO|FBS and fines/measurements).
  • Warning selection for the current day has been implemented, considering the time zone and deduplication by a composite key.
  • A multithreaded scheme has been established: a background thread polls the database, while the main one processes commands; subscribers are stored in a thread-safe manner.
  • A log of sent messages in the database has been implemented with batch insertion (ON CONFLICT DO NOTHING) and auto-cleaning at midnight Moscow time.
  • Idempotent self-migration of the schema has been implemented through DO blocks and analysis of information_schema/pg_constraint.

AI-ASSISTANT

Ask a question about this project