add update channels settings
This commit is contained in:
parent
686b24b957
commit
38b6cac55c
1
migrations/1537271655_app.down.sql
Normal file
1
migrations/1537271655_app.down.sql
Normal file
@ -0,0 +1 @@
|
||||
alter table bot drop column channel_settings_hash;
|
1
migrations/1537271655_app.up.sql
Normal file
1
migrations/1537271655_app.up.sql
Normal file
@ -0,0 +1 @@
|
||||
alter table bot add column channel_settings_hash varchar(70);
|
@ -14,6 +14,8 @@ type Options struct {
|
||||
Config string `short:"c" long:"config" default:"config.yml" description:"Path to configuration file"`
|
||||
}
|
||||
|
||||
const Type = "telegram"
|
||||
|
||||
var (
|
||||
config *TransportConfig
|
||||
orm *Orm
|
||||
|
@ -21,6 +21,7 @@ type Bot struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
ConnectionID int `gorm:"connection_id" json:"connectionId,omitempty"`
|
||||
Channel uint64 `gorm:"channel;not null;unique" json:"channel,omitempty"`
|
||||
ChannelSettingsHash string `gorm:"channel_settings_hash type:varchar(70)"`
|
||||
Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"`
|
||||
Name string `gorm:"name type:varchar(40)" json:"name,omitempty"`
|
||||
CreatedAt time.Time
|
||||
|
@ -13,6 +13,13 @@ func getConnection(uid string) *Connection {
|
||||
return &connection
|
||||
}
|
||||
|
||||
func getConnections() []*Connection {
|
||||
var connection []*Connection
|
||||
orm.DB.Find(&connection)
|
||||
|
||||
return connection
|
||||
}
|
||||
|
||||
func getConnectionByURL(urlCrm string) *Connection {
|
||||
var connection Connection
|
||||
orm.DB.First(&connection, "api_url = ?", urlCrm)
|
||||
@ -52,6 +59,10 @@ func getBotByToken(token string) (*Bot, error) {
|
||||
return &bot, nil
|
||||
}
|
||||
|
||||
func (b *Bot) save() error {
|
||||
return orm.DB.Save(b).Error
|
||||
}
|
||||
|
||||
func (b *Bot) deleteBot() error {
|
||||
return orm.DB.Delete(b, "token = ?", b.Token).Error
|
||||
}
|
||||
|
122
src/routing.go
122
src/routing.go
@ -58,36 +58,10 @@ func addBotHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
b.Name = bot.Self.FirstName
|
||||
|
||||
ch := v1.Channel{
|
||||
Type: "telegram",
|
||||
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,
|
||||
},
|
||||
Product: v1.Product{
|
||||
Creating: v1.ChannelFeatureReceive,
|
||||
Editing: v1.ChannelFeatureReceive,
|
||||
},
|
||||
Order: v1.Order{
|
||||
Creating: v1.ChannelFeatureReceive,
|
||||
Editing: v1.ChannelFeatureReceive,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
conn := getConnectionById(b.ConnectionID)
|
||||
client := v1.New(conn.MGURL, conn.MGToken)
|
||||
|
||||
var client = v1.New(conn.MGURL, conn.MGToken)
|
||||
data, status, err := client.ActivateTransportChannel(ch)
|
||||
data, status, err := client.ActivateTransportChannel(getChannelSettings())
|
||||
if status != http.StatusCreated {
|
||||
c.AbortWithStatusJSON(BadRequest("error_activating_channel"))
|
||||
logger.Error(conn.APIURL, status, err.Error(), data)
|
||||
@ -305,6 +279,98 @@ func getIntegrationModule(clientId string) v5.IntegrationModule {
|
||||
}
|
||||
}
|
||||
|
||||
func getChannelSettings(cid ...uint64) v1.Channel {
|
||||
var channelID uint64
|
||||
|
||||
if len(cid) > 0 {
|
||||
channelID = cid[0]
|
||||
}
|
||||
|
||||
return v1.Channel{
|
||||
ID: channelID,
|
||||
Type: Type,
|
||||
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,
|
||||
},
|
||||
Product: v1.Product{
|
||||
Creating: v1.ChannelFeatureReceive,
|
||||
Editing: v1.ChannelFeatureReceive,
|
||||
},
|
||||
Order: v1.Order{
|
||||
Creating: v1.ChannelFeatureReceive,
|
||||
Editing: v1.ChannelFeatureReceive,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func updateChannelsSettings() {
|
||||
hashSettings, err := getChannelSettingsHash()
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
connections := getConnections()
|
||||
if len(connections) > 0 {
|
||||
for _, conn := range connections {
|
||||
if !conn.Active {
|
||||
logger.Infof(
|
||||
"updateChannelsSettings connection %s deactivated",
|
||||
conn.APIURL,
|
||||
)
|
||||
continue
|
||||
}
|
||||
updateBots(conn, hashSettings)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func updateBots(conn *Connection, hashSettings string) {
|
||||
bots := conn.getBotsByClientID()
|
||||
if len(bots) > 0 {
|
||||
client := v1.New(conn.MGURL, conn.MGToken)
|
||||
for _, bot := range bots {
|
||||
if bot.ChannelSettingsHash == hashSettings {
|
||||
continue
|
||||
}
|
||||
|
||||
data, status, err := client.UpdateTransportChannel(getChannelSettings(bot.Channel))
|
||||
if config.Debug {
|
||||
logger.Infof(
|
||||
"updateChannelsSettings apiURL: %s, ChannelID: %d, Data: %v, Status: %d, err: %v",
|
||||
conn.APIURL, bot.Channel, data, status, err,
|
||||
)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
bot.ChannelSettingsHash = hashSettings
|
||||
err = bot.save()
|
||||
if err != nil {
|
||||
logger.Error(
|
||||
"updateChannelsSettings bot.save apiURL: %s, bot.Channel: %d , err: %v",
|
||||
conn.APIURL, bot.Channel, err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func telegramWebhookHandler(c *gin.Context) {
|
||||
token := c.Param("token")
|
||||
b, err := getBotByToken(token)
|
||||
|
@ -53,6 +53,7 @@ func start() {
|
||||
func setup() *gin.Engine {
|
||||
loadTranslateFile()
|
||||
setValidation()
|
||||
updateChannelsSettings()
|
||||
|
||||
if config.Debug == false {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
12
src/utils.go
12
src/utils.go
@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@ -119,3 +121,13 @@ func UploadUserAvatar(url string) (picURLs3 string, err error) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getChannelSettingsHash() (hash string, err error) {
|
||||
res, err := json.Marshal(getChannelSettings())
|
||||
|
||||
h := sha1.New()
|
||||
h.Write(res)
|
||||
hash = fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user