2023-08-10 15:49:25 +03:00
|
|
|
package tx
|
|
|
|
|
|
|
|
// 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-08-16 17:25:56 +03:00
|
|
|
Init *action
|
2023-08-10 15:49:25 +03:00
|
|
|
Screens ScreenMap
|
|
|
|
Keyboards KeyboardMap
|
2023-08-12 14:35:33 +03:00
|
|
|
Commands CommandMap
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// Returns new empty behaviour.
|
|
|
|
func NewBehaviour() *Behaviour {
|
|
|
|
return &Behaviour{
|
|
|
|
Screens: make(ScreenMap),
|
|
|
|
Keyboards: make(KeyboardMap),
|
2023-08-12 14:35:33 +03:00
|
|
|
Commands: make(CommandMap),
|
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 {
|
|
|
|
b.Init = newAction(a)
|
2023-08-10 15:49:25 +03:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// The function sets screens.
|
|
|
|
func (b *Behaviour) WithScreens(
|
|
|
|
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-08-12 14:35:33 +03:00
|
|
|
// The function sets commands.
|
|
|
|
func (b *Behaviour) WithCommands(cmds ...*Command) *Behaviour {
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if cmd.Name == "" {
|
|
|
|
panic("empty command name")
|
|
|
|
}
|
|
|
|
_, ok := b.Commands[cmd.Name]
|
|
|
|
if ok {
|
|
|
|
panic("duplicate command definition")
|
|
|
|
}
|
|
|
|
b.Commands[cmd.Name] = cmd
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// Check whether the screen exists in the behaviour.
|
|
|
|
func (beh *Behaviour) ScreenExist(id ScreenId) bool {
|
|
|
|
_, ok := beh.Screens[id]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the screen by it's ID.
|
|
|
|
func (beh *Behaviour) GetScreen(id ScreenId) *Screen {
|
|
|
|
if !beh.ScreenExist(id) {
|
|
|
|
panic(ScreenNotExistErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
screen := beh.Screens[id]
|
|
|
|
return screen
|
|
|
|
}
|
2023-08-13 15:37:36 +03:00
|
|
|
|
|
|
|
// The type describes behaviour for the bot in group chats.
|
|
|
|
type GroupBehaviour struct {
|
|
|
|
Init GroupAction
|
|
|
|
// List of commands
|
|
|
|
Commands GroupCommandMap
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// Returns new empty group behaviour object.
|
2023-08-13 15:37:36 +03:00
|
|
|
func NewGroupBehaviour() *GroupBehaviour {
|
|
|
|
return &GroupBehaviour{
|
|
|
|
Commands: make(GroupCommandMap),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// Sets an Action for initialization on each group connected to the
|
|
|
|
// group bot.
|
2023-08-13 15:37:36 +03:00
|
|
|
func (b *GroupBehaviour) WithInitAction(a GroupAction) *GroupBehaviour {
|
|
|
|
b.Init = a
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// The method reciveies a function to be called on initialization of the
|
|
|
|
// bot group bot.
|
2023-08-13 15:37:36 +03:00
|
|
|
func (b *GroupBehaviour) InitFunc(fn GroupActionFunc) *GroupBehaviour {
|
|
|
|
return b.WithInitAction(fn)
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// The method sets group commands.
|
2023-08-13 15:37:36 +03:00
|
|
|
func (b *GroupBehaviour) WithCommands(
|
|
|
|
cmds ...*GroupCommand,
|
|
|
|
) *GroupBehaviour {
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if cmd.Name == "" {
|
|
|
|
panic("empty command name")
|
|
|
|
}
|
|
|
|
_, ok := b.Commands[cmd.Name]
|
|
|
|
if ok {
|
|
|
|
panic("duplicate command definition")
|
|
|
|
}
|
|
|
|
b.Commands[cmd.Name] = cmd
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2023-08-15 16:02:14 +03:00
|
|
|
|
|
|
|
// The type describes behaviour for the bot in channels.
|
|
|
|
type ChannelBehaviour struct {
|
|
|
|
}
|