From 8e094d408a18dd73999fe8240bf37d7498fc3ca0 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 13 Dec 2018 17:31:49 +0300 Subject: [PATCH] add convert sticker --- go.mod | 3 ++- go.sum | 6 +++-- src/routing.go | 62 ++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 3fe6a75..d9ad56c 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.0 - github.com/retailcrm/mg-transport-api-client-go v1.1.18 + github.com/retailcrm/mg-transport-api-client-go v1.1.19 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 @@ -53,6 +53,7 @@ require ( github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/ugorji/go v1.1.1 // indirect golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // indirect + golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 // indirect golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect diff --git a/go.sum b/go.sum index 8bed425..3b54250 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.0 h1:APPO6ccJAeMV7Jz7BhrtDKSOm2r1j5Ft6fuEXNP2ij4= github.com/retailcrm/api-client-go v1.1.0/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= -github.com/retailcrm/mg-transport-api-client-go v1.1.18 h1:V59Ai79N1jrS1kAnQggNoRYsoAdr2RUV7Hf0xbw9r9Q= -github.com/retailcrm/mg-transport-api-client-go v1.1.18/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= +github.com/retailcrm/mg-transport-api-client-go v1.1.19 h1:bq4kyqcdWjeqQmgTgv99A9YvItpz4N6rKkdbZ66bNdU= +github.com/retailcrm/mg-transport-api-client-go v1.1.19/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= @@ -111,6 +111,8 @@ github.com/ugorji/go v1.1.1 h1:gmervu+jDMvXTbcHQ0pd2wee85nEoE0BsVyEuzkfK8w= github.com/ugorji/go v1.1.1/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ= golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ= golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b h1:VHyIDlv3XkfCa5/a81uzaoDkHH4rr81Z62g+xlnO8uM= +golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI= golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= diff --git a/src/routing.go b/src/routing.go index 723179c..cb06dca 100644 --- a/src/routing.go +++ b/src/routing.go @@ -3,6 +3,8 @@ package main import ( "encoding/json" "fmt" + "image/png" + "io" "net/http" "strconv" "strings" @@ -12,6 +14,7 @@ import ( "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/retailcrm/api-client-go/v5" "github.com/retailcrm/mg-transport-api-client-go/v1" + "golang.org/x/image/webp" ) func connectHandler(c *gin.Context) { @@ -974,13 +977,25 @@ func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.S return err } - item, _, err := getItemData( - client, - fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", botToken, file.FilePath), - caption, - ) - if err != nil { - return err + item := v1.Item{} + fileUrl := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", botToken, file.FilePath) + if t != "sticker" { + item, _, err = getItemData( + client, + fileUrl, + caption, + ) + if err != nil { + return err + } + } else { + item, err = convertAndUploadImage( + client, + fileUrl, + ) + if err != nil { + return err + } } items = append(items, item) @@ -1014,3 +1029,36 @@ func getItemData(client *v1.MgClient, url string, caption string) (v1.Item, int, func getFileURL(fileID string, b *tgbotapi.BotAPI) (tgbotapi.File, error) { return b.GetFile(tgbotapi.FileConfig{FileID: fileID}) } + +func convertAndUploadImage(client *v1.MgClient, url string) (v1.Item, error) { + item := v1.Item{} + + res, err := http.Get(url) + if err != nil { + return item, err + } + + img, err := webp.Decode(res.Body) + if err != nil { + return item, err + } + + pReader, pWriter := io.Pipe() + + go func() { + defer pWriter.Close() + err = png.Encode(pWriter, img) + if err != nil { + logger.Info(item, err.Error()) + } + }() + + data, _, err := client.UploadFile(pReader) + if err != nil { + return item, err + } + + item.ID = data.ID + + return item, nil +}