From 908d235cad3956885df844878c113b207e42eece Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 21 Sep 2023 15:28:06 +0300 Subject: [PATCH] Added the Go function for the Command structure. --- cmd/test/main.go | 34 ++++++++++++++++++++++------------ tg/command.go | 10 +++++++++- tg/private.go | 2 -- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index 088dc37..dd73e3d 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -4,6 +4,7 @@ import ( "log" "os" "strings" + "fmt" "github.com/mojosa-software/got/tg" ) @@ -116,7 +117,13 @@ WithInitFunc(func(c *tg.Context) { tg.NewKeyboard().Row( tg.NewButton("GoT Github page"). WithUrl("https://github.com/mojosa-software/got"), - ).Inline().Widget("The bot started!"), + ).Inline().Widget( + fmt.Sprint( + "The testing bot started!\n", + "You can see the basics of usage in the ", + "cmd/test/main.go file!", + ), + ), ).WithReply( navKeyboard.Widget("Choose what you are interested in"), ), @@ -128,24 +135,28 @@ WithInitFunc(func(c *tg.Context) { tg.NewButton("Lower case").Go("lower-case"), ).Row( backButton, - ).Reply().Widget( + ).Reply().WithOneTime(true).Widget( "Choose the function to mutate string", ), ), tg.NewNode( "upper-case", tg.NewPage().WithReply( - backKeyboard.Reply().Widget( - "Type a string and the bot will convert it to upper case", - ), + backKeyboard.Reply(). + WithOneTime(true). + Widget( + "Type a string and the bot will convert it to upper case", + ), ).WithSub( NewMutateMessageWidget(strings.ToUpper), ), ), tg.NewNode( "lower-case", tg.NewPage().WithReply( - backKeyboard.Reply().Widget( - "Type a string and the bot will convert it to lower case", - ), + backKeyboard.Reply(). + WithOneTime(true). + Widget( + "Type a string and the bot will convert it to lower case", + ), ).WithSub( NewMutateMessageWidget(strings.ToLower), ), @@ -180,10 +191,9 @@ WithInitFunc(func(c *tg.Context) { ), )).WithCommands( tg.NewCommand("start"). - Desc("start or restart the bot or move to the start screen"). - ActionFunc(func(c *tg.Context){ - c.Go("/") - }), + Desc( + "start or restart the bot or move to the start screen", + ).Go("/"), tg.NewCommand("hello"). Desc("sends the 'Hello, World!' message back"). ActionFunc(func(c *tg.Context) { diff --git a/tg/command.go b/tg/command.go index e4764db..8596852 100644 --- a/tg/command.go +++ b/tg/command.go @@ -56,6 +56,13 @@ func (c *Command) Desc(desc string) *Command { return c } +func (c *Command) Go(pth Path, args ...any) *Command { + return c.WithAction(ScreenGo{ + Path: pth, + Args: args, + }) +} + type GroupCommand struct { Name CommandName Description string @@ -78,6 +85,7 @@ func (cmd *GroupCommand) ActionFunc(fn GroupActionFunc) *GroupCommand { return cmd.WithAction(fn) } + func (cmd *GroupCommand) Desc(desc string) *GroupCommand { cmd.Description = desc return cmd @@ -142,7 +150,7 @@ func (w *CommandWidget) WithUsageFunc(fn ActionFunc) *CommandWidget { return w.WithUsage(fn) } -func (widget *Command) Filter( +func (widget *CommandWidget) Filter( u *Update, msgs ...*Message, ) bool { diff --git a/tg/private.go b/tg/private.go index 375a1ee..a25b195 100644 --- a/tg/private.go +++ b/tg/private.go @@ -154,8 +154,6 @@ func (c *Context) Go(pth Path, args ...any) error { pth = (c.Path() + "/" + pth).Clean() } - c.Sendf("path: %q\nmap: %v", pth, c.Bot.behaviour.Screens) - if !c.PathExist(pth) { return ScreenNotExistErr }