Rename Value -> Data for the sake of generalisity.

This commit is contained in:
Andrey Parhomenko 2023-09-11 10:23:04 +03:00
parent b2ce5fe2ea
commit 8326ccae1e
3 changed files with 17 additions and 9 deletions

View file

@ -12,7 +12,7 @@ type BotData struct {
Name string Name string
} }
type UserData struct { type SessionData struct {
Counter int 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 ( var (
startScreenButton = tg.NewButton("🏠 To the start screen"). startScreenButton = tg.NewButton("🏠 To the start screen").
ScreenChange("start") ScreenChange("start")
incDecKeyboard = tg.NewReply().Row( incDecKeyboard = tg.NewReply().Row(
tg.NewButton("+").ActionFunc(func(c *tg.Context) { tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d := c.Session.Value.(*UserData) d := ExtractSessionData(c)
d.Counter++ d.Counter++
c.Sendf("%d", d.Counter) c.Sendf("%d", d.Counter)
}), }),
tg.NewButton("-").ActionFunc(func(c *tg.Context) { tg.NewButton("-").ActionFunc(func(c *tg.Context) {
d := c.Session.Value.(*UserData) d := ExtractSessionData(c)
d.Counter-- d.Counter--
c.Sendf("%d", d.Counter) c.Sendf("%d", d.Counter)
}), }),
@ -103,7 +107,7 @@ var (
var beh = tg.NewBehaviour(). var beh = tg.NewBehaviour().
WithInitFunc(func(c *tg.Context) { WithInitFunc(func(c *tg.Context) {
// The session initialization. // The session initialization.
c.Session.Value = &UserData{} c.Session.Data = &SessionData{}
}). // On any message update before the bot created session. }). // On any message update before the bot created session.
WithPreStartFunc(func(c *tg.Context){ WithPreStartFunc(func(c *tg.Context){
@ -129,7 +133,7 @@ var beh = tg.NewBehaviour().
incDecKeyboard, incDecKeyboard,
).ActionFunc(func(c *tg.Context) { ).ActionFunc(func(c *tg.Context) {
// The function will be calleb before serving page. // The function will be calleb before serving page.
d := c.Session.Value.(*UserData) d := ExtractSessionData(c)
c.Sendf("Current counter value = %d", d.Counter) c.Sendf("Current counter value = %d", d.Counter)
}), }),
), ),
@ -163,7 +167,7 @@ var beh = tg.NewBehaviour().
).WithData( ).WithData(
"check", "check",
).ActionFunc(func(c *tg.Context) { ).ActionFunc(func(c *tg.Context) {
d := c.Session.Value.(*UserData) d := ExtractSessionData(c)
c.Sendf("Counter = %d", d.Counter) c.Sendf("Counter = %d", d.Counter)
}), }),
), ),
@ -212,7 +216,7 @@ var gBeh = tg.NewGroupBehaviour().
c.Send("Hello, World!") c.Send("Hello, World!")
}), }),
tg.NewGroupCommand("mycounter").ActionFunc(func(c *tg.GC) { 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) c.Sendf("Your counter value is %d", d.Counter)
}), }),
) )

View file

@ -100,7 +100,11 @@ func (c *context) Sendf(format string, v ...any) (*Message, error) {
// Interface to interact with the user. // Interface to interact with the user.
type Context struct { type Context struct {
*context *context
// The update that called the Context usage.
*Update *Update
// Used as way to provide outer values redirection
// into widgets and actions
Arg any
} }
// Customized actions for the bot. // Customized actions for the bot.

View file

@ -18,7 +18,7 @@ type Session struct {
// (got the '/start' command. // (got the '/start' command.
started bool started bool
// Custom value for each user. // Custom value for each user.
Value any Data any
} }
// Return new empty session with specified user ID. // Return new empty session with specified user ID.
@ -41,7 +41,7 @@ func (sm SessionMap) Add(sid SessionId) {
type GroupSession struct { type GroupSession struct {
Id SessionId Id SessionId
// Information for each user in the group. // Information for each user in the group.
Value any Data any
} }
// Returns new empty group session with specified group and user IDs. // Returns new empty group session with specified group and user IDs.