Added the Go function for the Command structure.

This commit is contained in:
Andrey Parhomenko 2023-09-21 15:28:06 +03:00
parent 7d149558f9
commit 908d235cad
3 changed files with 31 additions and 15 deletions

View file

@ -4,6 +4,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"fmt"
"github.com/mojosa-software/got/tg" "github.com/mojosa-software/got/tg"
) )
@ -116,7 +117,13 @@ WithInitFunc(func(c *tg.Context) {
tg.NewKeyboard().Row( tg.NewKeyboard().Row(
tg.NewButton("GoT Github page"). tg.NewButton("GoT Github page").
WithUrl("https://github.com/mojosa-software/got"), 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( ).WithReply(
navKeyboard.Widget("Choose what you are interested in"), navKeyboard.Widget("Choose what you are interested in"),
), ),
@ -128,24 +135,28 @@ WithInitFunc(func(c *tg.Context) {
tg.NewButton("Lower case").Go("lower-case"), tg.NewButton("Lower case").Go("lower-case"),
).Row( ).Row(
backButton, backButton,
).Reply().Widget( ).Reply().WithOneTime(true).Widget(
"Choose the function to mutate string", "Choose the function to mutate string",
), ),
), ),
tg.NewNode( tg.NewNode(
"upper-case", tg.NewPage().WithReply( "upper-case", tg.NewPage().WithReply(
backKeyboard.Reply().Widget( backKeyboard.Reply().
"Type a string and the bot will convert it to upper case", WithOneTime(true).
), Widget(
"Type a string and the bot will convert it to upper case",
),
).WithSub( ).WithSub(
NewMutateMessageWidget(strings.ToUpper), NewMutateMessageWidget(strings.ToUpper),
), ),
), ),
tg.NewNode( tg.NewNode(
"lower-case", tg.NewPage().WithReply( "lower-case", tg.NewPage().WithReply(
backKeyboard.Reply().Widget( backKeyboard.Reply().
"Type a string and the bot will convert it to lower case", WithOneTime(true).
), Widget(
"Type a string and the bot will convert it to lower case",
),
).WithSub( ).WithSub(
NewMutateMessageWidget(strings.ToLower), NewMutateMessageWidget(strings.ToLower),
), ),
@ -180,10 +191,9 @@ WithInitFunc(func(c *tg.Context) {
), ),
)).WithCommands( )).WithCommands(
tg.NewCommand("start"). tg.NewCommand("start").
Desc("start or restart the bot or move to the start screen"). Desc(
ActionFunc(func(c *tg.Context){ "start or restart the bot or move to the start screen",
c.Go("/") ).Go("/"),
}),
tg.NewCommand("hello"). tg.NewCommand("hello").
Desc("sends the 'Hello, World!' message back"). Desc("sends the 'Hello, World!' message back").
ActionFunc(func(c *tg.Context) { ActionFunc(func(c *tg.Context) {

View file

@ -56,6 +56,13 @@ func (c *Command) Desc(desc string) *Command {
return c return c
} }
func (c *Command) Go(pth Path, args ...any) *Command {
return c.WithAction(ScreenGo{
Path: pth,
Args: args,
})
}
type GroupCommand struct { type GroupCommand struct {
Name CommandName Name CommandName
Description string Description string
@ -78,6 +85,7 @@ func (cmd *GroupCommand) ActionFunc(fn GroupActionFunc) *GroupCommand {
return cmd.WithAction(fn) return cmd.WithAction(fn)
} }
func (cmd *GroupCommand) Desc(desc string) *GroupCommand { func (cmd *GroupCommand) Desc(desc string) *GroupCommand {
cmd.Description = desc cmd.Description = desc
return cmd return cmd
@ -142,7 +150,7 @@ func (w *CommandWidget) WithUsageFunc(fn ActionFunc) *CommandWidget {
return w.WithUsage(fn) return w.WithUsage(fn)
} }
func (widget *Command) Filter( func (widget *CommandWidget) Filter(
u *Update, u *Update,
msgs ...*Message, msgs ...*Message,
) bool { ) bool {

View file

@ -154,8 +154,6 @@ func (c *Context) Go(pth Path, args ...any) error {
pth = (c.Path() + "/" + pth).Clean() pth = (c.Path() + "/" + pth).Clean()
} }
c.Sendf("path: %q\nmap: %v", pth, c.Bot.behaviour.Screens)
if !c.PathExist(pth) { if !c.PathExist(pth) {
return ScreenNotExistErr return ScreenNotExistErr
} }