Fixed "act" declaration outside the handling loop so it is nil on every cycle of the loop.

This commit is contained in:
Andrey Parhomenko 2023-08-18 14:20:33 +03:00
parent 3f26d2f916
commit 34faec2f8b
5 changed files with 17 additions and 10 deletions

View file

@ -23,7 +23,7 @@ var (
c.Sendf("%d", d.Counter) c.Sendf("%d", d.Counter)
}), }),
tx.NewButton("-").ActionFunc(func(c *tx.Context) { tx.NewButton("-").ActionFunc(func(c *tx.Context) {
d := c.V.(*UserData) d := c.SessionValue().(*UserData)
d.Counter-- d.Counter--
c.Sendf("%d", d.Counter) c.Sendf("%d", d.Counter)
}), }),
@ -48,9 +48,8 @@ var (
WithSendLocation(true). WithSendLocation(true).
ActionFunc(func(c *tx.Context) { ActionFunc(func(c *tx.Context) {
var err error var err error
u := c.Update if c.Message.Location != nil {
if u.Message.Location != nil { l := c.Message.Location
l := u.Message.Location
err = c.Sendf( err = c.Sendf(
"Longitude: %f\n"+ "Longitude: %f\n"+
"Latitude: %f\n"+ "Latitude: %f\n"+
@ -181,7 +180,7 @@ var gBeh = tx.NewGroupBehaviour().
c.Send("Hello, World!") c.Send("Hello, World!")
}), }),
tx.NewGroupCommand("mycounter").ActionFunc(func(c *tx.GC) { 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) c.Sendf("Your counter value is %d", d.Counter)
}), }),
) )

View file

@ -36,7 +36,7 @@ func NewBot(token string) (*Bot, error) {
}, nil }, nil
} }
func (bot *Bot) GetSessionValueBySid( func (bot *Bot) SessionValueBySid(
sid SessionId, sid SessionId,
) (any, bool) { ) (any, bool) {
v, ok := bot.sessions[sid] v, ok := bot.sessions[sid]

View file

@ -21,13 +21,13 @@ type context struct {
// Goroutie function to handle each user. // Goroutie function to handle each user.
func (c *context) handleUpdateChan(updates chan *Update) { func (c *context) handleUpdateChan(updates chan *Update) {
var act Action
beh := c.behaviour beh := c.behaviour
if beh.Init != nil { if beh.Init != nil {
c.run(beh.Init, nil) c.run(beh.Init, nil)
} }
for u := range updates { for u := range updates {
var act Action
screen := c.curScreen screen := c.curScreen
// The part is added to implement custom update handling. // The part is added to implement custom update handling.
if u.Message != nil { if u.Message != nil {
@ -70,7 +70,10 @@ func (c *context) handleUpdateChan(updates chan *Update) {
} }
} }
} else if u.CallbackQuery != nil { } 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 data := u.CallbackQuery.Data
_, err := c.Request(cb) _, err := c.Request(cb)

View file

@ -18,8 +18,8 @@ func (c *GroupContext) SentFromSid() SessionId {
return SessionId(c.SentFrom().ID) return SessionId(c.SentFrom().ID)
} }
func (a *GroupContext) GetSessionValue() any { func (a *GroupContext) SessionValue() any {
v, _ := a.Bot.GetSessionValueBySid(a.SentFromSid()) v, _ := a.Bot.SessionValueBySid(a.SentFromSid())
return v return v
} }

View file

@ -57,3 +57,8 @@ func (c *Context) ChangeScreen(screenId ScreenId) error {
return nil return nil
} }
func (c *Context) SessionValue() any {
v, _ := c.SessionValueBySid(c.Id)
return v
}