Turn any Telegram channel with product links into a monetized feed in minutes.
This script listens to a source channel, detects the first URL in each new message, converts it to an AliExpress affiliate link, and reposts the updated message to a target channel automatically.
- Hands‑off monetization: keep posting as usual—links get converted automatically.
- Fast time‑to‑value: simple .env config, one command to run.
- Trackable results: uses your AliExpress
tracking_id(PID) for attribution.
- Watches a source channel (
SOURCE_CHANNEL_ID) for new messages. - Parses the first URL in the message body (regex
https?://\S+). - Converts that URL into an AliExpress affiliate link using
AliexpressApi(KEY,SECRET,TRACKING_IDrequired). - Replaces the original URL with the affiliate one and forwards the message to the target channel (
TARGET_CHANNEL_ID). - Logs all important steps and errors; fully async on top of Telethon.
If there is no URL or if affiliate conversion fails, the script logs it and skips replacement.
Telethonstarts a user session viaPHONE_NUMBER(createsanon.sessionon first run).@client.on(events.NewMessage(chats=SOURCE_CHANNEL_ID))fires on each new message.find_link_in_message()extracts the first URL.generate_affiliate_link()callsaliexpress.get_affiliate_links(original_link)and pickspromotion_link.- The message text is updated and sent to
TARGET_CHANNEL_ID.
Flow diagram:
[Source Channel] --(Telethon)--> [Extract first URL] --(AliExpress API)--> [Replace URL]
\__________________________________________________________________________/
|
Forward
v
[Target Channel: affiliate link inside]
Requires Python 3.9+.
# 1) Create and activate a virtual environment (recommended)
python -m venv .venv
# Windows:
.venv\Scripts\activate
# macOS/Linux:
source .venv/bin/activate
# 2) Install dependencies
pip install -U telethon python-dotenv aliexpress-api
# Note: some forks of the AliExpress SDK use different names.
# If you see ImportError for `aliexpress_api`, try:
# pip install aliexpress-affiliate-apiCreate a .env file in the project root:
# AliExpress Affiliate
KEY=your_aliexpress_app_key
SECRET=your_aliexpress_app_secret
TRACKING_ID=your_tracking_id # PID / trackingId
# Telegram (user session)
API_ID=1234567
API_HASH=your_api_hash
PHONE_NUMBER=+380XXXXXXXXX
# Channel IDs (integer IDs; for supergroups/channels use -100XXXXXXXXXX)
SOURCE_CHANNEL_ID=-1001111111111
TARGET_CHANNEL_ID=-1002222222222Notes
- Get
API_ID/API_HASHat https://my.telegram.org. - Use numeric IDs for private channels. You can fetch them via info bots or a small Telethon helper.
python main.pyOn the first launch, Telethon will ask for a one‑time code in Telegram to create the local session (anon.session). After that, the script runs continuously and relays messages as they arrive.
- Replaces only the first URL per message (extendable to many).
- Designed for AliExpress links; non‑AliExpress URLs may be ignored by the SDK.
- Uses a user session (not a bot). Ensure the account can read from the source and post to the target channel.
- Verbose logging explains why a URL wasn’t replaced or a message wasn’t sent.
- ModuleNotFoundError:
aliexpress_api→ installaliexpress-apior tryaliexpress-affiliate-apidepending on the fork you use. - Auth loop on first run → make sure the phone number is valid and you enter the Telegram login code promptly.
- Wrong channel IDs → verify you are using numeric IDs (often start with
-100for channels/supergroups). - No messages forwarded → confirm the account has permissions in both channels and that messages actually contain URLs.
I can build for you custom Telegram bots, scripts, and automation solutions — fast, reliable, and scalable.
What I can do:
- Website automation (simulate user actions, form filling, scheduling)
- Data collection & web scraping (APIs or no-API scraping)
- Custom parsers for structured/unstructured data
- Chatbot development (Telegram, Discord, Slack, WhatsApp)
- Backend systems & API integrations
Goal: automate routine tasks, collect valuable data, and make your processes more efficient.