From bf60fcb1df6f5144cd0866183647045b84ffd713 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 17 Jan 2019 12:27:11 +0300 Subject: [PATCH 1/3] update mg-transport-api-client-go --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 368c42f..c338a45 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.21 + github.com/retailcrm/mg-transport-api-client-go v1.1.23 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 ecb5e65..2dae3b9 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.21 h1:OKkey9uvB16ucICuO2hp/tfh0wfp3na0CxRiT2Iv3sE= -github.com/retailcrm/mg-transport-api-client-go v1.1.21/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= +github.com/retailcrm/mg-transport-api-client-go v1.1.23 h1:UxchsFDOKjTYIRCaBxQrKxvE7FGeHGGxIZsdqDBnjrY= +github.com/retailcrm/mg-transport-api-client-go v1.1.23/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= From e4f6822e51263cd07d746c4467a89f3cf9324023 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 17 Jan 2019 12:28:51 +0300 Subject: [PATCH 2/3] add channels deactivation --- src/routing.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/routing.go b/src/routing.go index a1433a6..d5adbfb 100644 --- a/src/routing.go +++ b/src/routing.go @@ -89,6 +89,8 @@ func addBotHandler(c *gin.Context) { err = conn.createBot(b) if err != nil { + client.DeactivateTransportChannel(data.ChannelID) + c.Error(err) return } @@ -405,11 +407,14 @@ func updateChannelsSettings() { } func updateBots(conn *Connection, hashSettings string) { + var channelIDs []uint64 bots := conn.getBotsByClientID() + if len(bots) > 0 { client := v1.New(conn.MGURL, conn.MGToken) client.Debug = config.Debug for _, bot := range bots { + channelIDs = append(channelIDs, bot.Channel) if bot.ChannelSettingsHash == hashSettings { continue } @@ -437,13 +442,51 @@ func updateBots(conn *Connection, hashSettings string) { ) } } - } + + deactivationChannels(client, channelIDs) } return } +func deactivationChannels(client *v1.MgClient, channelIDs []uint64) { + channelListItems, status, err := client.TransportChannels(v1.Channels{Active: true}) + if config.Debug { + logger.Debugf( + "TransportChannels ChannelListItems: %+v, Status: %d, err: %v", + channelListItems, status, err, + ) + } + + if len(channelListItems) > 0 { + for _, channel := range channelIDs { + for key, ch := range channelListItems { + if channel == ch.ID { + if len(channelListItems) == 1 { + channelListItems = channelListItems[:0] + break + } + + channelListItems = append(channelListItems[:key], channelListItems[key+1:]...) + } + } + } + } + + if len(channelListItems) > 0 { + for _, ch := range channelListItems { + channelListItems, status, err := client.DeactivateTransportChannel(ch.ID) + if config.Debug { + logger.Debugf( + "DeactivateTransportChannel ChannelListItems: %+v, Status: %d, err: %v", + channelListItems, status, err, + ) + } + } + } +} + func telegramWebhookHandler(c *gin.Context) { b := c.MustGet("bot").(Bot) From 6fe9c8e44b033aee0358dae4cfd912202efc15b6 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 18 Jan 2019 10:38:01 +0300 Subject: [PATCH 3/3] update mg-transport-api-client-go --- go.mod | 2 +- go.sum | 4 ++-- src/routing.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index c338a45..054f9e6 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.23 + github.com/retailcrm/mg-transport-api-client-go v1.1.24 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 2dae3b9..50ac78c 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.23 h1:UxchsFDOKjTYIRCaBxQrKxvE7FGeHGGxIZsdqDBnjrY= -github.com/retailcrm/mg-transport-api-client-go v1.1.23/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= +github.com/retailcrm/mg-transport-api-client-go v1.1.24 h1:8VdiyhCYgEhKwlXvvuJdsOMquNXEcqtHBVzDojNHxNg= +github.com/retailcrm/mg-transport-api-client-go v1.1.24/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 d5adbfb..6e287ca 100644 --- a/src/routing.go +++ b/src/routing.go @@ -444,13 +444,13 @@ func updateBots(conn *Connection, hashSettings string) { } } - deactivationChannels(client, channelIDs) + deactivateChannels(client, channelIDs) } return } -func deactivationChannels(client *v1.MgClient, channelIDs []uint64) { +func deactivateChannels(client *v1.MgClient, channelIDs []uint64) { channelListItems, status, err := client.TransportChannels(v1.Channels{Active: true}) if config.Debug { logger.Debugf(