A Telegram-based automated payment detection system
Project description
AvtoPaymentBot
A Telegram-based automated payment detection system designed specifically for the Uzbekistan market. This application uses Telegram Userbots via the Telethon library to intercept incoming transaction notifications from payment systems (Click, Payme, Uzcard, Humo) on behalf of merchants. It acts as an integration middleware, parsing transaction SMS/Messages and automatically firing Webhooks to the merchant's backend server whenever a matching payment intent is fulfilled.
Table of Contents
- Features
- Technology Stack
- Architecture & Mechanics
- Setup & Usage
- Environment Variables
- Testing
- License
Features
- Multi-Merchant Support: Manages concurrent Telegram Userbots for multiple independent merchants dynamically.
- Smart Payment Parser: Uses regex to interpret and normalize transaction amounts from Click, Payme, Uzcard, and Humo notification bots.
- Collision Management: For multiple identical base amounts, the system temporarily augments subsequent transaction requests by tiny fractions (+0.01 UZS offset/1 tiyin) to maintain uniqueness for idempotent processing.
- Webhook Integration: Supports instant asynchronous HTTP webhooks signed with an HMAC (SHA-256) signature payload to alert your backend.
- Background Health Checking: A background asynchronous daemon runs every 5 minutes to verify if merchant sessions have been revoked. If an active session is unauthorized, it immediately notifies both the merchant and the system admins via the primary Telegram Management bot.
- Automated Deployments: Full support for seamless Docker-compose orchestrations and backend CI/CD routines.
Technology Stack
- Python 3.11+
- Framework: FastAPI (Uvicorn)
- Database: PostgreSQL (with Asyncpg via SQLAlchemy 2.0 ORM) + Alembic for migrations
- Telegram APIs: Telethon (Userbots) and aiogram (Management Bot - Migrated to Telethon in
bot.pyfor simplicity in latest release) - Containerization: Docker & Docker Compose
- Error Tracking: Sentry SDK
- Testing: Pytest (with pytest-asyncio)
Architecture & Mechanics
- Management Bot: Merchants interact with the
@YourManagementBoton Telegram to generate aStringSession. - REST API: Web applications/services hit the FastAPI endpoints to create a new
PaymentIntentfor a specific merchant. - Telethon Worker:
worker/main.pybootsClientManager, executing the management bot and all active merchant userbots inside a unified event loop. - Message Interception: When a userbot receives a message from
KNOWN_BOT_USERNAMES(e.g.@clickuz), it fires a webhook payload logic check against the database. - Reconciliation: The
PaymentServiceattempts to match the payload amount to an openPaymentIntent. If matched, a secure webhook is dispatched.
Setup & Usage
1. Prerequisites
- Docker and Docker Compose installed
- A Telegram API ID and API Hash from
my.telegram.org - A Telegram Bot Token from
@BotFather - A domain/server pointing to port 80/443 (configured via Nginx proxy).
2. Configuration
Create a .env file in the backend/ directory referencing .env.example:
cp backend/.env.example backend/.env
Fill out TELEGRAM_API_ID, TELEGRAM_API_HASH, BOT_TOKEN, ADMIN_TELEGRAM_IDS and your preferred POSTGRES_* credentials.
3. Run with Docker Compose
cd backend
docker-compose up --build -d
The application, database, and background worker will launch simultaneously. Note: Run Alembic migrations natively if the entrypoint does not auto-stamp it.
docker-compose exec worker alembic upgrade head
4. Merchant Flow
- Open your management bot on Telegram.
- Send
/loginand provide your phone number and OTP code. - Use
/create(or the REST API) to generate a Payment Intent. - Wait for the user to transfer the funds to your registered card.
- Watch the Webhook fire!
Testing
To run the automated tests locally:
cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
PYTHONPATH=. pytest tests/
All 21+ test cases covering parsing logic, idempotency caps, collision behaviors, and API security should pass successfully.
License
MIT License. See the LICENSE file for details. Note: Using userbots to scrape messages technically falls into a grey area under Telegram's Terms of Service. Be mindful of usage rate-limits and restrict the listener solely to the official banking bots.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autopaybot-0.1.0.tar.gz.
File metadata
- Download URL: autopaybot-0.1.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be24fcb08822db1a05b5df8b09b4748518d7b92e13f8c0b689e7c11e51b5c729
|
|
| MD5 |
f7e96766b465bf01992f15aaec0dbc5d
|
|
| BLAKE2b-256 |
bfb5898b1981b85007264f8525aa8bbb348cdcd62494b1502b9b0a6913408f99
|
Provenance
The following attestation bundles were made for autopaybot-0.1.0.tar.gz:
Publisher:
publish.yml on theMirmakhmudov/AutoPay-API
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autopaybot-0.1.0.tar.gz -
Subject digest:
be24fcb08822db1a05b5df8b09b4748518d7b92e13f8c0b689e7c11e51b5c729 - Sigstore transparency entry: 2153394760
- Sigstore integration time:
-
Permalink:
theMirmakhmudov/AutoPay-API@388dd164124e0f142b75b7bc0700a490541b9235 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/theMirmakhmudov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@388dd164124e0f142b75b7bc0700a490541b9235 -
Trigger Event:
release
-
Statement type:
File details
Details for the file autopaybot-0.1.0-py3-none-any.whl.
File metadata
- Download URL: autopaybot-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4065817c42cd4cf74fcc36ee15ac8dd4eaa8ae51b0ecb596ec80903f8191852e
|
|
| MD5 |
c7cf4965e9eb54c1a5f8214fae55cfd5
|
|
| BLAKE2b-256 |
8fdce0b3fd86f02d2d3c01935c6a9e1d1e9a884d70b340455247046e2b344c00
|
Provenance
The following attestation bundles were made for autopaybot-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on theMirmakhmudov/AutoPay-API
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autopaybot-0.1.0-py3-none-any.whl -
Subject digest:
4065817c42cd4cf74fcc36ee15ac8dd4eaa8ae51b0ecb596ec80903f8191852e - Sigstore transparency entry: 2153395024
- Sigstore integration time:
-
Permalink:
theMirmakhmudov/AutoPay-API@388dd164124e0f142b75b7bc0700a490541b9235 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/theMirmakhmudov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@388dd164124e0f142b75b7bc0700a490541b9235 -
Trigger Event:
release
-
Statement type: