Bekarys

KZ Price

A Telegram bot and website that track product prices from kaspi.kz, keep the history, draw a chart and notify you when a price drops. Not affiliated with Kaspi.

  • Python
  • aiogram
  • FastAPI
  • SQLAlchemy
  • SQLite
  • Docker
  • GitHub Actions

Problem

The price of the same product on kaspi.kz changes several times a week. Catching a drop means opening the product page every day. The service does that for you: it remembers the price, shows its history and messages you on Telegram when it falls.

Solution

You send the bot a product link or paste it on the website. The service regularly fetches the lowest price among sellers, stores it in the history and compares it with the previous one. You can follow any drop or set a target price.

The bot, the website and the price checker run in one process on one SQLite database, and the interface is available in Kazakh, Russian and English. The service runs on a server in Kazakhstan behind Cloudflare. Every change is deployed through GitHub Actions only after the tests pass, and the database is copied to cloud storage every night.

The service is independent and not affiliated with Kaspi: kaspi.kz is only its source of prices.

Challenges

  • A gentle pace. The source sets its pace in robots.txt: no more than one request every 10 seconds, with background crawls in a set time window. The service always keeps to it. If the source refuses a request, the check cycle stops and the service goes quiet for a long while instead of retrying.
  • Check frequency tiers. The service scales through priorities, not frequency. Products with subscribers, products pinned by the admin and products recently opened on the website are checked about every 4 hours. Products opened within the last month are checked once a day, the rest once a week. A product nobody has opened for three months is not checked at all.
  • One request queue. The bot and the website share one client and one queue, so the total load on the source doesn't depend on where a person came from. Viewing a page on the website never touches the source.
  • One request per product. However many people follow a product, its price is requested once per cycle. A product with subscribers is never re-requested outside the cycle: otherwise the "previous price" would be overwritten and subscribers would miss the drop.
  • One price instead of a price per city. At first prices were stored separately for each city. A measurement showed that out of 16 products in Almaty and Astana only one price differed, and by just 15 ₸. The service switched to a single price for Astana, halving both the requests and the catalog. Subscriptions and price history were migrated automatically when the new version started.
  • All access to the source in one file. The API is unofficial, so requests and response parsing live in one module with an HTML fallback. When the format changes, one file gets fixed.
  • Short links from the app. The "Share" button in the app gives a short link — the service expands it on its own.

Architecture

  1. Telegram bot and websiteaiogram and FastAPI + Jinja in one process
  2. Catalogadding a product, shared by the bot and the website
  3. Request queueone client, a 10-second pause between requests
  4. Price checkerfrequency tiers, the notification decision
  5. SQLiteproducts, subscriptions, price history; a nightly copy to the cloud
  6. ServerDocker on a VPS in Kazakhstan, Caddy behind Cloudflare, deploys via GitHub Actions

Code samples

The request queue. Under the lock a request only books its send time, and it waits outside the lock. This way requests from the bot, the website and the background checker line up one after another without holding the lock while they wait:

async def _throttle(self) -> None:
    # Under the lock we only book a slot; we wait outside it.
    async with self._lock:
        now = self._clock()
        wait = self._next_slot - now
        self._next_slot = (
            max(now, self._next_slot)
            + self._min_interval
            + random.uniform(0, self._interval_jitter)
        )
    if wait > 0:
        await self._sleep(wait)

The rule for whether to notify a subscriber. With a target price, a repeat message goes out only if the price has dropped below the one already reported:

def decide_notification(
    old_price: int | None,
    new_price: int | None,
    target_price: int | None,
    last_notified_price: int | None,
) -> tuple[bool, int | None]:
    """Whether to notify a subscriber -> (notify, new last_notified_price)."""
    if new_price is None:
        return False, last_notified_price
    if target_price is None:
        return (old_price is not None and new_price < old_price), None
    if new_price > target_price:
        return False, None
    if last_notified_price is None or new_price < last_notified_price:
        return True, new_price
    return False, last_notified_price

How to use it

  1. In the store's app, open a product, tap "Share" and copy the link.
  2. Send the link to @kz_pricebot or paste it on the website.
  3. The bot shows the current price. The "Target price" button sets a threshold — then you get a message only when the price drops to it.
  4. When the price drops, the bot sends a notification. The "History" button shows the low, the high and a chart; on the website each product has a page with a chart for 7, 30 and 90 days.