From 8326ccae1e153742e974505c10f4998a870024b6 Mon Sep 17 00:00:00 2001 From: surdeus Date: Mon, 11 Sep 2023 10:23:04 +0300 Subject: [PATCH] Rename Value -> Data for the sake of generalisity. --- cmd/test/main.go | 18 +++++++++++------- tg/private.go | 4 ++++ tg/session.go | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index 0071a21..841caeb 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -12,7 +12,7 @@ type BotData struct { Name string } -type UserData struct { +type SessionData struct { Counter int } @@ -36,18 +36,22 @@ func (w *MutateMessageWidget) Serve(c *tg.Context, updates chan *tg.Update) { } } +func ExtractSessionData(c *tg.Context) *SessionData { + return c.Session.Data.(*SessionData) +} + var ( startScreenButton = tg.NewButton("🏠 To the start screen"). ScreenChange("start") incDecKeyboard = tg.NewReply().Row( tg.NewButton("+").ActionFunc(func(c *tg.Context) { - d := c.Session.Value.(*UserData) + d := ExtractSessionData(c) d.Counter++ c.Sendf("%d", d.Counter) }), tg.NewButton("-").ActionFunc(func(c *tg.Context) { - d := c.Session.Value.(*UserData) + d := ExtractSessionData(c) d.Counter-- c.Sendf("%d", d.Counter) }), @@ -103,7 +107,7 @@ var ( var beh = tg.NewBehaviour(). WithInitFunc(func(c *tg.Context) { // The session initialization. - c.Session.Value = &UserData{} + c.Session.Data = &SessionData{} }). // On any message update before the bot created session. WithPreStartFunc(func(c *tg.Context){ @@ -129,7 +133,7 @@ var beh = tg.NewBehaviour(). incDecKeyboard, ).ActionFunc(func(c *tg.Context) { // The function will be calleb before serving page. - d := c.Session.Value.(*UserData) + d := ExtractSessionData(c) c.Sendf("Current counter value = %d", d.Counter) }), ), @@ -163,7 +167,7 @@ var beh = tg.NewBehaviour(). ).WithData( "check", ).ActionFunc(func(c *tg.Context) { - d := c.Session.Value.(*UserData) + d := ExtractSessionData(c) c.Sendf("Counter = %d", d.Counter) }), ), @@ -212,7 +216,7 @@ var gBeh = tg.NewGroupBehaviour(). c.Send("Hello, World!") }), tg.NewGroupCommand("mycounter").ActionFunc(func(c *tg.GC) { - d := c.Session().Value.(*UserData) + d := c.Session().Data.(*SessionData) c.Sendf("Your counter value is %d", d.Counter) }), ) diff --git a/tg/private.go b/tg/private.go index 4d4d1ec..4f72d32 100644 --- a/tg/private.go +++ b/tg/private.go @@ -100,7 +100,11 @@ func (c *context) Sendf(format string, v ...any) (*Message, error) { // Interface to interact with the user. type Context struct { *context + // The update that called the Context usage. *Update + // Used as way to provide outer values redirection + // into widgets and actions + Arg any } // Customized actions for the bot. diff --git a/tg/session.go b/tg/session.go index 5c9f3bd..917e660 100644 --- a/tg/session.go +++ b/tg/session.go @@ -18,7 +18,7 @@ type Session struct { // (got the '/start' command. started bool // Custom value for each user. - Value any + Data any } // Return new empty session with specified user ID. @@ -41,7 +41,7 @@ func (sm SessionMap) Add(sid SessionId) { type GroupSession struct { Id SessionId // Information for each user in the group. - Value any + Data any } // Returns new empty group session with specified group and user IDs.