Files
MichilisandClaude Opus 5.5 d64db03f21 Phase 5: Telegram affiliate program
- docs/AFFILIATE.md: the owner's setup in Telegram (profile editor or
  BotFather's Mini App), commission and duration rules (raise only, end
  with commission 0, about 24 hours), who counts as referred, the link
  formats, and the no-double-rewards rule. Checked against Telegram's
  current pages.
- Affiliate purchases go through Stars invoices inside the Mini App
  (createInvoiceLink and WebApp.openInvoice, from Phase 4).
- /start accepts any payload; only our own rf_ (and old ref_) links set
  an inviter. A test sends payloads of every shape, Telegram's affiliate
  prefix included.
- Mini App Me page: an Earn section explaining both programs, with how
  to join Telegram's from the bot's profile, behind AFFILIATE_ENABLED.
  No affiliate numbers are computed or shown.
- /referral notes that Stars purchases earn through Telegram's program.
- The optional MTProto CLI is left out, with the reason in the docs.
- The Mini App browser test checks the Earn section in en, es and nl.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 21:31:06 +00:00

131 lines
6.5 KiB
Markdown

# Telegram's affiliate program
Telegram lets the owner of a Mini App open an **affiliate program**.
Anyone on Telegram can join it and get a link to the Mini App. When
somebody opens the Mini App for the first time through that link and
later spends Telegram Stars in it, Telegram pays the link's owner a share
of those Stars, for as long as the program says. Telegram handles all of
it: joining, links, counting and paying out.
The bot does not create the program and does not count affiliate
earnings. This page is about what the owner sets up by hand, and what the
bot does so the program works.
Sources, checked on 2026-11-18:
[affiliate programs](https://core.telegram.org/api/bots/referrals),
[referral links](https://core.telegram.org/api/links#referral-links),
[Mini Apps](https://core.telegram.org/bots/webapps),
[Bot API payments](https://core.telegram.org/bots/api#payments).
## Before you start (5 minutes)
Affiliates only earn on **Stars purchases made inside the Mini App**, so
both of these must work first.
1. **The Mini App.** Set `MINIAPP_ENABLED=true` and an HTTPS `PUBLIC_URL`,
and register the app in @BotFather as described in
[OPERATIONS.md](OPERATIONS.md). Open the bot and check that the menu
button opens the app.
2. **Stars payments.** Set `STARS_ENABLED=true` and your prices in
`STARS_PRICES` (or `[stars_prices]` in the config file), then restart.
In the app, open Buy, choose Telegram Stars (the switch is there when
Lightning is set up too) and check that a pass opens Telegram's payment
sheet.
## Open the program (5 minutes)
Use the Telegram account that owns the bot.
1. Open the bot's profile in Telegram and choose **Edit**, or open
@BotFather, start its Mini App and pick the bot.
2. Choose **Affiliate program**. Telegram shows this option to the
owner of a bot. If you do not see it, Telegram has not made affiliate
programs available to your account or region yet. That is decided by
Telegram (the `starref_program_allowed` setting), not by the bot.
3. Pick the **commission**: the share of each Stars purchase that goes to
the affiliate, in permille. Telegram only offers values within its
allowed range (`starref_min_commission_permille` to
`starref_max_commission_permille`). The range can change, so the app
shows the current one.
4. Optionally pick a **duration** in months: how long a referred user's
purchases keep earning their affiliate. Without one the program has no
end date.
5. Confirm. The program is live.
6. Set `AFFILIATE_ENABLED=true` and restart the bot. The Mini App's Me
page then explains Telegram's program next to the sats referral, and
/referral says that Stars purchases count there.
7. Check it as an ordinary user: open the bot's profile from another
account and look for the affiliate program.
Under the hood the apps call the MTProto method
`bots.updateStarRefProgram` with the bot, `commission_permille` and an
optional `duration_months`. This is not part of the Bot API, which is why
the bot cannot do it for you.
## Rules to know before you choose
- **Commission and duration can only be raised.** To lower either one,
end the program and open a new one.
- **Ending** means setting the commission to 0. The program then ends
about 24 hours later, at its `end_date`, and every affiliate link stops
working. A new program can only be opened after that; trying earlier
fails with `STARREF_AWAITING_END`.
- After the end, affiliates keep what they earned. People they referred
while the program ran keep earning them commission until the duration
that was set at the time runs out.
- **Who counts as referred:** somebody who starts the Mini App for the
first time after following an affiliate link. Their Stars purchases of
digital products in the Mini App then pay the affiliate automatically,
in Stars.
## What the bot does
- **Stars invoices for the Mini App.** The Buy page creates Stars
invoices with `createInvoiceLink` and opens them inside the app with
`WebApp.openInvoice`. The purchase is confirmed by the
`successful_payment` update Telegram sends to the bot, never by the
app. Telegram's page talks about purchases "within the mini app" and
does not say whether invoices sent in the bot chat count too. The chat
also offers Stars, so people can pay where they are, but **for the
affiliate program the Mini App is the way to buy**.
- **Any /start payload is fine.** Affiliate links come in two forms:
`https://t.me/<bot>?ref=<referrer>`, or
`https://t.me/<bot>?start=<prefix><referrer>`, where the prefixes are
Telegram's (`starref_start_param_prefixes`). Telegram resolves the
referrer. The bot only receives `/start <prefix><referrer>`, which it
answers with the normal welcome. Our own referral links start with
`rf_` (older ones with `ref_`). Only those set an inviter for the sats
program; any other payload is ignored for referrals. Neither program
can get in the way of the other. A test sends every kind of payload,
including made-up and broken ones.
- **No double rewards.** One purchase earns one reward:
- A **Lightning** purchase can earn the inviter our **sats reward**
(`REFERRAL_REWARD_SATS`, once per invited user, after the first
settled Lightning purchase).
- A **Stars** purchase can only earn an affiliate **Telegram's
commission**. Our sats reward ignores Stars purchases completely.
This rule is in `billing.rewardReferrer`, and covered by
`TestStarsPurchasesEarnNoSatsReferral`.
- **No numbers of its own.** The Earn section explains how to join from
the bot's profile and has a button that opens it. It never shows
affiliate earnings: Telegram shows those to the affiliate.
## What Telegram's pages do not say
These are decided by Telegram and are not documented on the pages above.
The bot makes no promises about them:
- How a user who followed several affiliate links is attributed.
- When commissions are paid out and when affiliates can withdraw them.
- The exact labels in each Telegram app; they can differ by version.
## Not included: a command-line tool for the program
An optional `cmd/affiliatectl` was suggested: a small MTProto tool for
`bots.getAdminedBots` and `bots.updateStarRefProgram`. It was left out.
It would add a large MTProto library to the module, and it needs the
owner's own Telegram login session. Keeping that session out of the
repository and out of the bot's runtime is easy to get wrong, and a
mistake there exposes the owner's whole account. The official apps do the
same job safely in a few taps, as described above.