Phase 2: money
Lightning payments through LNbits, wallets, passes, credits, the entitlement resolver and priority lanes. - internal/entitle: pure resolver (one-off, own pass, group pass, group credits, own credits, free) with a table-driven suite and an invariant check over every input combination - internal/billing: wallets, append-only ledger with dedupe keys, stacking passes, purchases, gift codes, referral reward - LNbits client verified against v0.12.12 and v1.7.1 source - Settlement by one conditional UPDATE: idempotent under any mix of webhooks, polls and the check button. Webhooks are unsigned, so every payment is confirmed with LNbits before it is credited - Polling fallback, reconciliation of all unsettled invoices at startup, late payments still honored, no expiry while LNbits is unreachable - Charge, quota and job are written in one transaction; failed, cancelled and undeliverable downloads are refunded exactly once - Paid lane first with one slot of the fleet reserved for free users - Daily download quota and daily volume cap, per-user and per-group rate limits - Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice message that flips to paid, over-limit offer (passes or this file only), deep links from groups, skip the queue - Fake LNbits server for tests; end-to-end money flows including a payment made while the core was down Cashu redemption is behind an interface and not implemented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
3b361c17a1
commit
0d50f453a0
+91
-11
@@ -3,6 +3,7 @@ package bot
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
@@ -53,6 +54,20 @@ func (b *Bot) handleMessage(ctx context.Context, m *telego.Message) {
|
||||
b.cmdQueue(ctx, m, act)
|
||||
case "cancel":
|
||||
b.cmdCancel(ctx, m, act)
|
||||
case "buy":
|
||||
b.cmdBuy(ctx, m, act)
|
||||
case "account":
|
||||
b.cmdAccount(ctx, m, act)
|
||||
case "limits":
|
||||
b.cmdLimits(ctx, m, act)
|
||||
case "credits":
|
||||
b.cmdCredits(ctx, m, act)
|
||||
case "donate":
|
||||
b.cmdDonate(ctx, m, act)
|
||||
case "redeem":
|
||||
b.cmdRedeem(ctx, m, act, commandArgs(m))
|
||||
case "referral":
|
||||
b.cmdReferral(ctx, m, act)
|
||||
default:
|
||||
if private {
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "cmd.unknown", nil), nil)
|
||||
@@ -78,6 +93,16 @@ func (b *Bot) handleMessage(ctx context.Context, m *telego.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
// commandArgs returns the text after the command of a message.
|
||||
func commandArgs(m *telego.Message) string {
|
||||
for _, e := range m.Entities {
|
||||
if e.Type == telego.EntityTypeBotCommand && e.Offset == 0 && e.Length <= len(m.Text) {
|
||||
return strings.TrimSpace(m.Text[e.Length:])
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// parseCommand finds a bot command at the start of the message. forUs is
|
||||
// false when the command names a different bot (/dl@otherbot).
|
||||
func (b *Bot) parseCommand(m *telego.Message) (cmd string, forUs, isCmd bool) {
|
||||
@@ -119,18 +144,42 @@ func messageLinks(m *telego.Message) []string {
|
||||
}
|
||||
|
||||
func (b *Bot) cmdStart(ctx context.Context, m *telego.Message, act actor) {
|
||||
key := "start.private"
|
||||
if !isPrivate(m.Chat.Type) {
|
||||
key = "start.group"
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, "start.group", nil), nil)
|
||||
return
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, key, i18n.Args{"Name": act.user.FirstName}), nil)
|
||||
// Deep links arrive as "/start <payload>".
|
||||
payload := commandArgs(m)
|
||||
switch {
|
||||
case strings.HasPrefix(payload, "buy_"):
|
||||
b.startBuyLink(ctx, m, act, strings.TrimPrefix(payload, "buy_"))
|
||||
return
|
||||
case strings.HasPrefix(payload, "skip_"):
|
||||
b.startSkipLink(ctx, m, act, strings.TrimPrefix(payload, "skip_"))
|
||||
return
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, "start.private", i18n.Args{"Name": act.user.FirstName}), nil)
|
||||
}
|
||||
|
||||
func (b *Bot) cmdHelp(ctx context.Context, m *telego.Message, act actor) {
|
||||
lim := b.svc.LimitsFor(ctx, act.user.ID)
|
||||
dec, err := b.svc.Entitlement(ctx, service.Who{UserID: act.user.ID, ChatID: m.Chat.ID, ChatType: m.Chat.Type}, false)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, "help.text", i18n.Args{
|
||||
"MaxSize": b.tr.Size(act.lang, lim.MaxSize),
|
||||
"MaxDuration": b.tr.Span(act.lang, secondsDuration(lim.MaxDuration)),
|
||||
"MaxSize": b.tr.Size(act.lang, dec.Limits.MaxSize),
|
||||
"MaxDuration": b.tr.Span(act.lang, secondsDuration(dec.Limits.MaxDuration)),
|
||||
}), nil)
|
||||
}
|
||||
|
||||
@@ -241,14 +290,29 @@ func (b *Bot) cancelDelivery(ctx context.Context, deliveryID string, userID int6
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
note := b.release(ctx, d)
|
||||
job, err := b.store.GetJob(ctx, d.JobID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.tr.HTML(d.Lang, "status.cancelled", nil))
|
||||
b.finalStatus(ctx, d, titleLine(job.Title)+"\n"+b.tr.HTML(d.Lang, "status.cancelled", nil)+note)
|
||||
return nil
|
||||
}
|
||||
|
||||
// release gives back what an undelivered download took and returns a line
|
||||
// to append to the status when sats were refunded.
|
||||
func (b *Bot) release(ctx context.Context, d *db.Delivery) string {
|
||||
refunded, err := b.svc.Release(context.WithoutCancel(ctx), d.ID)
|
||||
if err != nil {
|
||||
b.log.Error("release delivery", "delivery_id", d.ID, "err", err)
|
||||
return ""
|
||||
}
|
||||
if refunded <= 0 {
|
||||
return ""
|
||||
}
|
||||
return "\n" + b.tr.HTML(d.Lang, "status.refunded", i18n.Args{"Amount": b.tr.Sats(d.Lang, refunded)})
|
||||
}
|
||||
|
||||
func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
|
||||
act, err := b.actorFor(ctx, &q.From)
|
||||
if err != nil {
|
||||
@@ -265,6 +329,16 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
|
||||
b.cbDownload(ctx, q, act, id, arg)
|
||||
case cbCancelCard:
|
||||
b.cbCancelCard(ctx, q, act, id)
|
||||
case cbSkipQueue:
|
||||
b.cbSkip(ctx, q, act, id)
|
||||
case cbBuy:
|
||||
b.cbBuyProduct(ctx, q, act, id, arg)
|
||||
case cbOfferBuy:
|
||||
b.cbOffer(ctx, q, act, id, arg)
|
||||
case cbPayCheck:
|
||||
b.cbPayCheck(ctx, q, act, id)
|
||||
case cbPayCancel:
|
||||
b.cbPayCancel(ctx, q, act, id)
|
||||
case cbCancelJob:
|
||||
err := b.cancelDelivery(ctx, id, act.user.ID)
|
||||
switch {
|
||||
@@ -283,11 +357,17 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
|
||||
}
|
||||
}
|
||||
|
||||
// limitText renders a limit refusal for the user.
|
||||
// limitText renders a limit refusal for the user as plain text.
|
||||
func (b *Bot) limitText(lang string, le *service.LimitError) string {
|
||||
l := le.Decision.Limits
|
||||
count := l.ActiveJobs
|
||||
if le.Code == service.LimitDailyDownloads {
|
||||
count = l.DailyDownloads
|
||||
}
|
||||
return b.tr.T(lang, "limit."+le.Code, i18n.Args{
|
||||
"MaxSize": b.tr.Size(lang, le.Limits.MaxSize),
|
||||
"MaxDuration": b.tr.Span(lang, secondsDuration(le.Limits.MaxDuration)),
|
||||
"Count": le.Limits.ActiveJobs,
|
||||
"MaxSize": b.tr.Size(lang, l.MaxSize),
|
||||
"MaxDuration": b.tr.Span(lang, secondsDuration(l.MaxDuration)),
|
||||
"MaxVolume": b.tr.Size(lang, l.DailyBytes),
|
||||
"Count": count,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user