Phase 8: widgets and polish

- Live job card widget (progress bar, in-widget cancel via widget_query)
  and live invoice widget (QR drawn from a bitmap, countdown, paid state),
  Bot API 10.4 sendWidget, private chats only
- Automatic fallback to the ordinary message when sendWidget fails, with
  a breaker that pauses widgets after repeated failures or on servers
  without the method; TELEGRAM_WIDGETS
- Prometheus metrics on the core (/metrics) and on every worker: queue
  depth and wait, slots, heartbeats, job outcomes, errors per site,
  deliveries, cache, invoices, Lightning, ledger check, widgets
- Example Prometheus config and alert rules, Caddyfile, dashboard notes
- Load test: cmd/loadtest and deploy/loadtest (make loadtest)
- Browser check of the widgets' HTML, including decoding a drawn QR
- docs/DEPLOY.md (production in under an hour) and docs/MONITORING.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 18:24:36 +00:00
co-authored by Claude Opus 5.5
parent f1f9c6156c
commit 3c153af97f
45 changed files with 3158 additions and 20 deletions
+39
View File
@@ -19,10 +19,12 @@ import (
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/groups"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/internal/metrics"
"github.com/noderunners/dlbot/internal/sched"
"github.com/noderunners/dlbot/internal/service"
"github.com/noderunners/dlbot/internal/storage"
"github.com/noderunners/dlbot/internal/tg"
"github.com/noderunners/dlbot/internal/widget"
)
// Deps are the collaborators of the bot.
@@ -46,6 +48,8 @@ type Deps struct {
Admins *groups.Admins
// ReloadI18n reloads the catalogs from disk. Nil when they are built in.
ReloadI18n func() error
// Metrics records what happens. Nil records nothing.
Metrics *metrics.Metrics
}
// Bot handles Telegram updates and job events.
@@ -74,6 +78,13 @@ type Bot struct {
bg sync.WaitGroup
baseCtx context.Context
metrics *metrics.Metrics
// widgets decides whether HTML widgets are tried; nil turns them off.
widgets *widget.Breaker
liveMu sync.Mutex
live map[string]*liveJob // status widgets by delivery ID
liveTried map[string]int // deliveries a status widget was tried for
statusMu sync.Mutex
statuses map[string]*statusState // by delivery ID
sending map[string]bool // jobs being delivered right now
@@ -130,6 +141,9 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
rate: newRateLimiter(),
baseCtx: ctx,
statuses: map[string]*statusState{},
metrics: d.Metrics,
live: map[string]*liveJob{},
liveTried: map[string]int{},
sending: map[string]bool{},
pumping: map[int64]bool{},
cancelledAt: map[int64]time.Time{},
@@ -144,6 +158,9 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
if b.admins == nil {
b.admins = groups.NewAdmins(d.API, 0)
}
if d.Config.Telegram.Widgets {
b.widgets = widget.NewBreaker()
}
d.Service.SetAdmins(b.admins)
d.Sched.Subscribe(func(ev sched.Event) {
select {
@@ -261,12 +278,18 @@ func updateChatID(u telego.Update) int64 {
return u.CallbackQuery.From.ID
case u.MyChatMember != nil:
return u.MyChatMember.Chat.ID
case u.WidgetQuery != nil:
if u.WidgetQuery.Chat != nil {
return u.WidgetQuery.Chat.ID
}
return u.WidgetQuery.From.ID
}
return int64(u.UpdateID)
}
// HandleUpdate routes one update.
func (b *Bot) HandleUpdate(ctx context.Context, u telego.Update) {
b.metrics.Update(updateType(u))
switch {
case u.Message != nil:
b.handleMessage(ctx, u.Message)
@@ -274,9 +297,25 @@ func (b *Bot) HandleUpdate(ctx context.Context, u telego.Update) {
b.handleCallback(ctx, u.CallbackQuery)
case u.MyChatMember != nil:
b.handleMyChatMember(ctx, u.MyChatMember)
case u.WidgetQuery != nil:
b.handleWidgetQuery(ctx, u.WidgetQuery)
}
}
func updateType(u telego.Update) string {
switch {
case u.Message != nil:
return "message"
case u.CallbackQuery != nil:
return "callback_query"
case u.MyChatMember != nil:
return "my_chat_member"
case u.WidgetQuery != nil:
return "widget_query"
}
return "other"
}
// actor is the user behind an update, with the language to answer in.
type actor struct {
user *db.User