A Python script that monitors new Telegram Star Gifts and auto-buys limited gifts based on configurable rules (market-cap threshold, max supply, per-user distribution, cooldowns, etc.).
It uses two Telegram sessions: one for parsing the gift catalog and one for buying/sending gifts.
⚠️ Use responsibly. Respect Telegram’s Terms of Service and local laws. Consider API rate limits (FloodWait) and avoid abusive behavior.
- Connects two Telegram accounts:
- Parsing account — reads the gift catalog via
get_star_gifts(). - Stars account — sends/buys gifts with
send_star_gift().
- Parsing account — reads the gift catalog via
- Filters limited gifts and ignores already-seen IDs (
gift_list). - Computes a simple “market cap” score
market_cap_like = price × total_amount × 0.015and compares it tomcapthreshold. - Splits total purchases among N recipients (1–10) with per-send delays.
- Sends notifications to a specified account.
- Structured logging with
loguru.
- Python 3.10+
- Dependencies:
pyrogram,loguru(optionallypython-dotenvif you prefer.env) - Two Telegram accounts/sessions:
- one with access to the gift catalog,
- one that actually has Stars to send.
- API ID / API Hash from https://my.telegram.org/auth
Example requirements.txt:
pyrogram==2.2.7
loguru>=0.7.2
python-dotenv>=1.0.1Install:
python3 -m venv .venv && source .venv/bin/activate
pip install -U pip
pip install -r requirements.txtThe script imports settings from config.py. Create it next to the script and fill in your values.
# config.py — sample configuration
# Number of recipients (1..10)
num_of_users = 3
# Recipient usernames (Telegram @usernames or numeric IDs)
first_username = "user1"
second_username = "user2"
third_username = "user3"
fourth_username = ""
fifth_username = ""
sixth_username = ""
seventh_username = ""
eighth_username = ""
ninth_username = ""
tenth_username = ""
# Delay between individual sends (seconds)
time_sleep = 2.0
# Where to send notifications (username or ID)
acc_for_notification = "me"
# Filtering thresholds
# Gifts pass only if:
# - gift.is_limited == True
# - computed market_cap_like <= mcap
# - gift.total_amount <= max_supply
mcap = 50000 # tune this threshold for your strategy
max_supply = 10000 # maximum allowed total_amount
# Budgeting
stars_for_each = 1000 # total Stars you plan to allocate per gift (split among users)
# Main loop cooldown (seconds) between parsing cycles
parsing_cooldown = 5
# Phone numbers for session login (format Telegram accepts)
tg_number_for_parse_acc = "+48111111111"
tg_number_for_buy_acc = "+48222222222"You will also set your API credentials inside the script (see below) or switch to environment variables if you prefer.
At the top of the script, set the two app credentials you got from my.telegram.org:
# API keys from https://my.telegram.org/auth
parsing_acc_api_id = 1111111
parsing_acc_api_hash = "11111111"
acc_with_stars_api_id = 111111
acc_with_stars_api_hash = "11111111"Tip: for better security, load these from environment variables or a
.envfile (e.g., withpython-dotenv).
- Login sequence:
- Starts a session
parsing_acc(parsing account). - Prompts you to confirm a code for the buying account as well (
acc_with_stars), then closes it (to ensure the session is initialized).
- Starts a session
- Main loop:
Everyparsing_cooldownseconds:- Fetches current gifts via
app.get_star_gifts(). - For each new, limited gift not in
gift_list:- Computes a simple “market-cap-like” score:
price × total_amount × 0.015. - If
<= mcapandtotal_amount <= max_supply, it calculates how many to send fromstars_for_each, splits evenly acrossnum_of_users, and callsbuy_nft(...).
- Computes a simple “market-cap-like” score:
- Logs success or errors via
loguru.
- Fetches current gifts via
- Sending gifts:
buy_nft()opens theacc_with_starsclient and iterates recipients withtime_sleeppauses, callingsend_star_gift(...).
Save your script (e.g., main.py) and run:
python main.pyOn first run, Pyrogram will ask for login codes (per session). Make sure both numbers in config.py can receive Telegram login codes.
- Flood control: The current implementation uses static
time.sleep. If you hitpyrogram.errors.FloodWait, increasetime_sleepandparsing_cooldown. For production, consider adding an exponential backoff and catchingFloodWait. - Seen gifts list (
gift_list): Hard-coded in the script to avoid reprocessing known gifts. You can persist it to disk (e.g., JSON) to keep state across restarts. - Distribution math: The script rounds to evenly split total sends among recipients. Adjust logic if you want different prioritization (e.g., first recipients get the remainder).
- Notifications: The parser sends a short message to
acc_for_notificationwith(gift.id, gift.sticker.emoji, market_cap_like)when a qualifying gift appears.
- Stuck on login / codes not arriving:
Ensurephone_numbervalues are correct and you can receive Telegram login codes for both accounts. FloodWaitor rate limits:
Increasetime_sleepandparsing_cooldown. Add jitter or a retry policy.- Attribute vs dict access:
The code expects gift objects (gift.id,gift.price, etc.). If you change providers, keep the same data model.
- Persist
gift_listtogift_seen.json(load on start, atomic save on update). - Replace
if num_of_users == Nbranches with a dynamicUSERNAMESlist. - Open
acc_with_starsonce and pass the client tobuy_nft(...). - Structured logging (bind gift_id/username) + file rotation.
- Central rate limiter with jitter.
MIT (feel free to change if needed).