Phase 3: own referral system (sats)
- Referral links carry a random 8 character base32 code (rf_<code>) built from the bot username from getMe. Old ref_<id> links still work but are never shown. - The reward (REFERRAL_REWARD_SATS, old name REFERRAL_REWARD still read) is paid through the ledger once the invited user's first purchase settles, once per invited user, never for self-referrals. - REFERRAL_DAILY_CAP limits what an inviter earns per UTC day; rewards above it wait as pending and are paid on a later day by the billing loop. - /referral shows invited, converted, earned and pending, with Share, Copy link and QR code buttons. The Mini App Me page mirrors it. - Migration 00008: users.ref_code (unique) and referral_rewards, backfilled from earlier referral credits in the ledger. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
123cede7f3
commit
f7ceadc462
@@ -3,7 +3,6 @@ package bot
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -302,13 +301,8 @@ func (b *Bot) cmdStart(ctx context.Context, m *telego.Message, act actor) {
|
||||
case payload == "buy":
|
||||
b.showBuy(ctx, m.Chat.ID, act)
|
||||
return
|
||||
case strings.HasPrefix(payload, "ref_"):
|
||||
// Only somebody who is new to the bot can be referred.
|
||||
if referrer, err := strconv.ParseInt(strings.TrimPrefix(payload, "ref_"), 10, 64); err == nil && act.user.IsNew {
|
||||
if _, err := b.store.SetReferrer(ctx, act.user.ID, referrer); err != nil {
|
||||
b.log.Error("set referrer", "user_id", act.user.ID, "err", err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
b.applyReferral(ctx, act, payload)
|
||||
}
|
||||
// Somebody new picks a language under the welcome. It starts in the
|
||||
// language of their Telegram app, which is usually right.
|
||||
@@ -528,6 +522,8 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
|
||||
b.cbRetryLookup(ctx, q, act, id)
|
||||
case cbUpgrade:
|
||||
b.cbShowUpgrade(ctx, q, act)
|
||||
case cbReferralQR:
|
||||
b.cbSendReferralQR(ctx, q, act)
|
||||
case cbPersonal:
|
||||
b.cbPersonalSetting(ctx, q, act, id, arg)
|
||||
case cbSkipQueue:
|
||||
|
||||
+123
-11
@@ -1,8 +1,10 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -215,26 +217,136 @@ func (b *Bot) cmdReferral(ctx context.Context, m *telego.Message, act actor) {
|
||||
if !b.privateOnly(ctx, m, act) {
|
||||
return
|
||||
}
|
||||
text, err := b.referralText(ctx, act)
|
||||
text, kb, err := b.referralView(ctx, act, false)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, text, nil)
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, text, kb)
|
||||
}
|
||||
|
||||
// referralText explains the referral program with the user's own link.
|
||||
func (b *Bot) referralText(ctx context.Context, act actor) (string, error) {
|
||||
n, err := b.store.CountReferrals(ctx, act.user.ID)
|
||||
// referralPrefix starts the /start payload of a referral link:
|
||||
// rf_<code>. Old links carry ref_<Telegram user ID>; they keep working but
|
||||
// are never shown again.
|
||||
const (
|
||||
referralPrefix = "rf_"
|
||||
legacyReferralPrefix = "ref_"
|
||||
)
|
||||
|
||||
// ReferralLink is a user's invite link. The bot's username comes from
|
||||
// getMe, the code is random.
|
||||
func (b *Bot) ReferralLink(ctx context.Context, userID int64) (string, error) {
|
||||
code, err := b.store.RefCode(ctx, userID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
link := "https://t.me/" + b.username + "?start=ref_" + strconv.FormatInt(act.user.ID, 10)
|
||||
return b.tr.HTML(act.lang, "referral.text", i18n.Args{
|
||||
"Link": link,
|
||||
"Reward": b.tr.Sats(act.lang, b.cfg.Prices.ReferralReward),
|
||||
"Count": n,
|
||||
}), nil
|
||||
return "https://t.me/" + b.username + "?start=" + referralPrefix + code, nil
|
||||
}
|
||||
|
||||
// ReferralStats sums up a user's referrals.
|
||||
func (b *Bot) ReferralStats(ctx context.Context, userID int64) (db.ReferralStats, error) {
|
||||
return b.store.ReferralStatsFor(ctx, userID)
|
||||
}
|
||||
|
||||
// ReferralQR is a user's invite link as a QR code PNG.
|
||||
func (b *Bot) ReferralQR(ctx context.Context, userID int64) ([]byte, error) {
|
||||
link, err := b.ReferralLink(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return qrcode.Encode(link, qrcode.Medium, 512)
|
||||
}
|
||||
|
||||
// shareURL opens Telegram's share sheet with a link and a line of text.
|
||||
func shareURL(link, text string) string {
|
||||
return "https://t.me/share/url?url=" + url.QueryEscape(link) + "&text=" + url.QueryEscape(text)
|
||||
}
|
||||
|
||||
// referralView explains the referral program with the user's own link,
|
||||
// stats and buttons to share it. back adds a button to the personal
|
||||
// settings.
|
||||
func (b *Bot) referralView(ctx context.Context, act actor, back bool) (string, *telego.InlineKeyboardMarkup, error) {
|
||||
lang := act.lang
|
||||
link, err := b.ReferralLink(ctx, act.user.ID)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
st, err := b.store.ReferralStatsFor(ctx, act.user.ID)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
text := b.tr.HTML(lang, "referral.text", i18n.Args{
|
||||
"Link": link,
|
||||
"Reward": b.tr.Sats(lang, b.cfg.Prices.ReferralReward),
|
||||
"Invited": b.tr.Number(lang, int64(st.Invited)),
|
||||
"Converted": b.tr.Number(lang, int64(st.Converted)),
|
||||
"Earned": b.tr.Sats(lang, st.Earned),
|
||||
"Pending": b.tr.Sats(lang, st.Pending),
|
||||
})
|
||||
if c := b.cfg.ReferralDailyCap; c > 0 {
|
||||
text += "\n\n" + b.tr.HTML(lang, "referral.cap", i18n.Args{"Cap": b.tr.Sats(lang, c)})
|
||||
}
|
||||
rows := [][]telego.InlineKeyboardButton{
|
||||
{
|
||||
urlButton(b.tr.T(lang, "btn.share", nil), shareURL(link, b.tr.T(lang, "referral.share_text", nil))),
|
||||
{Text: b.tr.T(lang, "btn.copy_link", nil), CopyText: &telego.CopyTextButton{Text: link}},
|
||||
},
|
||||
{button(b.tr.T(lang, "btn.qr", nil), cbReferralQR)},
|
||||
}
|
||||
if back {
|
||||
rows = append(rows, b.backRow(lang))
|
||||
}
|
||||
return text, keyboard(rows...), nil
|
||||
}
|
||||
|
||||
// cbReferralQR sends the invite link as a QR code.
|
||||
const cbReferralQR = "rq"
|
||||
|
||||
func (b *Bot) cbSendReferralQR(ctx context.Context, q *telego.CallbackQuery, act actor) {
|
||||
png, err := b.ReferralQR(ctx, act.user.ID)
|
||||
if err != nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
||||
return
|
||||
}
|
||||
link, _ := b.ReferralLink(ctx, act.user.ID)
|
||||
b.answer(ctx, q, "", false)
|
||||
_, err = b.api.SendPhoto(ctx, &telego.SendPhotoParams{
|
||||
ChatID: chatID(act.user.ID),
|
||||
Photo: telego.InputFile{File: namedReader{Reader: bytes.NewReader(png), name: "invite.png"}},
|
||||
Caption: b.tr.HTML(act.lang, "referral.qr_caption", i18n.Args{"Link": link}),
|
||||
ParseMode: telego.ModeHTML,
|
||||
})
|
||||
if err != nil {
|
||||
b.log.Warn("send referral QR", "user_id", act.user.ID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// applyReferral records who invited a user who arrived through an invite
|
||||
// link, and reports whether payload was one. Only somebody new to the bot
|
||||
// can be referred, never by themselves. Old links with the inviter's
|
||||
// Telegram ID still work.
|
||||
func (b *Bot) applyReferral(ctx context.Context, act actor, payload string) bool {
|
||||
var referrer int64
|
||||
switch {
|
||||
case strings.HasPrefix(payload, referralPrefix):
|
||||
if u, err := b.store.UserByRefCode(ctx, strings.TrimPrefix(payload, referralPrefix)); err == nil {
|
||||
referrer = u.ID
|
||||
}
|
||||
case strings.HasPrefix(payload, legacyReferralPrefix):
|
||||
id, err := strconv.ParseInt(strings.TrimPrefix(payload, legacyReferralPrefix), 10, 64)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
referrer = id
|
||||
default:
|
||||
return false
|
||||
}
|
||||
if referrer != 0 && referrer != act.user.ID && act.user.IsNew {
|
||||
if _, err := b.store.SetReferrer(ctx, act.user.ID, referrer); err != nil {
|
||||
b.log.Error("set referrer", "user_id", act.user.ID, "err", err)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Bot) cmdRedeem(ctx context.Context, m *telego.Message, act actor, arg string) {
|
||||
|
||||
@@ -190,12 +190,12 @@ func (b *Bot) cbPersonalSetting(ctx context.Context, q *telego.CallbackQuery, ac
|
||||
}
|
||||
case psReferral:
|
||||
b.answer(ctx, q, "", false)
|
||||
text, err := b.referralText(ctx, act)
|
||||
text, kb, err := b.referralView(ctx, act, true)
|
||||
if err != nil {
|
||||
b.log.Error("referral text", "err", err)
|
||||
b.log.Error("referral view", "err", err)
|
||||
return
|
||||
}
|
||||
show(text, keyboard(b.backRow(act.lang)))
|
||||
show(text, kb)
|
||||
return
|
||||
default:
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
||||
|
||||
Reference in New Issue
Block a user