From 6324599eed40d051977345e94c7048fd4df88bea Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 11 Oct 2023 14:20:25 +0300 Subject: [PATCH] feat: now the API provides ability to send location on the API itself level. --- cmd/test/main.go | 9 +++++++++ tg/location.go | 29 +++++++++++++++++++++++++++++ tg/message.go | 15 +++++++++++++++ tg/send.go | 14 ++++++++------ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 tg/location.go diff --git a/cmd/test/main.go b/cmd/test/main.go index 455d9df..485d78b 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -313,6 +313,15 @@ WithUsage(tg.Func(func(c *tg.Context){ WithAction(tg.Func(func(c *tg.Context){ c.Sendf("%q", c.History()) })), + tg.NewCommand("washington"). + Desc("Send location of the Washington"). + WithAction(tg.Func(func(c *tg.Context){ + c.Send( + tg.NewMessage("").Location( + 47.751076, -120.740135, + ), + ) + })), )) func main() { log.Println(beh.Screens) diff --git a/tg/location.go b/tg/location.go new file mode 100644 index 0000000..c01b5ea --- /dev/null +++ b/tg/location.go @@ -0,0 +1,29 @@ +package tg + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +type Location = tgbotapi.Location + +type LocationCompo struct { + *MessageCompo + Location +} + +func (compo *LocationCompo) SendConfig( + sid SessionId, bot *Bot, +) (*SendConfig) { + cid := sid.ToApi() + loc := tgbotapi.NewLocation( + cid, + compo.Latitude, + compo.Longitude, + ) + ret := &SendConfig{ + Location: &loc, + } + + return ret +} + diff --git a/tg/message.go b/tg/message.go index 3f706e7..5826151 100644 --- a/tg/message.go +++ b/tg/message.go @@ -77,6 +77,21 @@ func (msg *MessageCompo) Reply(reply *Reply) *ReplyCompo { } } +// Transform the message component into the location one. +func (msg *MessageCompo) Location( + lat, long float64, +) *LocationCompo { + ret := &LocationCompo{ + MessageCompo: msg, + Location: Location{ + Latitude: lat, + Longitude: long, + }, + } + return ret +} + +// Implementing the Sendable interface. func (config *MessageCompo) SendConfig( sid SessionId, bot *Bot, ) (*SendConfig) { diff --git a/tg/send.go b/tg/send.go index c379b2e..36451c5 100644 --- a/tg/send.go +++ b/tg/send.go @@ -29,6 +29,7 @@ type SendConfig struct { // The image to be sent. Image *tgbotapi.PhotoConfig + Location *tgbotapi.LocationConfig Error error } @@ -41,12 +42,13 @@ type MessageMap map[string] *Message // Convert to the bot.Api.Send format. func (config *SendConfig) ToApi() tgbotapi.Chattable { - if config.Message != nil { - return *config.Message - } - - if config.Image != nil { - return *config.Image + switch { + case config.Message != nil : + return *(config.Message) + case config.Image != nil : + return *(config.Image) + case config.Location != nil : + return *(config.Location) } return nil }