E-commerce
Affiliate Deal Bot
An automation that runs unattended against three marketplaces, where every hard problem came from the real world rather than from the design.

The problem
Deal channels on Telegram publish offers that someone else has already monetised: the links carry another affiliate tag. Reposting them earns nothing, and rewriting them by hand does not scale past a handful a day. The bot has to recognise an offer, work out which product it actually points at, confirm the discount is real, and rebuild the post under its own tag, without ever publishing the same deal twice or posting a product into the wrong channel.
Approach
- Every incoming message is offered to all handlers instead of the first one that claims it. A single post routinely mixes an Amazon block with a Mercado Livre one, and a first-match design silently dropped the rest.
- Each marketplace resolves differently. Amazon shortlinks hide the ASIN, so the bot follows the redirect and canonicalises to dp/{ASIN}, which also discards the original affiliate attribution. Shopee goes through the Affiliate Open API. Mercado Livre needed the hardest work: the signed ref cannot be rewritten, so the bot reads the tracking JSON embedded in the resolved page to recover the real item id.
- Deduplication is atomic, not best-effort. The store reserves a key with claim() before posting and releases it if the post fails, so two concurrent messages carrying the same offer cannot both publish it. The window is seven days.
- Failures are handled where they happen: a Telegram FloodWait is waited out and retried instead of losing the offer, and a misconfigured channel stops the bot at startup rather than publishing into the wrong place.
What it proves
- 461 passing tests over a codebase where URL parsing and price parsing are isolated in their own modules, so adding a marketplace does not touch the pipeline.
- Mercado Livre shortlink resolution validated against 43 real links, at 91% success.
- A production bug found on 2026-07-17 affected roughly 58% of posts, because the recovered id is not always a catalogue id; the fix reads the real product path from the same payload.
- Runs unattended in production, feeding a live Telegram channel.
PythonTelethonasyncioSQLitepytestREST APIsWeb ScrapingAutomation