2023-08-19 09:12:26 +03:00
|
|
|
package tg
|
2023-08-12 14:35:33 +03:00
|
|
|
|
|
|
|
import (
|
2023-09-11 13:00:38 +03:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-08-12 14:35:33 +03:00
|
|
|
)
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
|
2023-09-30 09:55:45 +03:00
|
|
|
type CommandType uint8
|
|
|
|
const (
|
|
|
|
PrivateCommandType CommandType = iota
|
|
|
|
GroupCommandType
|
|
|
|
ChannelCommandType
|
|
|
|
)
|
|
|
|
|
2023-08-12 14:35:33 +03:00
|
|
|
type CommandName string
|
|
|
|
type Command struct {
|
2024-03-28 10:41:09 +03:00
|
|
|
Name CommandName
|
2023-09-30 09:55:45 +03:00
|
|
|
Type CommandType
|
2023-08-12 14:35:33 +03:00
|
|
|
Description string
|
2024-03-28 10:41:09 +03:00
|
|
|
Action Action
|
2023-09-11 13:37:04 +03:00
|
|
|
Widget Widget
|
2024-03-29 14:30:48 +03:00
|
|
|
WidgetArg any
|
2023-08-12 14:35:33 +03:00
|
|
|
}
|
2024-03-28 10:41:09 +03:00
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
type CommandMap map[CommandName]Command
|
2023-08-12 14:35:33 +03:00
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func NewCommand(name CommandName, desc string) Command {
|
2023-10-11 14:45:35 +03:00
|
|
|
if name == "" || desc == "" {
|
|
|
|
panic("name and description cannot be an empty string")
|
|
|
|
}
|
2024-03-29 14:30:48 +03:00
|
|
|
return Command{
|
2023-08-12 14:35:33 +03:00
|
|
|
Name: name,
|
2023-10-11 14:45:35 +03:00
|
|
|
Description: desc,
|
2023-08-12 14:35:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Command) WithAction(a Action) Command {
|
2023-09-21 20:32:24 +03:00
|
|
|
c.Action = a
|
2023-08-12 14:35:33 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Command) WithWidget(w Widget) Command {
|
2023-09-11 13:37:04 +03:00
|
|
|
c.Widget = w
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
// Convert command into the tgbotapi.BotCommand
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Command) ToAPI() tgbotapi.BotCommand {
|
2023-09-11 13:00:38 +03:00
|
|
|
ret := tgbotapi.BotCommand{}
|
|
|
|
ret.Command = string(c.Name)
|
|
|
|
ret.Description = c.Description
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
// Simple command to go to another screen.
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Command) Go(pth Widget) Command {
|
|
|
|
return c.WithAction(WidgetGo{
|
2024-03-29 14:30:48 +03:00
|
|
|
Path: pth,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Command) GoWithArg(pth Widget, arg any) Command {
|
|
|
|
return c.WithAction(WidgetGo{
|
2023-09-21 15:28:06 +03:00
|
|
|
Path: pth,
|
2024-03-29 14:30:48 +03:00
|
|
|
Arg: arg,
|
2023-09-21 15:28:06 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:58:45 +03:00
|
|
|
// The type is used to recognize commands and execute
|
|
|
|
// its actions and widgets .
|
2023-09-27 14:09:49 +03:00
|
|
|
type CommandCompo struct {
|
2023-09-11 15:58:45 +03:00
|
|
|
PreStart Action
|
|
|
|
Commands CommandMap
|
|
|
|
Usage Action
|
|
|
|
}
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
// Returns new empty CommandCompo.
|
2024-03-29 14:30:48 +03:00
|
|
|
func NewCommandCompo(cmds ...Command) *CommandCompo {
|
|
|
|
ret := (&CommandCompo{}).SetCommands(cmds...)
|
2023-09-11 15:58:45 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the commands to handle.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (w *CommandCompo) SetCommands(cmds ...Command) *CommandCompo {
|
2023-09-29 13:36:37 +03:00
|
|
|
if w.Commands == nil {
|
|
|
|
w.Commands = make(CommandMap)
|
|
|
|
}
|
2023-09-11 15:58:45 +03:00
|
|
|
for _, cmd := range cmds {
|
|
|
|
if cmd.Name == "" {
|
|
|
|
panic("empty command name")
|
|
|
|
}
|
|
|
|
_, ok := w.Commands[cmd.Name]
|
|
|
|
if ok {
|
|
|
|
panic("duplicate command definition")
|
|
|
|
}
|
|
|
|
w.Commands[cmd.Name] = cmd
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the prestart action.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (w *CommandCompo) SetPreStart(a Action) *CommandCompo {
|
2023-09-11 15:58:45 +03:00
|
|
|
w.PreStart = a
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the usage action.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (w *CommandCompo) SetUsage(a Action) *CommandCompo {
|
2023-09-11 15:58:45 +03:00
|
|
|
w.Usage = a
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
// Filtering all the non commands.
|
|
|
|
func (widget *CommandCompo) Filter(
|
|
|
|
u Update,
|
2023-09-16 14:34:17 +03:00
|
|
|
) bool {
|
2023-09-27 14:09:49 +03:00
|
|
|
if u.Message == nil || !u.Message.IsCommand() {
|
2023-09-16 14:34:17 +03:00
|
|
|
return false
|
2023-09-27 14:09:49 +03:00
|
|
|
}
|
2023-09-16 14:34:17 +03:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
// Implementing server.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (compo *CommandCompo) Serve(c Context) {
|
|
|
|
// First should bring the new command into the action.
|
|
|
|
c.Bot().DeleteCommands()
|
|
|
|
err := c.Bot().SetCommands(
|
2024-07-21 16:02:47 +03:00
|
|
|
tgbotapi.NewBotCommandScopeChat(c.SessionID().ToAPI()),
|
2023-09-30 09:55:45 +03:00
|
|
|
compo.Commands,
|
2023-09-11 15:58:45 +03:00
|
|
|
)
|
2024-03-29 14:30:48 +03:00
|
|
|
|
2023-10-11 14:45:35 +03:00
|
|
|
if err != nil {
|
|
|
|
c.Sendf("error: %q", err)
|
|
|
|
}
|
2023-09-11 15:58:45 +03:00
|
|
|
|
2023-09-16 15:40:30 +03:00
|
|
|
var cmdUpdates *UpdateChan
|
2023-09-20 22:48:35 +03:00
|
|
|
for u := range c.Input() {
|
2024-07-21 16:02:47 +03:00
|
|
|
if c.Path() == nil && u.Message != nil {
|
2023-09-11 15:58:45 +03:00
|
|
|
// Skipping and executing the preinit action
|
|
|
|
// while we have the empty screen.
|
|
|
|
// E. g. the session did not start.
|
2024-07-21 16:02:47 +03:00
|
|
|
if !u.Message.IsCommand() ||
|
|
|
|
u.Message.Command() != "start" {
|
2023-09-27 14:09:49 +03:00
|
|
|
c.WithUpdate(u).Run(compo.PreStart)
|
2023-09-11 15:58:45 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Message != nil && u.Message.IsCommand() {
|
|
|
|
// Command handling.
|
|
|
|
cmdName := CommandName(u.Message.Command())
|
2023-09-27 14:09:49 +03:00
|
|
|
cmd, ok := compo.Commands[cmdName]
|
2023-09-11 15:58:45 +03:00
|
|
|
if !ok {
|
2023-09-27 14:09:49 +03:00
|
|
|
c.WithUpdate(u).Run(compo.Usage)
|
2023-09-11 15:58:45 +03:00
|
|
|
continue
|
2023-09-12 11:41:50 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
c.WithUpdate(u).Run(cmd.Action)
|
2023-09-11 15:58:45 +03:00
|
|
|
if cmd.Widget != nil {
|
2024-03-29 14:30:48 +03:00
|
|
|
// Closing current widget
|
2023-09-16 15:40:30 +03:00
|
|
|
cmdUpdates.Close()
|
2024-03-29 14:30:48 +03:00
|
|
|
// And running the other one.
|
|
|
|
cmdUpdates, _ = c.runWidget(cmd.Widget, cmd.WidgetArg)
|
2023-09-11 15:58:45 +03:00
|
|
|
}
|
2023-09-12 11:41:50 +03:00
|
|
|
continue
|
2023-09-11 15:58:45 +03:00
|
|
|
}
|
|
|
|
|
2023-09-20 22:48:35 +03:00
|
|
|
if !cmdUpdates.Closed() {
|
2023-09-11 15:58:45 +03:00
|
|
|
// Send to the commands channel if we are
|
|
|
|
// executing one.
|
2023-09-16 15:40:30 +03:00
|
|
|
cmdUpdates.Send(u)
|
2023-09-11 15:58:45 +03:00
|
|
|
} else {
|
2024-03-29 14:30:48 +03:00
|
|
|
c.SkipUpdate(u)
|
2023-09-11 15:58:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|