From 34faec2f8b0d823604d06ee8b3e1ff6c826d1f87 Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 18 Aug 2023 14:20:33 +0300 Subject: [PATCH] Fixed "act" declaration outside the handling loop so it is nil on every cycle of the loop. --- cmd/test/main.go | 9 ++++----- src/tx/bot.go | 2 +- src/tx/context.go | 7 +++++-- src/tx/group.go | 4 ++-- src/tx/private.go | 5 +++++ 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index 65a482c..b8ad2e3 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -23,7 +23,7 @@ var ( c.Sendf("%d", d.Counter) }), tx.NewButton("-").ActionFunc(func(c *tx.Context) { - d := c.V.(*UserData) + d := c.SessionValue().(*UserData) d.Counter-- c.Sendf("%d", d.Counter) }), @@ -48,9 +48,8 @@ var ( WithSendLocation(true). ActionFunc(func(c *tx.Context) { var err error - u := c.Update - if u.Message.Location != nil { - l := u.Message.Location + if c.Message.Location != nil { + l := c.Message.Location err = c.Sendf( "Longitude: %f\n"+ "Latitude: %f\n"+ @@ -181,7 +180,7 @@ var gBeh = tx.NewGroupBehaviour(). c.Send("Hello, World!") }), tx.NewGroupCommand("mycounter").ActionFunc(func(c *tx.GC) { - d := c.GetSessionValue().(*UserData) + d := c.SessionValue().(*UserData) c.Sendf("Your counter value is %d", d.Counter) }), ) diff --git a/src/tx/bot.go b/src/tx/bot.go index 6a5b568..4a5ba5e 100644 --- a/src/tx/bot.go +++ b/src/tx/bot.go @@ -36,7 +36,7 @@ func NewBot(token string) (*Bot, error) { }, nil } -func (bot *Bot) GetSessionValueBySid( +func (bot *Bot) SessionValueBySid( sid SessionId, ) (any, bool) { v, ok := bot.sessions[sid] diff --git a/src/tx/context.go b/src/tx/context.go index d3ef6b8..b6f84af 100644 --- a/src/tx/context.go +++ b/src/tx/context.go @@ -21,13 +21,13 @@ type context struct { // Goroutie function to handle each user. func (c *context) handleUpdateChan(updates chan *Update) { - var act Action beh := c.behaviour if beh.Init != nil { c.run(beh.Init, nil) } for u := range updates { + var act Action screen := c.curScreen // The part is added to implement custom update handling. if u.Message != nil { @@ -70,7 +70,10 @@ func (c *context) handleUpdateChan(updates chan *Update) { } } } else if u.CallbackQuery != nil { - cb := apix.NewCallback(u.CallbackQuery.ID, u.CallbackQuery.Data) + cb := apix.NewCallback( + u.CallbackQuery.ID, + u.CallbackQuery.Data, + ) data := u.CallbackQuery.Data _, err := c.Request(cb) diff --git a/src/tx/group.go b/src/tx/group.go index 6a2f15e..b46205a 100644 --- a/src/tx/group.go +++ b/src/tx/group.go @@ -18,8 +18,8 @@ func (c *GroupContext) SentFromSid() SessionId { return SessionId(c.SentFrom().ID) } -func (a *GroupContext) GetSessionValue() any { - v, _ := a.Bot.GetSessionValueBySid(a.SentFromSid()) +func (a *GroupContext) SessionValue() any { + v, _ := a.Bot.SessionValueBySid(a.SentFromSid()) return v } diff --git a/src/tx/private.go b/src/tx/private.go index 70b4a1c..2be40d3 100644 --- a/src/tx/private.go +++ b/src/tx/private.go @@ -57,3 +57,8 @@ func (c *Context) ChangeScreen(screenId ScreenId) error { return nil } + +func (c *Context) SessionValue() any { + v, _ := c.SessionValueBySid(c.Id) + return v +}