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

add channels deactivation

This commit is contained in:
DmitryZagorulko 2019-01-17 12:28:51 +03:00
parent bf60fcb1df
commit e4f6822e51

View File

@ -89,6 +89,8 @@ func addBotHandler(c *gin.Context) {
err = conn.createBot(b) err = conn.createBot(b)
if err != nil { if err != nil {
client.DeactivateTransportChannel(data.ChannelID)
c.Error(err) c.Error(err)
return return
} }
@ -405,11 +407,14 @@ func updateChannelsSettings() {
} }
func updateBots(conn *Connection, hashSettings string) { func updateBots(conn *Connection, hashSettings string) {
var channelIDs []uint64
bots := conn.getBotsByClientID() bots := conn.getBotsByClientID()
if len(bots) > 0 { if len(bots) > 0 {
client := v1.New(conn.MGURL, conn.MGToken) client := v1.New(conn.MGURL, conn.MGToken)
client.Debug = config.Debug client.Debug = config.Debug
for _, bot := range bots { for _, bot := range bots {
channelIDs = append(channelIDs, bot.Channel)
if bot.ChannelSettingsHash == hashSettings { if bot.ChannelSettingsHash == hashSettings {
continue continue
} }
@ -437,13 +442,51 @@ func updateBots(conn *Connection, hashSettings string) {
) )
} }
} }
} }
deactivationChannels(client, channelIDs)
} }
return 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) { func telegramWebhookHandler(c *gin.Context) {
b := c.MustGet("bot").(Bot) b := c.MustGet("bot").(Bot)