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)
}),
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)
}),
)

View file

@ -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]

View file

@ -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)

View file

@ -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
}

View file

@ -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
}