Files
MichilisandClaude Fable 5.2 0d50f453a0 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>
2026-11-18 08:09:35 +00:00

113 lines
3.8 KiB
Go

package bot
import (
"strings"
"github.com/mymmrac/telego"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/internal/media"
"github.com/noderunners/dlbot/internal/service"
)
// Callback data is "<action>:<id>[:<arg>]". It carries identifiers only,
// never translated text, so buttons keep working when the language changes.
const (
cbDownload = "dl" // dl:<request id>:<profile>
cbCancelCard = "cx" // cx:<request id>
cbCancelJob = "jc" // jc:<delivery id>
cbSkipQueue = "sq" // sq:<delivery id>
cbBuy = "by" // by:<product>[:<amount>]
cbOfferBuy = "ob" // ob:<product>:<request id>:<profile>
cbPayCheck = "pc" // pc:<purchase id>
cbPayCancel = "px" // px:<purchase id>
)
func cbData(parts ...string) string { return strings.Join(parts, ":") }
// parseCallback splits callback data into the action, the ID and the rest.
func parseCallback(data string) (action, id, arg string) {
parts := strings.SplitN(data, ":", 3)
action = parts[0]
if len(parts) > 1 {
id = parts[1]
}
if len(parts) > 2 {
arg = parts[2]
}
return
}
func button(text, data string) telego.InlineKeyboardButton {
return telego.InlineKeyboardButton{Text: text, CallbackData: data}
}
func keyboard(rows ...[]telego.InlineKeyboardButton) *telego.InlineKeyboardMarkup {
var kept [][]telego.InlineKeyboardButton
for _, r := range rows {
if len(r) > 0 {
kept = append(kept, r)
}
}
return &telego.InlineKeyboardMarkup{InlineKeyboard: kept}
}
// cardProfiles picks the video buttons worth showing: Best always, then the
// lower resolutions the source can actually be scaled down to.
func cardProfiles(info *service.CardInfo) []media.Profile {
out := []media.Profile{media.Best}
for _, p := range []media.Profile{media.P720, media.P480} {
if info.MaxHeight == 0 || info.MaxHeight > p.Height() {
out = append(out, p)
}
}
return out
}
// cardKeyboard builds the format buttons of a link card.
func (b *Bot) cardKeyboard(lang, requestID string, info *service.CardInfo) *telego.InlineKeyboardMarkup {
label := func(p media.Profile) string {
name := b.profileLabel(lang, p)
if size := info.Sizes[string(p)]; size > 0 {
return b.tr.T(lang, "btn.with_size", i18n.Args{"Label": name, "Size": b.tr.Size(lang, size)})
}
return name
}
var video, extra []telego.InlineKeyboardButton
for _, p := range cardProfiles(info) {
video = append(video, button(label(p), cbData(cbDownload, requestID, string(p))))
}
if info.HasAudio {
extra = append(extra, button(label(media.MP3), cbData(cbDownload, requestID, string(media.MP3))))
}
if info.Thumbnail != "" {
extra = append(extra, button(b.profileLabel(lang, media.Thumb), cbData(cbDownload, requestID, string(media.Thumb))))
}
cancel := []telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.cancel", nil), cbData(cbCancelCard, requestID))}
return keyboard(video, extra, cancel)
}
// cancelJobKeyboard is the single Cancel button under a live status.
func (b *Bot) cancelJobKeyboard(lang, deliveryID string) *telego.InlineKeyboardMarkup {
return keyboard([]telego.InlineKeyboardButton{
button(b.tr.T(lang, "btn.cancel", nil), cbData(cbCancelJob, deliveryID)),
})
}
// queuedKeyboard adds "Skip the queue" to the Cancel button of a job that
// waits in the free lane.
func (b *Bot) queuedKeyboard(lang, deliveryID string) *telego.InlineKeyboardMarkup {
skip := b.tr.T(lang, "btn.with_size", i18n.Args{
"Label": b.tr.T(lang, "btn.skip_queue", nil),
"Size": b.tr.Sats(lang, b.cfg.Prices.SkipQueue),
})
return keyboard(
[]telego.InlineKeyboardButton{button(skip, cbData(cbSkipQueue, deliveryID))},
[]telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.cancel", nil), cbData(cbCancelJob, deliveryID))},
)
}
func urlButton(text, url string) telego.InlineKeyboardButton {
return telego.InlineKeyboardButton{Text: text, URL: url}
}