Added arguments to screen change making it easier to define widget behaviour both for screens and commands.

This commit is contained in:
Andrey Parhomenko 2023-09-13 12:52:23 +03:00
parent d500d84de6
commit 5e1faf0c44
2 changed files with 16 additions and 13 deletions

View file

@ -27,6 +27,9 @@ func NewMutateMessageWidget(fn func(string) string) *MutateMessageWidget {
} }
func (w *MutateMessageWidget) Serve(c *tg.Context, updates chan *tg.Update) error { 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 { for u := range updates {
if u.Message == nil { if u.Message == nil {
continue continue
@ -65,7 +68,9 @@ var (
Row( Row(
tg.NewButton("Inc/Dec").ScreenChange("start/inc-dec"), tg.NewButton("Inc/Dec").ScreenChange("start/inc-dec"),
).Row( ).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"), tg.NewButton("Lower case").ScreenChange("start/lower-case"),
).Row( ).Row(
tg.NewButton("Send location").ScreenChange("start/send-location"), tg.NewButton("Send location").ScreenChange("start/send-location"),
@ -109,7 +114,6 @@ var beh = tg.NewBehaviour().
WithInitFunc(func(c *tg.Context) { WithInitFunc(func(c *tg.Context) {
// The session initialization. // The session initialization.
c.Session.Data = &SessionData{} c.Session.Data = &SessionData{}
}).WithScreens( }).WithScreens(
tg.NewScreen("start", tg.NewPage( tg.NewScreen("start", tg.NewPage(
"The bot started!", "The bot started!",
@ -175,6 +179,7 @@ var beh = tg.NewBehaviour().
tg.NewCommand("start"). tg.NewCommand("start").
Desc("start or restart the bot or move to the start screen"). Desc("start or restart the bot or move to the start screen").
ActionFunc(func(c *tg.Context){ ActionFunc(func(c *tg.Context){
c.Sendf("Your username is %q", c.Message.From.UserName)
c.ChangeScreen("start") c.ChangeScreen("start")
}), }),
tg.NewCommand("hello"). tg.NewCommand("hello").

View file

@ -86,8 +86,9 @@ type Context struct {
// The update that called the Context usage. // The update that called the Context usage.
*Update *Update
// Used as way to provide outer values redirection // Used as way to provide outer values redirection
// into widgets and actions // into widgets and actions. It is like arguments
Arg any // for REST API request etc.
Args []any
} }
// Customized actions for the bot. // Customized actions for the bot.
@ -117,18 +118,11 @@ func (sc ScreenChange) Act(c *Context) {
type C = Context type C = Context
// Changes screen of user to the Id one. // 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) { if !c.Bot.behaviour.ScreenExist(screenId) {
return ScreenNotExistErr 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 // Getting the screen and changing to
// then executing its widget. // then executing its widget.
screen := c.Bot.behaviour.Screens[screenId] screen := c.Bot.behaviour.Screens[screenId]
@ -143,7 +137,11 @@ func (c *Context) ChangeScreen(screenId ScreenId) error {
if screen.Widget != nil { if screen.Widget != nil {
// Running the widget if the screen has one. // Running the widget if the screen has one.
go func() { go func() {
screen.Widget.Serve(c, c.skippedUpdates) screen.Widget.Serve(&Context{
context: c.context,
Update: c.Update,
Args: args,
}, c.skippedUpdates)
}() }()
} else { } else {
panic("no widget defined for the screen") panic("no widget defined for the screen")