From d99ea68198a8d54e80d0f3c3b9d0a7528d76da67 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 19 Sep 2023 15:21:33 +0300 Subject: [PATCH] Fixed location button problems. --- .air.toml | 6 +++--- cmd/test/main.go | 4 ++-- readme.md | 2 +- tg/button.go | 10 ++++++++++ tg/widget.go | 20 +++++++++++++------- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.air.toml b/.air.toml index f20e344..446ac56 100644 --- a/.air.toml +++ b/.air.toml @@ -4,7 +4,7 @@ tmp_dir = "tmp" [build] args_bin = [] - bin = "exe/test.exe" + bin = "exe/test" cmd = "go build -o ./exe/ ./cmd/test" delay = 0 @@ -26,8 +26,8 @@ tmp_dir = "tmp" poll_interval = 0 rerun = false rerun_delay = 500 - send_interrupt = true - stop_on_error = true + send_interrupt = false + stop_on_error = false [color] app = "red" diff --git a/cmd/test/main.go b/cmd/test/main.go index bb3283e..4a91929 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -151,9 +151,9 @@ var beh = tg.NewBehaviour(). ), tg.NewScreen("start/send-location", tg.NewPage( - "Send your location and I will tell where you are!", + "", ).WithReply( - sendLocationKeyboard.Widget(""), + sendLocationKeyboard.Widget("Press the button to send your location!"), ).WithInline( tg.NewKeyboard().Row( tg.NewButton( diff --git a/readme.md b/readme.md index df63c16..f4965ef 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# got v0.1.0 +# got v0.3.2 Go Telegram. diff --git a/tg/button.go b/tg/button.go index d6813a8..77c1232 100644 --- a/tg/button.go +++ b/tg/button.go @@ -15,6 +15,16 @@ type Button struct { type ButtonMap map[string]*Button +// Returns the only location button in the map. +func (btnMap ButtonMap) LocationButton() *Button { + for _, btn := range btnMap { + if btn.SendLocation { + return btn + } + } + return nil +} + // Represents the reply button row. type ButtonRow []*Button diff --git a/tg/widget.go b/tg/widget.go index 8dc2c77..e9afe48 100644 --- a/tg/widget.go +++ b/tg/widget.go @@ -243,12 +243,22 @@ func (widget *ReplyKeyboardWidget) Filter( if widget == nil { return true } + if u.Message == nil { return true } + + _, ok := widget.ButtonMap()[u.Message.Text] if !ok { - return true + if u.Message.Location != nil { + locBtn := widget.ButtonMap().LocationButton() + if locBtn == nil { + return true + } + } else { + return true + } } return false } @@ -261,15 +271,11 @@ func (widget *ReplyKeyboardWidget) Serve( var btn *Button text := u.Message.Text btns := widget.ButtonMap() + btn, ok := btns[text] if !ok { if u.Message.Location != nil { - for _, b := range btns { - if b.SendLocation { - btn = b - ok = true - } - } + btn = btns.LocationButton() } }