2023-08-19 09:12:26 +03:00
|
|
|
package tg
|
2023-08-10 15:49:25 +03:00
|
|
|
|
|
|
|
// The package implements
|
|
|
|
// behaviour for the Telegram bots.
|
|
|
|
|
2023-08-12 14:35:33 +03:00
|
|
|
// The type describes behaviour for the bot in personal chats.
|
2023-08-10 15:49:25 +03:00
|
|
|
type Behaviour struct {
|
2023-09-27 14:09:49 +03:00
|
|
|
Root Component
|
2023-09-21 20:32:24 +03:00
|
|
|
Init Action
|
2023-08-10 15:49:25 +03:00
|
|
|
Screens ScreenMap
|
2023-08-12 14:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// Returns new empty behaviour.
|
|
|
|
func NewBehaviour() *Behaviour {
|
|
|
|
return &Behaviour{
|
2023-09-11 15:58:45 +03:00
|
|
|
Screens: make(ScreenMap),
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 17:25:56 +03:00
|
|
|
// The Action will be called on session creation,
|
|
|
|
// not when starting or restarting the bot with the Start Action.
|
|
|
|
func (b *Behaviour) WithInit(a Action) *Behaviour {
|
2023-09-21 20:32:24 +03:00
|
|
|
b.Init = a
|
2023-08-10 15:49:25 +03:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-09-07 15:45:38 +03:00
|
|
|
// Alias to WithInit to simplify behaviour definitions.
|
2023-08-16 17:25:56 +03:00
|
|
|
func (b *Behaviour) WithInitFunc(
|
2023-08-10 15:49:25 +03:00
|
|
|
fn ActionFunc,
|
|
|
|
) *Behaviour {
|
2023-08-16 17:25:56 +03:00
|
|
|
return b.WithInit(fn)
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|
|
|
|
|
2023-09-21 12:03:54 +03:00
|
|
|
func (b *Behaviour) WithRootNode(node *RootNode) *Behaviour {
|
|
|
|
b.Screens = node.ScreenMap()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// The function sets screens.
|
2023-09-21 12:03:54 +03:00
|
|
|
/*func (b *Behaviour) WithScreens(
|
2023-08-10 15:49:25 +03:00
|
|
|
screens ...*Screen,
|
|
|
|
) *Behaviour {
|
|
|
|
for _, screen := range screens {
|
|
|
|
if screen.Id == "" {
|
|
|
|
panic("empty screen ID")
|
|
|
|
}
|
|
|
|
_, ok := b.Screens[screen.Id]
|
|
|
|
if ok {
|
|
|
|
panic("duplicate keyboard IDs")
|
|
|
|
}
|
|
|
|
b.Screens[screen.Id] = screen
|
|
|
|
}
|
|
|
|
return b
|
2023-09-21 12:03:54 +03:00
|
|
|
}*/
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-09-11 15:58:45 +03:00
|
|
|
// The function sets as the standard root widget CommandWidget
|
|
|
|
// and its commands..
|
2023-09-27 14:09:49 +03:00
|
|
|
func (b *Behaviour) WithRoot(root Component) *Behaviour {
|
|
|
|
b.Root = root
|
2023-08-12 14:35:33 +03:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// Check whether the screen exists in the behaviour.
|
2023-09-21 12:03:54 +03:00
|
|
|
func (beh *Behaviour) PathExist(pth Path) bool {
|
|
|
|
_, ok := beh.Screens[pth]
|
2023-08-10 15:49:25 +03:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the screen by it's ID.
|
2023-09-21 12:03:54 +03:00
|
|
|
func (beh *Behaviour) GetScreen(pth Path) *Screen {
|
|
|
|
pth = pth.Clean()
|
|
|
|
if !beh.PathExist(pth) {
|
2023-08-10 15:49:25 +03:00
|
|
|
panic(ScreenNotExistErr)
|
|
|
|
}
|
|
|
|
|
2023-09-21 12:03:54 +03:00
|
|
|
screen := beh.Screens[pth]
|
2023-08-10 15:49:25 +03:00
|
|
|
return screen
|
|
|
|
}
|
2023-08-13 15:37:36 +03:00
|
|
|
|