state

package
v2.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: GPL-3.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package state provides persistent sessions and conversational FSM scenes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttachSession

func AttachSession(ctx *botctx.Context, session Session)

func SessionMiddleware

func SessionMiddleware(repo storage.SessionRepository, ttl time.Duration) middleware.Middleware

SessionMiddleware serializes updates to the same session key within this process. Multi-process stores must provide their own cross-process control.

Types

type FSM

type FSM struct {
	// contains filtered or unexported fields
}

FSM registers scenes and routes active sessions to their current step.

func NewFSM

func NewFSM(repo storage.SessionRepository, ttl time.Duration) *FSM

NewFSM creates a scene manager backed by a session repository.

func (*FSM) Enter

func (f *FSM) Enter(ctx *botctx.Context, sceneName string) error

Enter activates a registered scene for the current user and chat.

func (*FSM) Exit

func (f *FSM) Exit(ctx *botctx.Context) error

Exit clears the active scene and its persisted session.

func (*FSM) Handle

func (f *FSM) Handle(ctx *botctx.Context) (bool, error)

Handle processes the active scene and reports whether it consumed the event.

func (*FSM) Middleware

func (f *FSM) Middleware() middleware.Middleware

func (*FSM) Scene

func (f *FSM) Scene(scene *Scene)

Scene registers a scene by name.

type Scene

type Scene struct {
	// contains filtered or unexported fields
}

Scene is an ordered conversational flow of named steps.

func NewScene

func NewScene(name string, ttl time.Duration) *Scene

NewScene creates an empty named scene.

func (*Scene) CurrentStep

func (s *Scene) CurrentStep(session Session) string

func (*Scene) Handle

func (s *Scene) Handle(ctx *botctx.Context, session Session) error

func (*Scene) Next

func (s *Scene) Next(session Session)

func (*Scene) Step

func (s *Scene) Step(name string, h StepHandler) *Scene

Step appends a named handler to the scene.

type Session

type Session interface {
	Get(key string) any
	Set(key string, value any)
	Delete(key string)
	Values() map[string]any
	Save(ctx context.Context) error
}

Session stores mutable scene data and persists it through a repository.

func LoadSession added in v2.0.2

func LoadSession(ctx context.Context, repo storage.SessionRepository, key string, ttl time.Duration) (Session, error)

LoadSession loads a session, returning storage and decoding failures to the caller.

func NewSession

func NewSession(repo storage.SessionRepository, key string, ttl time.Duration) Session

NewSession is kept for source compatibility. Prefer LoadSession when load failures must be handled before running application code.

func SessionFromContext

func SessionFromContext(ctx *botctx.Context) Session

type StepHandler

type StepHandler func(*botctx.Context, Session) error

StepHandler processes one step in a scene.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL