tg/beh.go
2024-03-29 16:30:48 +05:00

54 lines
1.1 KiB
Go

package tg
// The type describes behaviour for the bot in personal chats.
type Behaviour struct {
Root Component
Init Action
Screens ScreenMap
}
// Returns new empty behaviour.
func NewBehaviour() *Behaviour {
return &Behaviour{
Screens: make(ScreenMap),
}
}
// The Action will be called on session creation,
// not when starting or restarting the bot with the Start Action.
func (b *Behaviour) SetInit(a Action) *Behaviour {
b.Init = a
return b
}
// Sets the root node of the Behaviour.
// Mostly used for commands and such stuff.
func (b *Behaviour) SetRootNode(node *RootNode) *Behaviour {
b.Screens = node.ScreenMap()
return b
}
// The function sets as the standard root widget CommandWidget
// and its commands..
func (b *Behaviour) SetRootWidget(root Component) *Behaviour {
b.Root = root
return b
}
// Check whether the screen exists in the behaviour.
func (beh *Behaviour) PathExist(pth Path) bool {
_, ok := beh.Screens[pth]
return ok
}
// Returns the screen by it's ID.
func (beh *Behaviour) GetScreen(pth Path) *Screen {
pth = pth.Clean()
if !beh.PathExist(pth) {
panic(ScreenNotExistErr)
}
screen := beh.Screens[pth]
return screen
}