Fixed issues with Update filtering.

This commit is contained in:
Andrey Parhomenko 2023-07-12 14:20:52 +03:00
parent 36f529df58
commit 2935a7db99
3 changed files with 19 additions and 12 deletions

View file

@ -43,10 +43,11 @@ func (bot *Bot) Run() error {
chans := make(map[SessionId] chan *Update)
for u := range updates {
var sid SessionId
if u.Message != nil {
// Create new session if the one does not exist
// for this user.
sid := SessionId(u.Message.Chat.ID)
sid = SessionId(u.Message.Chat.ID)
if _, ok := bot.sessions[sid] ; !ok {
bot.sessions.Add(sid)
}
@ -71,10 +72,13 @@ func (bot *Bot) Run() error {
continue
}
}
chn := chans[sid]
} else if u.CallbackQuery != nil {
sid = SessionId(u.CallbackQuery.Message.Chat.ID)
}
chn, ok := chans[sid]
if ok {
chn <- &u
}
}
}
return nil

View file

@ -1,5 +1,10 @@
package behx
import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"fmt"
)
// The type represents way to interact with user in
// handling functions. Is provided to Act() function always.
type Context struct {
@ -13,12 +18,12 @@ func (ctx *Context) handleUpdateChan(updates chan *Update) {
session := ctx.Session
bot.Start.Act(ctx)
for u := range updates {
screen := bot.Screens[session.CurrentScreenId]
kbd := bot.Keyboards[screen.KeyboardId]
btns := kbd.buttonMap()
if u.Message != nil {
screen := bot.Screens[session.CurrentScreenId]
kbd := bot.Keyboards[screen.KeyboardId]
btns := kbd.buttonMap()
text := u.Message.Text
btn, ok := btns[text]

View file

@ -29,9 +29,7 @@ var secondKbd = behx.NewKeyboard(
behx.NewButtonRow(
behx.NewButton(
"❤",
behx.NewCustomAction(func(c *behx.Context){
log.Println("pressed the button!")
}),
behx.NewScreenChange("start"),
),
),
)