2023-08-19 09:12:26 +03:00
|
|
|
package tg
|
2023-08-12 14:35:33 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
//"flag"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
Name CommandName
|
2023-09-30 09:55:45 +03:00
|
|
|
Type CommandType
|
2023-08-12 14:35:33 +03:00
|
|
|
Description string
|
2023-09-21 20:32:24 +03:00
|
|
|
Action Action
|
2023-09-11 13:37:04 +03:00
|
|
|
Widget Widget
|
2023-08-12 14:35:33 +03:00
|
|
|
}
|
2023-08-13 15:37:36 +03:00
|
|
|
type CommandMap map[CommandName]*Command
|
2023-08-12 14:35:33 +03:00
|
|
|
|
2023-10-11 14:45:35 +03:00
|
|
|
func NewCommand(name CommandName, desc string) *Command {
|
|
|
|
if name == "" || desc == "" {
|
|
|
|
panic("name and description cannot be an empty string")
|
|
|
|
}
|
2023-08-12 14:35:33 +03:00
|
|
|
return &Command{
|
|
|
|
Name: name,
|
2023-10-11 14:45:35 +03:00
|
|
|
Description: desc,
|
2023-08-12 14:35:33 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) ActionFunc(af ActionFunc) *Command {
|
|
|
|
return c.WithAction(af)
|
|
|
|
}
|
|
|
|
|
2023-09-11 13:37:04 +03:00
|
|
|
func (c *Command) WithWidget(w Widget) *Command {
|
|
|
|
c.Widget = w
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:13:31 +03:00
|
|
|
func (c *Command) WidgetFunc(fn Func) *Command {
|
2023-09-11 13:37:04 +03:00
|
|
|
return c.WithWidget(fn)
|
|
|
|
}
|
|
|
|
|
2023-09-11 13:00:38 +03:00
|
|
|
func (c *Command) ToApi() tgbotapi.BotCommand {
|
|
|
|
ret := tgbotapi.BotCommand{}
|
|
|
|
ret.Command = string(c.Name)
|
|
|
|
ret.Description = c.Description
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:28:06 +03:00
|
|
|
func (c *Command) Go(pth Path, args ...any) *Command {
|
|
|
|
return c.WithAction(ScreenGo{
|
|
|
|
Path: pth,
|
|
|
|
Args: args,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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.
|
2023-09-29 13:36:37 +03:00
|
|
|
func NewCommandCompo(cmds ...*Command) *CommandCompo {
|
|
|
|
ret := (&CommandCompo{}).WithCommands(cmds...)
|
|
|
|
//ret.Commands = make(CommandMap)
|
2023-09-11 15:58:45 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the commands to handle.
|
2023-09-27 14:09:49 +03:00
|
|
|
func (w *CommandCompo) WithCommands(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.
|
2023-09-27 14:09:49 +03:00
|
|
|
func (w *CommandCompo) WithPreStart(a Action) *CommandCompo {
|
2023-09-11 15:58:45 +03:00
|
|
|
w.PreStart = a
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the prestart action with function.
|
2023-09-27 14:09:49 +03:00
|
|
|
func (w *CommandCompo) WithPreStartFunc(fn ActionFunc) *CommandCompo {
|
2023-09-11 15:58:45 +03:00
|
|
|
return w.WithPreStart(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the usage action.
|
2023-09-27 14:09:49 +03:00
|
|
|
func (w *CommandCompo) WithUsage(a Action) *CommandCompo {
|
2023-09-11 15:58:45 +03:00
|
|
|
w.Usage = a
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the usage action with function.
|
2023-09-27 14:09:49 +03:00
|
|
|
func (w *CommandCompo) WithUsageFunc(fn ActionFunc) *CommandCompo {
|
2023-09-11 15:58:45 +03:00
|
|
|
return w.WithUsage(fn)
|
|
|
|
}
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
func (widget *CommandCompo) Filter(
|
2023-09-16 14:34:17 +03:00
|
|
|
u *Update,
|
|
|
|
) 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.
|
|
|
|
func (compo *CommandCompo) Serve(c *Context) {
|
2023-09-30 09:55:45 +03:00
|
|
|
/*commanders := make(map[CommandName] BotCommander)
|
2023-09-27 14:09:49 +03:00
|
|
|
for k, v := range compo.Commands {
|
2023-09-11 15:58:45 +03:00
|
|
|
commanders[k] = v
|
2023-09-30 09:55:45 +03:00
|
|
|
}*/
|
|
|
|
c.Bot.DeleteCommands()
|
2023-10-11 14:45:35 +03:00
|
|
|
err := c.Bot.SetCommands(
|
|
|
|
tgbotapi.NewBotCommandScopeChat(c.Session.Id.ToApi()),
|
2023-09-30 09:55:45 +03:00
|
|
|
compo.Commands,
|
2023-09-11 15:58:45 +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() {
|
2023-09-21 12:03:54 +03:00
|
|
|
if c.Path() == "" && 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.
|
|
|
|
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 {
|
2023-09-16 15:40:30 +03:00
|
|
|
cmdUpdates.Close()
|
2024-01-17 16:56:13 +03:00
|
|
|
cmdUpdates, _ = c.WithUpdate(u).RunWidget(cmd.Widget)
|
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 {
|
|
|
|
c.Skip(u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|