diff --git a/cmd/test/main.go b/cmd/test/main.go index 4d07e3a..7b82e9e 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -27,6 +27,9 @@ func NewMutateMessageWidget(fn func(string) string) *MutateMessageWidget { } func (w *MutateMessageWidget) Serve(c *tg.Context, updates chan *tg.Update) error { + for _, arg := range c.Args { + c.Sendf("%v", arg) + } for u := range updates { if u.Message == nil { continue @@ -65,7 +68,9 @@ var ( Row( tg.NewButton("Inc/Dec").ScreenChange("start/inc-dec"), ).Row( - tg.NewButton("Upper case").ScreenChange("start/upper-case"), + tg.NewButton("Upper case").ActionFunc(func(c *tg.Context){ + c.ChangeScreen("start/upper-case", "this shit", "works") + }), tg.NewButton("Lower case").ScreenChange("start/lower-case"), ).Row( tg.NewButton("Send location").ScreenChange("start/send-location"), @@ -109,7 +114,6 @@ var beh = tg.NewBehaviour(). WithInitFunc(func(c *tg.Context) { // The session initialization. c.Session.Data = &SessionData{} - }).WithScreens( tg.NewScreen("start", tg.NewPage( "The bot started!", @@ -175,6 +179,7 @@ var beh = tg.NewBehaviour(). tg.NewCommand("start"). Desc("start or restart the bot or move to the start screen"). ActionFunc(func(c *tg.Context){ + c.Sendf("Your username is %q", c.Message.From.UserName) c.ChangeScreen("start") }), tg.NewCommand("hello"). diff --git a/tg/private.go b/tg/private.go index 1fbf77f..251cf09 100644 --- a/tg/private.go +++ b/tg/private.go @@ -86,8 +86,9 @@ type Context struct { // The update that called the Context usage. *Update // Used as way to provide outer values redirection - // into widgets and actions - Arg any + // into widgets and actions. It is like arguments + // for REST API request etc. + Args []any } // Customized actions for the bot. @@ -117,18 +118,11 @@ func (sc ScreenChange) Act(c *Context) { type C = Context // Changes screen of user to the Id one. -func (c *Context) ChangeScreen(screenId ScreenId) error { +func (c *Context) ChangeScreen(screenId ScreenId, args ...any) error { if !c.Bot.behaviour.ScreenExist(screenId) { return ScreenNotExistErr } - // Stop the reading by sending the nil, - // since we change the screen and - // current goroutine needs to be stopped. - // if c.readingUpdate { - // c.Updates <- nil - // } - // Getting the screen and changing to // then executing its widget. screen := c.Bot.behaviour.Screens[screenId] @@ -143,7 +137,11 @@ func (c *Context) ChangeScreen(screenId ScreenId) error { if screen.Widget != nil { // Running the widget if the screen has one. go func() { - screen.Widget.Serve(c, c.skippedUpdates) + screen.Widget.Serve(&Context{ + context: c.context, + Update: c.Update, + Args: args, + }, c.skippedUpdates) }() } else { panic("no widget defined for the screen")