2018-05-17 18:35:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-08-15 17:56:36 +03:00
|
|
|
|
|
|
|
"fmt"
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2018-05-23 18:03:11 +03:00
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2018-05-17 18:35:53 +03:00
|
|
|
"github.com/retailcrm/api-client-go/v5"
|
|
|
|
"github.com/retailcrm/mg-transport-api-client-go/v1"
|
|
|
|
)
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func connectHandler(c *gin.Context) {
|
2018-05-18 18:11:09 +03:00
|
|
|
res := struct {
|
2018-08-15 17:56:36 +03:00
|
|
|
Conn Connection
|
2018-08-14 17:49:01 +03:00
|
|
|
Locale map[string]string
|
2018-05-18 18:11:09 +03:00
|
|
|
}{
|
2018-08-15 17:56:36 +03:00
|
|
|
c.MustGet("account").(Connection),
|
2018-08-14 17:49:01 +03:00
|
|
|
getLocale(),
|
2018-05-18 18:11:09 +03:00
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
c.HTML(http.StatusOK, "home", &res)
|
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func addBotHandler(c *gin.Context) {
|
2018-08-15 17:56:36 +03:00
|
|
|
b := c.MustGet("bot").(Bot)
|
2018-05-31 19:44:23 +03:00
|
|
|
cl, err := getBotByToken(b.Token)
|
2018-05-31 16:18:52 +03:00
|
|
|
if err != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Error(err)
|
2018-05-25 18:09:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-31 19:44:23 +03:00
|
|
|
if cl.ID != 0 {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("bot_already_created")})
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-04 16:48:04 +03:00
|
|
|
bot, err := tgbotapi.NewBotAPI(b.Token)
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_token")})
|
2018-08-16 18:13:01 +03:00
|
|
|
logger.Error(b.Token, err.Error())
|
2018-05-23 18:03:11 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-01 12:08:22 +03:00
|
|
|
bot.Debug = config.Debug
|
2018-05-23 18:03:11 +03:00
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
wr, err := bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token))
|
2018-05-23 18:03:11 +03:00
|
|
|
if err != nil {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")})
|
2018-08-16 18:13:01 +03:00
|
|
|
logger.Error(b.Token, err.Error())
|
2018-05-23 18:03:11 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
if !wr.Ok {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")})
|
2018-08-16 18:13:01 +03:00
|
|
|
logger.Error(b.Token, wr.ErrorCode, wr.Result)
|
2018-05-25 18:09:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
b.Name = bot.Self.FirstName
|
2018-05-17 18:35:53 +03:00
|
|
|
|
|
|
|
ch := v1.Channel{
|
|
|
|
Type: "telegram",
|
|
|
|
Events: []string{
|
|
|
|
"message_sent",
|
|
|
|
"message_updated",
|
|
|
|
"message_deleted",
|
|
|
|
"message_read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
conn := getConnectionById(b.ConnectionID)
|
2018-05-31 18:17:19 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
var client = v1.New(conn.MGURL, conn.MGToken)
|
2018-05-17 18:35:53 +03:00
|
|
|
data, status, err := client.ActivateTransportChannel(ch)
|
|
|
|
if status != http.StatusCreated {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activating_channel")})
|
2018-08-16 18:13:01 +03:00
|
|
|
logger.Error(conn.APIURL, status, err.Error(), data)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Channel = data.ChannelID
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
err = conn.createBot(b)
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Error(err)
|
2018-05-24 17:16:21 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-15 17:56:36 +03:00
|
|
|
c.JSON(http.StatusCreated, b)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func deleteBotHandler(c *gin.Context) {
|
2018-08-15 17:56:36 +03:00
|
|
|
b := c.MustGet("bot").(Bot)
|
2018-08-14 17:49:01 +03:00
|
|
|
conn := getConnectionById(b.ConnectionID)
|
|
|
|
if conn.MGURL == "" || conn.MGToken == "" {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("not_found_account")})
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(b.ID, "MGURL or MGToken is empty")
|
2018-05-25 18:09:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
var client = v1.New(conn.MGURL, conn.MGToken)
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
data, status, err := client.DeactivateTransportChannel(getBotChannelByToken(b.Token))
|
|
|
|
if status > http.StatusOK {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_deactivating_channel")})
|
2018-06-28 12:17:19 +03:00
|
|
|
logger.Error(b.ID, status, err.Error(), data)
|
|
|
|
return
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
err = b.deleteBot()
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Error(err)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{})
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func settingsHandler(c *gin.Context) {
|
|
|
|
uid := c.Param("uid")
|
2018-05-18 18:11:09 +03:00
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
p := getConnection(uid)
|
2018-05-17 18:35:53 +03:00
|
|
|
if p.ID == 0 {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Redirect(http.StatusFound, "/")
|
2018-05-29 10:08:38 +03:00
|
|
|
return
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
bots := p.getBotsByClientID()
|
2018-05-17 18:35:53 +03:00
|
|
|
|
|
|
|
res := struct {
|
2018-05-18 18:11:09 +03:00
|
|
|
Conn *Connection
|
|
|
|
Bots Bots
|
2018-08-14 17:49:01 +03:00
|
|
|
Locale map[string]string
|
2018-05-17 18:35:53 +03:00
|
|
|
}{
|
|
|
|
p,
|
|
|
|
bots,
|
2018-08-14 17:49:01 +03:00
|
|
|
getLocale(),
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
c.HTML(http.StatusOK, "form", &res)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func saveHandler(c *gin.Context) {
|
2018-08-15 17:56:36 +03:00
|
|
|
conn := c.MustGet("connection").(Connection)
|
2018-08-14 17:49:01 +03:00
|
|
|
_, err, code := getAPIClient(conn.APIURL, conn.APIKEY)
|
2018-05-28 18:16:13 +03:00
|
|
|
if err != nil {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(code, gin.H{"error": err.Error()})
|
2018-05-28 18:16:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
err = conn.saveConnection()
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Error(err)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": getLocalizedMessage("successful")})
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func createHandler(c *gin.Context) {
|
2018-08-15 17:56:36 +03:00
|
|
|
conn := c.MustGet("connection").(Connection)
|
2018-05-22 10:34:39 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
cl := getConnectionByURL(conn.APIURL)
|
2018-05-17 18:35:53 +03:00
|
|
|
if cl.ID != 0 {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("connection_already_created")})
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:13:01 +03:00
|
|
|
client, err, code := getAPIClient(conn.APIURL, conn.APIKEY)
|
2018-05-28 18:16:13 +03:00
|
|
|
if err != nil {
|
2018-08-16 18:13:01 +03:00
|
|
|
if code == http.StatusInternalServerError {
|
|
|
|
c.Error(err)
|
|
|
|
} else {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(code, gin.H{"error": err.Error()})
|
2018-08-16 18:13:01 +03:00
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-15 17:56:36 +03:00
|
|
|
conn.ClientID = GenerateToken()
|
|
|
|
data, status, errr := client.IntegrationModuleEdit(getIntegrationModule(conn.ClientID))
|
2018-05-17 18:35:53 +03:00
|
|
|
if errr.RuntimeErr != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Error(errr.RuntimeErr)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status >= http.StatusBadRequest {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activity_mg")})
|
2018-08-14 17:49:01 +03:00
|
|
|
logger.Error(conn.APIURL, status, errr.ApiErr, data)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
conn.MGURL = data.Info["baseUrl"]
|
|
|
|
conn.MGToken = data.Info["token"]
|
|
|
|
conn.Active = true
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
err = conn.createConnection()
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.Error(err)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(
|
|
|
|
http.StatusCreated,
|
|
|
|
gin.H{
|
|
|
|
"url": "/settings/" + conn.ClientID,
|
|
|
|
"message": getLocalizedMessage("successful"),
|
|
|
|
},
|
|
|
|
)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func activityHandler(c *gin.Context) {
|
|
|
|
var rec v5.ActivityCallback
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
if err := c.ShouldBindJSON(&rec); err != nil {
|
|
|
|
c.Error(err)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
conn := getConnection(rec.ClientId)
|
|
|
|
if conn.ID == 0 {
|
2018-08-17 13:36:25 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest,
|
2018-08-14 17:49:01 +03:00
|
|
|
gin.H{
|
|
|
|
"success": false,
|
|
|
|
"error": "Wrong data",
|
|
|
|
},
|
|
|
|
)
|
2018-05-24 17:16:21 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
conn.Active = rec.Activity.Active && !rec.Activity.Freeze
|
2018-05-28 18:16:13 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
if err := conn.setConnectionActivity(); err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
2018-05-28 18:16:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
2018-05-28 18:16:13 +03:00
|
|
|
}
|
2018-08-15 17:56:36 +03:00
|
|
|
|
|
|
|
func getIntegrationModule(clientId string) v5.IntegrationModule {
|
|
|
|
return v5.IntegrationModule{
|
2018-08-16 18:13:01 +03:00
|
|
|
Code: config.TransportInfo.Code,
|
|
|
|
IntegrationCode: config.TransportInfo.Code,
|
2018-08-15 17:56:36 +03:00
|
|
|
Active: true,
|
2018-08-16 18:13:01 +03:00
|
|
|
Name: config.TransportInfo.Name,
|
2018-08-15 17:56:36 +03:00
|
|
|
ClientID: clientId,
|
|
|
|
Logo: fmt.Sprintf(
|
2018-08-16 18:13:01 +03:00
|
|
|
"https://%s%s",
|
2018-08-15 17:56:36 +03:00
|
|
|
config.HTTPServer.Host,
|
2018-08-16 18:13:01 +03:00
|
|
|
config.TransportInfo.LogoPath,
|
2018-08-15 17:56:36 +03:00
|
|
|
),
|
|
|
|
BaseURL: fmt.Sprintf(
|
|
|
|
"https://%s",
|
|
|
|
config.HTTPServer.Host,
|
|
|
|
),
|
|
|
|
AccountURL: fmt.Sprintf(
|
|
|
|
"https://%s/settings/%s",
|
|
|
|
config.HTTPServer.Host,
|
|
|
|
clientId,
|
|
|
|
),
|
|
|
|
Actions: map[string]string{"activity": "/actions/activity"},
|
|
|
|
Integrations: &v5.Integrations{
|
|
|
|
MgTransport: &v5.MgTransport{
|
|
|
|
WebhookUrl: fmt.Sprintf(
|
|
|
|
"https://%s/webhook/",
|
|
|
|
config.HTTPServer.Host,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|