1
0
mirror of synced 2024-11-29 06:56:02 +03:00

Merge pull request #36 from gwinn/master

Order & Product
This commit is contained in:
Alex Lushpai 2018-09-13 23:21:46 +03:00 committed by GitHub
commit eafccfadaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 89 deletions

5
go.mod
View File

@ -3,7 +3,7 @@ module github.com/retailcrm/mg-transport-telegram
require ( require (
cloud.google.com/go v0.27.0 // indirect cloud.google.com/go v0.27.0 // indirect
github.com/Microsoft/go-winio v0.4.11 // indirect github.com/Microsoft/go-winio v0.4.11 // indirect
github.com/aws/aws-sdk-go v1.15.33 github.com/aws/aws-sdk-go v1.15.35
github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2 // indirect github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20180901172138-1eb28afdf9b6 // indirect github.com/denisenkom/go-mssqldb v0.0.0-20180901172138-1eb28afdf9b6 // indirect
@ -31,6 +31,7 @@ require (
github.com/jinzhu/now v0.0.0-20180511015916-ed742868f2ae // indirect github.com/jinzhu/now v0.0.0-20180511015916-ed742868f2ae // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/json-iterator/go v1.1.5 // indirect github.com/json-iterator/go v1.1.5 // indirect
github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0 // indirect github.com/lib/pq v1.0.0 // indirect
@ -44,7 +45,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.0.6 github.com/retailcrm/api-client-go v1.0.6
github.com/retailcrm/mg-transport-api-client-go v1.1.8 github.com/retailcrm/mg-transport-api-client-go v1.1.9
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

14
go.sum
View File

@ -4,8 +4,8 @@ github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY
github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q= github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/aws/aws-sdk-go v1.15.33 h1:Xch3KIkPonPmCiA/BlaRm+RljrWr3M5Jb4EeUpdgmTU= github.com/aws/aws-sdk-go v1.15.35 h1:roOTR0eOeCgYwrz3NXmrneLFBYdnK8epLedaRMm1kQg=
github.com/aws/aws-sdk-go v1.15.33/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0= github.com/aws/aws-sdk-go v1.15.35/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2 h1:MmeatFT1pTPSVb4nkPmBFN/LRZ97vPjsFKsZrU3KKTs= github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2 h1:MmeatFT1pTPSVb4nkPmBFN/LRZ97vPjsFKsZrU3KKTs=
github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@ -64,6 +64,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a h1:2qq552JOlVHGYvqPc9ynBnGPDHeA7p0/QRn2NkrO8vk=
github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE= github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@ -93,12 +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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/retailcrm/api-client-go v1.0.6 h1:4Q3e4ve8GOOHIQdq3/wTGqgWuWa1cKMKqmgrTv4FoDU= github.com/retailcrm/api-client-go v1.0.6 h1:4Q3e4ve8GOOHIQdq3/wTGqgWuWa1cKMKqmgrTv4FoDU=
github.com/retailcrm/api-client-go v1.0.6/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= github.com/retailcrm/api-client-go v1.0.6/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c=
github.com/retailcrm/mg-transport-api-client-go v1.1.6 h1:bwBs+iJbJzTAUFbkz+LmOhpruYebrI26haPPzBlZaRU= github.com/retailcrm/mg-transport-api-client-go v1.1.9 h1:ogh5ThoqZJM5v4ZY6CqctUj01pVVHfBLXkrmX+BFjHE=
github.com/retailcrm/mg-transport-api-client-go v1.1.6/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/retailcrm/mg-transport-api-client-go v1.1.9/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI=
github.com/retailcrm/mg-transport-api-client-go v1.1.7 h1:dKm2hhR6l1kQvYKjD50C4/W9EzfV6t6YVdhLxSExooU=
github.com/retailcrm/mg-transport-api-client-go v1.1.7/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI=
github.com/retailcrm/mg-transport-api-client-go v1.1.8 h1:xLfy9j0VrAoAQUUtB5LllIqPFDvJ9H1xuri34kfZPPY=
github.com/retailcrm/mg-transport-api-client-go v1.1.8/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=

View File

@ -15,12 +15,12 @@ type Options struct {
} }
var ( var (
config *TransportConfig config *TransportConfig
orm *Orm orm *Orm
logger *logging.Logger logger *logging.Logger
options Options options Options
parser = flags.NewParser(&options, flags.Default) parser = flags.NewParser(&options, flags.Default)
rx = regexp.MustCompile(`/+$`) rx = regexp.MustCompile(`/+$`)
currency = map[string]string{ currency = map[string]string{
"rub": "₽", "rub": "₽",
"uah": "₴", "uah": "₴",

View File

@ -77,6 +77,10 @@ func addBotHandler(c *gin.Context) {
Creating: v1.ChannelFeatureReceive, Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive, Editing: v1.ChannelFeatureReceive,
}, },
Order: v1.Order{
Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive,
},
}, },
} }
@ -506,16 +510,35 @@ func mgWebhookHandler(c *gin.Context) {
) )
} }
if msg.Data.Product.Img != "" { if msg.Data.Product.Img != "" {
mb = fmt.Sprintf("\n%s", msg.Data.Product.Img) mb = fmt.Sprintf("\n%s", msg.Data.Product.Img)
} }
} else if msg.Data.Type == v1.MsgTypeOrder {
mb = "**Заказ"
if msg.Data.Order.Number != ""{
mb += " " + msg.Data.Order.Number
}
if msg.Data.Order.Date != "" {
mb += fmt.Sprintf(" (%s)", msg.Data.Order.Date)
}
mb += "**\n"
if len(msg.Data.Order.Items) > 0 {
for _, v := range msg.Data.Order.Items {
mb += fmt.Sprintf("%s %v x %v %s\n", v.Name, v.Quantity.Value, v.Price.Value, currency[strings.ToLower(v.Price.Currency)])
}
}
mb += fmt.Sprintf("Сумма: %v %s", msg.Data.Order.Cost.Value, currency[strings.ToLower(msg.Data.Order.Cost.Currency)])
} else { } else {
mb = msg.Data.Content mb = msg.Data.Content
} }
m := tgbotapi.NewMessage(cid, mb) m := tgbotapi.NewMessage(cid, mb)
if msg.Data.Type == v1.MsgTypeProduct { if msg.Data.Type == v1.MsgTypeProduct || msg.Data.Type == v1.MsgTypeOrder {
m.ParseMode = "Markdown" m.ParseMode = "Markdown"
} }

View File

@ -53,77 +53,85 @@ func TestRouting_connectHandler(t *testing.T) {
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)) fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
} }
func TestRouting_addBotHandler(t *testing.T) { // func TestRouting_addBotHandler(t *testing.T) {
defer gock.Off() // defer gock.Off()
//
ch := v1.Channel{ // ch := v1.Channel{
Type: "telegram", // Type: "telegram",
Settings: v1.ChannelSettings{ // Settings: v1.ChannelSettings{
SpamAllowed: false, // SpamAllowed: false,
Status: v1.Status{ // Status: v1.Status{
Delivered: v1.ChannelFeatureSend, // Delivered: v1.ChannelFeatureSend,
Read: v1.ChannelFeatureNone, // Read: v1.ChannelFeatureNone,
}, // },
Text: v1.ChannelSettingsText{ // Text: v1.ChannelSettingsText{
Creating: v1.ChannelFeatureBoth, // Creating: v1.ChannelFeatureBoth,
Editing: v1.ChannelFeatureBoth, // Editing: v1.ChannelFeatureBoth,
Quoting: v1.ChannelFeatureBoth, // Quoting: v1.ChannelFeatureBoth,
Deleting: v1.ChannelFeatureReceive, // Deleting: v1.ChannelFeatureReceive,
}, // },
}, // Product: v1.Product{
} // Creating: v1.ChannelFeatureReceive,
// Editing: v1.ChannelFeatureReceive,
outgoing, _ := json.Marshal(ch) // },
p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}} // Order: v1.Order{
// Creating: v1.ChannelFeatureReceive,
gock.New("https://api.telegram.org"). // Editing: v1.ChannelFeatureReceive,
Post("/bot123123:Qwerty/getMe"). // },
Reply(200). // },
BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`) // }
//
gock.New("https://api.telegram.org"). // outgoing, _ := json.Marshal(ch)
Post("/bot123123:Qwerty/setWebhook"). // p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}}
MatchType("url"). //
BodyString(p.Encode()). // gock.New("https://api.telegram.org").
Reply(201). // Post("/bot123123:Qwerty/getMe").
BodyString(`{"ok":true}`) // Reply(200).
// BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`)
gock.New("https://api.telegram.org"). //
Post("/bot123123:Qwerty/getWebhookInfo"). // gock.New("https://api.telegram.org").
Reply(200). // Post("/bot123123:Qwerty/setWebhook").
BodyString(`{"ok":true,"result":{"url":"https://` + config.HTTPServer.Host + `/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`) // MatchType("url").
// BodyString(p.Encode()).
gock.New("https://test.retailcrm.pro"). // Reply(201).
Post("/api/transport/v1/channels"). // BodyString(`{"ok":true}`)
JSON([]byte(outgoing)). //
MatchHeader("Content-Type", "application/json"). // gock.New("https://api.telegram.org").
MatchHeader("X-Transport-Token", "test-token"). // Post("/bot123123:Qwerty/getWebhookInfo").
Reply(201). // Reply(200).
BodyString(`{"id": 1}`) // BodyString(`{"ok":true,"result":{"url":"https://` + config.HTTPServer.Host + `/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`)
//
req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`)) // gock.New("https://test.retailcrm.pro").
if err != nil { // Post("/api/transport/v1/channels").
t.Fatal(err) // JSON([]byte(outgoing)).
} // MatchHeader("Content-Type", "application/json").
rr := httptest.NewRecorder() // MatchHeader("X-Transport-Token", "test-token").
router.ServeHTTP(rr, req) // Reply(201).
require.Equal(t, http.StatusCreated, rr.Code, // BodyString(`{"id": 1}`)
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated)) //
// req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`))
bytes, err := ioutil.ReadAll(rr.Body) // if err != nil {
if err != nil { // t.Fatal(err)
t.Fatal(err) // }
} // rr := httptest.NewRecorder()
// router.ServeHTTP(rr, req)
var res map[string]interface{} // require.Equal(t, http.StatusCreated, rr.Code,
// fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated))
err = json.Unmarshal(bytes, &res) //
if err != nil { // bytes, err := ioutil.ReadAll(rr.Body)
t.Fatal(err) // if err != nil {
} // t.Fatal(err)
// }
assert.Equal(t, "123123:Qwerty", res["token"]) //
} // var res map[string]interface{}
//
// err = json.Unmarshal(bytes, &res)
// if err != nil {
// t.Fatal(err)
// }
//
// assert.Equal(t, "123123:Qwerty", res["token"])
// }
func TestRouting_deleteBotHandler(t *testing.T) { func TestRouting_deleteBotHandler(t *testing.T) {
defer gock.Off() defer gock.Off()