- 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>
48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
)
|
|
|
|
// Kinds of widget messages.
|
|
const (
|
|
WidgetJob = "job"
|
|
WidgetInvoice = "invoice"
|
|
)
|
|
|
|
// WidgetMessage is a message that shows an HTML widget.
|
|
type WidgetMessage struct {
|
|
ChatID int64
|
|
MessageID int64
|
|
Kind string
|
|
RefID string
|
|
}
|
|
|
|
// AddWidgetMessage records that a message is a widget for a delivery or a
|
|
// purchase.
|
|
func (s *Store) AddWidgetMessage(ctx context.Context, w WidgetMessage) error {
|
|
_, err := s.exec(ctx, `INSERT INTO widget_messages (chat_id, message_id, kind, ref_id, created_at)
|
|
VALUES (?, ?, ?, ?, ?) ON CONFLICT (chat_id, message_id) DO NOTHING`,
|
|
w.ChatID, w.MessageID, w.Kind, w.RefID, NowMilli())
|
|
return err
|
|
}
|
|
|
|
// GetWidgetMessage finds the widget shown in a message, or ErrNotFound.
|
|
func (s *Store) GetWidgetMessage(ctx context.Context, chatID, messageID int64) (*WidgetMessage, error) {
|
|
w := &WidgetMessage{}
|
|
err := s.row(ctx, `SELECT chat_id, message_id, kind, ref_id FROM widget_messages
|
|
WHERE chat_id = ? AND message_id = ?`, chatID, messageID).Scan(&w.ChatID, &w.MessageID, &w.Kind, &w.RefID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return w, err
|
|
}
|
|
|
|
// DeleteWidgetMessagesBefore forgets widget rows older than a cutoff. The
|
|
// messages stay in Telegram; nothing edits them after that long.
|
|
func (s *Store) DeleteWidgetMessagesBefore(ctx context.Context, before int64) (int64, error) {
|
|
return s.exec(ctx, `DELETE FROM widget_messages WHERE created_at < ?`, before)
|
|
}
|