Finite State Machine for telebot. Based on aiogram FSM version.
It not a full implementation FSM. It just states manager for telegram bots.
This module build over github.com/vitaliy-ukiru/telebot-filter.
go get github.com/vitaliy-ukiru/fsm-telebot/v2
bot, err := tele.NewBot(tele.Settings{
Token: os.Getenv("BOT_TOKEN"),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
Verbose: *debug,
})
g = bot.Group()
m := fsm.New(memory.NewStorage())
Also, you can add optional settings. See fsmopt/manager.go
- fsmopt.Strategy - Setup custom strategy for user's targeting in storage
- fsmopt.ContextFactory - Setup custom FSM context factory
- fsmopt.FilterProcessor - Setup custom state filter processor
This middleware needs only for cache fsm.Context object in telebot.Context.
g.Use(m.WrapContext)
You can use any flow of telebot-filter
dp := dispatcher.NewDispatcher(g)
Optionally you can add router object from dispatcher.
These methods work and with router object.
dp.Dispatch(
m.New(
// List of options for fsm.HandlerConfig
// in fsmopt/handler.go
...,
),
)
m.Bind(
dp,
// List of options for fsm.HandlerConfig
// in fsmopt/handler.go
...,
)
m.Handle(
dp,
tele.OnText, // endpoint
MyState, // state filter as fsm.StateMatcher
handler, // handler as fsm.Handler
)
With this flow available only Manager.New
You don't need to pass endpoint option. Because routing package don't look on that.
bot.Handle("/start",
routing.New(
// don't specify the endpoint because it doesn't count.
m.New(
...
),
m.New(
...
),
),
)
See examples in directory examples.
FSM need storage user state and data for user.
This storage distributes with fsm-telebot/v2. It just in-memory storage, that lose data on stop app
github.com/vitaliy-ukiru/fsm-telebot/v2/pkg/storage/memory
This storage based on redis in distributes separated.
go get github.com/nacknime-official/fsm-telebot-redis-storage/v2
If you want to add you implementation in this list, then open pull request.