mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-09-14 16:19:29 +00:00
/alias <name> saves a replied message under a name and /insert <name> sends it back; /aliases lists every name and /unalias deletes one. Every Telegram format is supported — sticker, photo, GIF, video, video note, audio, voice, document, plain text — and each is kept as the file_id Telegram already issued, so nothing is downloaded and an alias survives redeploys. The namespace is global and the last assignment wins, matching the shared sticker pack; /unalias is open to anyone for the same reason. A saved name also works as its own command: /cheer rather than /insert cheer. This needs two new seams in the module contract. Module.Fallback handles a /command no module registered, and the dispatcher installs it after every Command — the bot library returns the first matching handler, so code always beats a name resolved at runtime, including an alias that shares a command added in a later build. /alias refuses a name already in the registry for the same reason, since such an alias would only reach /insert. An unknown command stays silent: the fallback sees every unrecognised /foo in every chat, so replying would make typos noisy and would confirm which names exist. Module.Inline answers inline-mode queries — "@botname <prefix>" from any chat, filtered by prefix and capped at Telegram's 50 results. Each result is a cached inline type carrying the stored file_id, so the picker renders real previews without an upload. Video notes are omitted because Telegram defines no InlineQueryResultCachedVideoNote and substituting a plain video would change what was saved. Inline mode must be enabled in BotFather before Telegram delivers these updates. Both slots are single-occupancy with conflict detection at Build. Auth.Permits learns the inline sender so a gated inline handler would not deny everyone. Build's command indexing and slot claiming move into addCommands/addSingletons, keeping it under the project's cyclomatic cap. Also restores the sticker module: /addsticker moves back out of util, which has no store, into internal/modules/sticker as its only command.
124 lines
5.5 KiB
Go
124 lines
5.5 KiB
Go
package modules
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-telegram/bot"
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
"github.com/tiennm99/miti99bot/internal/storage"
|
|
)
|
|
|
|
// Visibility classifies who may invoke a command. The dispatcher enforces
|
|
// this at command-handler entry: Public is unrestricted; Protected requires
|
|
// the sender to be in Auth.AdminUserIDs (or be the bot owner); Private
|
|
// requires the sender to be Auth.BotOwnerID. /help filters by the same field.
|
|
type Visibility int
|
|
|
|
const (
|
|
VisibilityPublic Visibility = iota
|
|
VisibilityProtected
|
|
VisibilityPrivate
|
|
)
|
|
|
|
// CommandHandler runs in response to a Telegram command. Returning an error
|
|
// causes the dispatcher to log the failure. Telegram retries are governed by
|
|
// the webhook HTTP status (200), not handler errors — so the error return is
|
|
// purely for logging/metrics, not flow control.
|
|
type CommandHandler func(ctx context.Context, b *bot.Bot, update *models.Update) error
|
|
|
|
// CallbackHandler runs in response to inline-keyboard callback data. Callback
|
|
// payloads are not commands and therefore do not participate in command stats.
|
|
type CallbackHandler func(ctx context.Context, b *bot.Bot, update *models.Update) error
|
|
|
|
// Callback registers an inline-keyboard callback-data prefix owned by a module.
|
|
// Prefixes must be globally non-overlapping so one callback reaches one owner.
|
|
type Callback struct {
|
|
Prefix string
|
|
Visibility Visibility
|
|
Handler CallbackHandler
|
|
}
|
|
|
|
// CronHandler runs when a cron fires — driven by the in-process scheduler
|
|
// (internal/cron) on self-host, or by a POST to /cron/{name} for manual
|
|
// triggers. Crons receive the per-module-prefixed Deps via the registry;
|
|
// handlers should not capture the base Deps from the factory closure or KV
|
|
// writes will collide across modules.
|
|
type CronHandler func(ctx context.Context, deps Deps) error
|
|
|
|
// Command is a single Telegram bot command exposed by a module.
|
|
type Command struct {
|
|
Name string // ^[a-z0-9_]{1,32}$ — Telegram BotFather rules
|
|
Visibility Visibility // public/protected/private
|
|
Description string // concise summary shown in command discovery (required, non-empty)
|
|
Parameters string // optional syntax after the command, e.g. "<quantity> <ticker>"
|
|
Handler CommandHandler // required
|
|
}
|
|
|
|
// Cron is a single scheduled job exposed by a module.
|
|
type Cron struct {
|
|
Schedule string // 5-field cron expr (UTC); the in-process scheduler fires the handler on it
|
|
Name string // unique within module
|
|
Handler CronHandler // required
|
|
}
|
|
|
|
// Module is a self-contained feature unit: a name plus zero or more commands
|
|
// and crons. Modules are constructed by Factory functions that capture their
|
|
// per-module Deps via closure.
|
|
//
|
|
// Module.Name is overridden by the registry to its catalog key; factories may
|
|
// leave it blank.
|
|
type Module struct {
|
|
Name string
|
|
Commands []Command
|
|
Callbacks []Callback
|
|
Crons []Cron
|
|
CommandHook func(ctx context.Context, name string, update *models.Update) // optional; called by dispatcher after each authorized command invocation. update carries the originating Telegram update so hooks can attribute usage to a user.
|
|
Fallback *CommandFallback // optional; handles a /command no module registered. At most one across all modules.
|
|
Inline *InlineQuery // optional; handles inline-mode queries. At most one across all modules.
|
|
}
|
|
|
|
// CommandFallback handles a /command that no module registered.
|
|
//
|
|
// Install registers it after every Command, and the bot library returns the
|
|
// *first* matching handler, so a registered command can never reach here. That
|
|
// ordering is the whole mechanism: code always wins over anything resolved at
|
|
// runtime, including an alias that shares a command's name.
|
|
//
|
|
// Name is the parsed command, lowercased with any @botname suffix stripped —
|
|
// the same normalisation matchCommand applies — so a fallback never re-parses
|
|
// the entity itself.
|
|
type CommandFallback struct {
|
|
Visibility Visibility
|
|
Handler func(ctx context.Context, b *bot.Bot, name string, update *models.Update) error
|
|
}
|
|
|
|
// InlineQuery handles inline-mode queries ("@botname <text>" typed in any chat).
|
|
//
|
|
// The bot library has no HandlerType for inline queries, so Install matches
|
|
// update.InlineQuery itself. Inline mode must also be enabled for the bot in
|
|
// BotFather; without that Telegram never delivers these updates and the handler
|
|
// is simply never called.
|
|
type InlineQuery struct {
|
|
Visibility Visibility
|
|
Handler func(ctx context.Context, b *bot.Bot, update *models.Update) error
|
|
}
|
|
|
|
// Deps is the dependency bundle a Factory receives.
|
|
//
|
|
// Deps.Registry is a pointer to the Registry being built. At factory call
|
|
// time the Registry is partially populated (only modules earlier in the
|
|
// MODULES env order); by the time any handler runs, it is fully populated.
|
|
// Modules that need to introspect commands (e.g. /help) capture this pointer
|
|
// in their handler closures.
|
|
type Deps struct {
|
|
Store storage.Collection // the module's own collection; build typed views with storage.Typed[T]
|
|
Registry *Registry // populated by Build; safe to capture but read-only at module use
|
|
Bot *bot.Bot // nil-safe: only crons that fan-out (lol daily push) need it
|
|
}
|
|
|
|
// Factory constructs a Module from its Deps. Deps are passed directly (instead
|
|
// of a separate Init step) so handler closures can capture them — idiomatic Go
|
|
// and removes a lifecycle ordering trap.
|
|
type Factory func(deps Deps) Module
|