Documentation
¶
Overview ¶
Package botswizard is a thin declarative wrapper over the framework's built-in wizard-param machinery (ChatData's AwaitingReplyTo plus AddWizardParam/GetWizardParam, where params ride in the AwaitingReplyTo query string and self-clear when it is reset).
Instead of hand-rolling one command per step and threading state by hand, you describe a wizard as an ordered list of typed Steps. The driver then handles:
- routing each reply to the current step,
- re-prompting on invalid input (Step.Parse returns an error),
- /cancel and /back,
- an optional inactivity TTL,
- typed access to the collected values,
- self-clearing state on completion.
One Wizard registers a single command (Wizard.Command); the current step index and all collected values live in the self-clearing wizard params, so there is no persistent state to leak.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Step ¶
type Step struct {
// Key is the wizard-param key the parsed value is stored under and the key
// it is later read from in Values. Must not start with "_wz".
Key string
// Prompt is the question shown when the step is entered (HTML allowed).
Prompt string
// Parse validates and normalizes the raw user text into the value to store.
// Returning an error re-prompts the SAME step with the error text shown
// above the prompt. A nil Parse stores the trimmed text as-is.
Parse func(raw string) (stored string, err error)
}
Step is one question in a wizard.
type Values ¶
Values holds the collected step values keyed by Step.Key, with typed getters.
type Wizard ¶
type Wizard struct {
// Code is the command code (also the AwaitingReplyTo path). Unique per bot.
Code botsfw.CommandCode
// Steps are asked in order.
Steps []Step
// OnComplete runs after the last step with all collected values. Its
// message is returned to the user. State is already cleared when it runs.
OnComplete func(whc botsfw.WebhookContext, v Values) (botmsg.MessageFromBot, error)
// Intro is optional text prepended to the first prompt.
Intro string
// TTL, if > 0, abandons the wizard when this long passes since Start.
TTL time.Duration
// CancelText / TimeoutText override the default end messages.
CancelText string
TimeoutText string
// Now overrides the clock for TTL (tests); nil = time.Now.
Now func() time.Time
}
Wizard is a declarative, multi-step, text-input dialog.
func (Wizard) Command ¶
Command returns the single command that drives every step of this wizard. Register it with the bot; arm the wizard from your entry command via Start.
func (Wizard) Start ¶
func (w Wizard) Start(whc botsfw.WebhookContext, prefill Values) (botmsg.MessageFromBot, error)
Start arms the wizard and sends the first prompt. prefill lets an entry command supply leading answers (e.g. "/commit run a 5k" prefilling the title); each is validated through its Step.Parse and, if valid, that step is skipped.