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

View file

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

View file

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