From c2b8e33b18d223370169c4984a3cbaca3c452e7c Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 21 Aug 2019 09:24:29 +0300 Subject: [PATCH 1/2] Feature: Voice messages support --- go.mod | 2 +- go.sum | 4 ++-- src/routing.go | 20 ++++++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 06783e2..38b5b20 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/retailcrm/api-client-go v1.1.1 - github.com/retailcrm/mg-transport-api-client-go v1.1.29 + github.com/retailcrm/mg-transport-api-client-go v1.1.31 github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf // indirect github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a // indirect github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect diff --git a/go.sum b/go.sum index 7c57471..d42591d 100644 --- a/go.sum +++ b/go.sum @@ -95,8 +95,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/retailcrm/api-client-go v1.1.1 h1:yqsyYjBDdmDwExVlTdGucY9/IpEokXpkfTfA6z5AZ7M= github.com/retailcrm/api-client-go v1.1.1/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= -github.com/retailcrm/mg-transport-api-client-go v1.1.29 h1:C2YMCbRsbyw8jz8Xh6oWK9DFgslcLZYDRjJdZpAMDoE= -github.com/retailcrm/mg-transport-api-client-go v1.1.29/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= +github.com/retailcrm/mg-transport-api-client-go v1.1.31 h1:21pE1JhT49rvbMLDYJa0iiqbb/roz+eSp27fPck4uUw= +github.com/retailcrm/mg-transport-api-client-go v1.1.31/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a h1:JSvGDIbmil4Ui/dDdFBExb7/cmkNjyX5F97oglmvCDo= diff --git a/src/routing.go b/src/routing.go index 2bca50a..dca2f59 100644 --- a/src/routing.go +++ b/src/routing.go @@ -509,8 +509,16 @@ func telegramWebhookHandler(c *gin.Context) { ) } + bot, err := tgbotapi.NewBotAPI(b.Token) + if err != nil { + logger.Error(b, err) + c.AbortWithStatus(http.StatusBadRequest) + return + } + var client = v1.New(conn.MGURL, conn.MGToken) client.Debug = config.Debug + bot.Debug = config.Debug if update.Message != nil { nickname := update.Message.From.UserName @@ -585,7 +593,7 @@ func telegramWebhookHandler(c *gin.Context) { if snd.Message.Text == "" { setLocale(update.Message.From.LanguageCode) - err := setAttachment(update.Message, client, &snd, b.Token) + err := setAttachment(bot, update.Message, client, &snd, b.Token) if err != nil { logger.Error(client.Token, err.Error()) c.AbortWithStatus(http.StatusBadRequest) @@ -1007,7 +1015,7 @@ func textMessage(cid int64, mb string, quoteExternalID string) (chattable tgbota return } -func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.SendData, botToken string) error { +func setAttachment(bot *tgbotapi.BotAPI, attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.SendData, botToken string) error { var ( items []v1.Item fileID string @@ -1036,6 +1044,9 @@ func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.S case "sticker": fileID = attachments.Sticker.FileID snd.Message.Type = v1.MsgTypeImage + case "voice": + fileID = attachments.Voice.FileID + snd.Message.Type = v1.MsgTypeAudio default: snd.Message.Text = getLocalizedMessage(t) } @@ -1048,7 +1059,8 @@ func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.S item := v1.Item{} fileUrl := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", botToken, file.FilePath) - if t != "sticker" { + switch { + case t == "sticker" || t == "voice": item, _, err = getItemData( client, fileUrl, @@ -1057,7 +1069,7 @@ func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.S if err != nil { return err } - } else { + default: item, err = convertAndUploadImage( client, fileUrl, From 834dd4f286e657f426302c51190735582ff1531e Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 26 Aug 2019 14:45:03 +0300 Subject: [PATCH 2/2] Remove reduntant api initialization --- src/routing.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/routing.go b/src/routing.go index dca2f59..f50a528 100644 --- a/src/routing.go +++ b/src/routing.go @@ -509,16 +509,8 @@ func telegramWebhookHandler(c *gin.Context) { ) } - bot, err := tgbotapi.NewBotAPI(b.Token) - if err != nil { - logger.Error(b, err) - c.AbortWithStatus(http.StatusBadRequest) - return - } - var client = v1.New(conn.MGURL, conn.MGToken) client.Debug = config.Debug - bot.Debug = config.Debug if update.Message != nil { nickname := update.Message.From.UserName @@ -593,7 +585,7 @@ func telegramWebhookHandler(c *gin.Context) { if snd.Message.Text == "" { setLocale(update.Message.From.LanguageCode) - err := setAttachment(bot, update.Message, client, &snd, b.Token) + err := setAttachment(update.Message, client, &snd, b.Token) if err != nil { logger.Error(client.Token, err.Error()) c.AbortWithStatus(http.StatusBadRequest) @@ -1015,7 +1007,7 @@ func textMessage(cid int64, mb string, quoteExternalID string) (chattable tgbota return } -func setAttachment(bot *tgbotapi.BotAPI, attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.SendData, botToken string) error { +func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.SendData, botToken string) error { var ( items []v1.Item fileID string