fsm-telebot
fsm-telebot copied to clipboard
Finite State Machine for Go telebot
fsm-telebot
- fsm-telebot
- Overview
- Install
-
Quick Start
- Create telebot.Bot instance
- Optionally create group (recommended)
-
Init FSM manager
- List of settings
- Optionally setup middleware for context
-
Bind handler via telebot-filter
-
telebot-filter/dispatcher
- Init dispatcher
- Setups handlers
- telebot-filter/routing
-
telebot-filter/dispatcher
- Examples
-
Storages
-
Memory storage
- Path:
-
Redis storage
- Install
- Add your implementation to list
-
Memory storage
Overview
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.
Install
go get github.com/vitaliy-ukiru/fsm-telebot/v2
Quick Start
Create telebot.Bot instance
bot, err := tele.NewBot(tele.Settings{
Token: os.Getenv("BOT_TOKEN"),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
Verbose: *debug,
})
Optionally create group (recommended)
g = bot.Group()
Init FSM manager
m := fsm.New(memory.NewStorage())
Also, you can add optional settings. See fsmopt/manager.go
List of settings
- 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
Optionally setup middleware for context
This middleware needs only for cache fsm.Context object in telebot.Context.
g.Use(m.WrapContext)
Bind handler via telebot-filter
You can use any flow of telebot-filter
telebot-filter/dispatcher
Init dispatcher
dp := dispatcher.NewDispatcher(g)
Optionally you can add router object from dispatcher.
Setups handlers
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
)
telebot-filter/routing
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(
...
),
),
)
Examples
See examples in directory examples.
Storages
FSM need storage user state and data for user.
Memory storage
This storage distributes with fsm-telebot/v2. It just in-memory storage, that lose data on stop app
Path:
github.com/vitaliy-ukiru/fsm-telebot/v2/pkg/storage/memory
Redis storage
This storage based on redis in distributes separated.
Install
go get github.com/nacknime-official/fsm-telebot-redis-storage/v2
Add your implementation to list
If you want to add you implementation in this list, then open pull request.