diff --git a/docker-compose-test.yml b/docker-compose-test.yml index cf789fc..5b7b322 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -11,7 +11,7 @@ services: - ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432} mg_telegram_test: - image: golang:1.11-stretch + image: golang:1.11.3-stretch working_dir: /mgtg user: ${UID:-1000}:${GID:-1000} environment: diff --git a/docker-compose.yml b/docker-compose.yml index a31ed0e..edc9a6c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,7 +11,7 @@ services: - ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432} mg_telegram: - image: golang:1.11-stretch + image: golang:1.11.3-stretch working_dir: /mgtg user: ${UID:-1000}:${GID:-1000} environment: diff --git a/go.mod b/go.mod index 3fe6a75..15cdffe 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.20 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 f1857ba..3f9ec7f 100644 --- a/go.sum +++ b/go.sum @@ -95,10 +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.17 h1:8L0meFJFZwr3TyiF/xypI+g0xRXIGkWhyQ6qiCfojqw= -github.com/retailcrm/mg-transport-api-client-go v1.1.17/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= -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.20 h1:1aDJ2QVtSkMtSUm50exmzQFSsE+/FQiWYdw2pamUyLc= +github.com/retailcrm/mg-transport-api-client-go v1.1.20/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= @@ -113,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 fea45a5..a1433a6 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) { @@ -75,6 +78,14 @@ func addBotHandler(c *gin.Context) { } b.Channel = data.ChannelID + b.Lang = "en" + + hashSettings, err := getChannelSettingsHash() + if err != nil { + logger.Errorf("addBotHandler hashSettings apiURl: %s, err: %s", conn.APIURL, err.Error()) + } else { + b.ChannelSettingsHash = hashSettings + } err = conn.createBot(b) if err != nil { @@ -253,6 +264,13 @@ func activityHandler(c *gin.Context) { conn.APIURL = systemUrl } + hashSettings, err := getChannelSettingsHash() + if err != nil { + logger.Errorf("activityHandler hashSettings apiURl: %s, err: %s", conn.APIURL, err.Error()) + } else { + updateBots(conn, hashSettings) + } + if err := conn.saveConnection(); err != nil { c.Error(err) return @@ -534,7 +552,13 @@ func telegramWebhookHandler(c *gin.Context) { data, st, err := client.Messages(snd) if err != nil { logger.Error(b.Token, err.Error(), st, data) - c.Error(err) + + if update.Message.ReplyToMessage != nil { + c.AbortWithStatus(http.StatusOK) + } else { + c.Error(err) + } + return } @@ -959,13 +983,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) @@ -999,3 +1035,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 +} diff --git a/src/routing_test.go b/src/routing_test.go index c7b1a41..79b5aa7 100644 --- a/src/routing_test.go +++ b/src/routing_test.go @@ -13,12 +13,12 @@ import ( "github.com/gin-gonic/gin" "github.com/h2non/gock" - "github.com/retailcrm/mg-transport-api-client-go/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) var router *gin.Engine +var ch = getChannelSettings() func init() { os.Chdir("../") @@ -56,48 +56,9 @@ func TestRouting_connectHandler(t *testing.T) { func TestRouting_addBotHandler(t *testing.T) { defer gock.Off() - ch := v1.Channel{ - Type: "telegram", - Name: "@TestBot", - Settings: v1.ChannelSettings{ - SpamAllowed: false, - Status: v1.Status{ - Delivered: v1.ChannelFeatureSend, - Read: v1.ChannelFeatureNone, - }, - Text: v1.ChannelSettingsText{ - Creating: v1.ChannelFeatureBoth, - Editing: v1.ChannelFeatureBoth, - Quoting: v1.ChannelFeatureBoth, - Deleting: v1.ChannelFeatureReceive, - MaxCharsCount: MaxCharsCount, - }, - Product: v1.Product{ - Creating: v1.ChannelFeatureReceive, - Editing: v1.ChannelFeatureReceive, - }, - Order: v1.Order{ - Creating: v1.ChannelFeatureReceive, - Editing: v1.ChannelFeatureReceive, - }, - File: v1.ChannelSettingsFilesBase{ - Creating: v1.ChannelFeatureBoth, - Editing: v1.ChannelFeatureBoth, - Quoting: v1.ChannelFeatureBoth, - Deleting: v1.ChannelFeatureReceive, - Max: 1, - }, - Image: v1.ChannelSettingsFilesBase{ - Creating: v1.ChannelFeatureBoth, - Editing: v1.ChannelFeatureBoth, - Quoting: v1.ChannelFeatureBoth, - Deleting: v1.ChannelFeatureReceive, - Max: 10, - }, - }, - } + ch.Name = "@TestBot" - outgoing, _ := json.Marshal(&ch) + outgoing, _ := json.Marshal(ch) p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}} gock.New("https://api.telegram.org").