1
0
mirror of synced 2024-11-25 13:16:02 +03:00

Merge pull request #64 from gwinn/release

Release
This commit is contained in:
Alex Lushpai 2018-12-21 11:46:36 +03:00 committed by GitHub
commit 6e96cdb6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 57 deletions

View File

@ -11,7 +11,7 @@ services:
- ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432} - ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432}
mg_telegram_test: mg_telegram_test:
image: golang:1.11-stretch image: golang:1.11.3-stretch
working_dir: /mgtg working_dir: /mgtg
user: ${UID:-1000}:${GID:-1000} user: ${UID:-1000}:${GID:-1000}
environment: environment:

View File

@ -11,7 +11,7 @@ services:
- ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432} - ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432}
mg_telegram: mg_telegram:
image: golang:1.11-stretch image: golang:1.11.3-stretch
working_dir: /mgtg working_dir: /mgtg
user: ${UID:-1000}:${GID:-1000} user: ${UID:-1000}:${GID:-1000}
environment: environment:

3
go.mod
View File

@ -42,7 +42,7 @@ require (
github.com/pkg/errors v0.8.0 github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/retailcrm/api-client-go v1.1.0 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.21
github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf // indirect github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf // indirect
github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a // indirect github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a // indirect
github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect
@ -50,6 +50,7 @@ require (
github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/ugorji/go v1.1.1 // indirect github.com/ugorji/go v1.1.1 // indirect
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // 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/net v0.0.0-20180911220305-26e67e76b6c3 // indirect
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect

10
go.sum
View File

@ -87,10 +87,10 @@ 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/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 h1:APPO6ccJAeMV7Jz7BhrtDKSOm2r1j5Ft6fuEXNP2ij4=
github.com/retailcrm/api-client-go v1.1.0/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= 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.20 h1:1aDJ2QVtSkMtSUm50exmzQFSsE+/FQiWYdw2pamUyLc=
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.20/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.21 h1:OKkey9uvB16ucICuO2hp/tfh0wfp3na0CxRiT2Iv3sE=
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.21/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 h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co=
github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= 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= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a h1:JSvGDIbmil4Ui/dDdFBExb7/cmkNjyX5F97oglmvCDo=
@ -105,6 +105,8 @@ github.com/ugorji/go v1.1.1 h1:gmervu+jDMvXTbcHQ0pd2wee85nEoE0BsVyEuzkfK8w=
github.com/ugorji/go v1.1.1/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ= 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 h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ=
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 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 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 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= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=

View File

@ -3,6 +3,8 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"image/png"
"io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -12,6 +14,7 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/retailcrm/api-client-go/v5" "github.com/retailcrm/api-client-go/v5"
"github.com/retailcrm/mg-transport-api-client-go/v1" "github.com/retailcrm/mg-transport-api-client-go/v1"
"golang.org/x/image/webp"
) )
func connectHandler(c *gin.Context) { func connectHandler(c *gin.Context) {
@ -75,6 +78,14 @@ func addBotHandler(c *gin.Context) {
} }
b.Channel = data.ChannelID 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) err = conn.createBot(b)
if err != nil { if err != nil {
@ -253,6 +264,13 @@ func activityHandler(c *gin.Context) {
conn.APIURL = systemUrl 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 { if err := conn.saveConnection(); err != nil {
c.Error(err) c.Error(err)
return return
@ -534,7 +552,13 @@ func telegramWebhookHandler(c *gin.Context) {
data, st, err := client.Messages(snd) data, st, err := client.Messages(snd)
if err != nil { if err != nil {
logger.Error(b.Token, err.Error(), st, data) logger.Error(b.Token, err.Error(), st, data)
if update.Message.ReplyToMessage != nil {
c.AbortWithStatus(http.StatusOK)
} else {
c.Error(err) c.Error(err)
}
return return
} }
@ -959,14 +983,26 @@ func setAttachment(attachments *tgbotapi.Message, client *v1.MgClient, snd *v1.S
return err return err
} }
item, _, err := getItemData( item := v1.Item{}
fileUrl := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", botToken, file.FilePath)
if t != "sticker" {
item, _, err = getItemData(
client, client,
fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", botToken, file.FilePath), fileUrl,
caption, caption,
) )
if err != nil { if err != nil {
return err return err
} }
} else {
item, err = convertAndUploadImage(
client,
fileUrl,
)
if err != nil {
return err
}
}
items = append(items, item) 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) { func getFileURL(fileID string, b *tgbotapi.BotAPI) (tgbotapi.File, error) {
return b.GetFile(tgbotapi.FileConfig{FileID: fileID}) 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
}

View File

@ -13,12 +13,12 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/h2non/gock" "github.com/h2non/gock"
"github.com/retailcrm/mg-transport-api-client-go/v1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var router *gin.Engine var router *gin.Engine
var ch = getChannelSettings()
func init() { func init() {
os.Chdir("../") os.Chdir("../")
@ -56,48 +56,9 @@ func TestRouting_connectHandler(t *testing.T) {
func TestRouting_addBotHandler(t *testing.T) { func TestRouting_addBotHandler(t *testing.T) {
defer gock.Off() defer gock.Off()
ch := v1.Channel{ ch.Name = "@TestBot"
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,
},
},
}
outgoing, _ := json.Marshal(&ch) outgoing, _ := json.Marshal(ch)
p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}} p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}}
gock.New("https://api.telegram.org"). gock.New("https://api.telegram.org").