2018-05-17 18:35:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
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-06-01 12:08:22 +03:00
|
|
|
rx := regexp.MustCompile(`/+$`)
|
2018-08-14 17:49:01 +03:00
|
|
|
ra := rx.ReplaceAllString(c.Query("account"), ``)
|
2018-06-01 12:08:22 +03:00
|
|
|
p := Connection{
|
|
|
|
APIURL: ra,
|
|
|
|
}
|
2018-05-18 18:11:09 +03:00
|
|
|
|
|
|
|
res := struct {
|
|
|
|
Conn *Connection
|
2018-08-14 17:49:01 +03:00
|
|
|
Locale map[string]string
|
2018-05-18 18:11:09 +03:00
|
|
|
}{
|
|
|
|
&p,
|
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-05-17 18:35:53 +03:00
|
|
|
var b Bot
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
if err := c.ShouldBindJSON(&b); err != nil {
|
|
|
|
c.Error(err)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Token == "" {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("no_bot_token")})
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-14 17:49:01 +03:00
|
|
|
c.JSON(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 {
|
|
|
|
logger.Error(b.Token, err.Error())
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_token")})
|
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 {
|
|
|
|
logger.Error(b.Token, err.Error())
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")})
|
2018-05-23 18:03:11 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
if !wr.Ok {
|
|
|
|
logger.Error(b.Token, wr.ErrorCode, wr.Result)
|
2018-08-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")})
|
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-14 17:49:01 +03:00
|
|
|
logger.Error(conn.APIURL, status, err.Error(), data)
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activating_channel")})
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonString, err := json.Marshal(b)
|
|
|
|
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, jsonString)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
func deleteBotHandler(c *gin.Context) {
|
2018-05-17 18:35:53 +03:00
|
|
|
var b Bot
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
if err := c.ShouldBindJSON(&b); err != nil {
|
|
|
|
c.Error(err)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
conn := getConnectionById(b.ConnectionID)
|
|
|
|
if conn.MGURL == "" || conn.MGToken == "" {
|
|
|
|
c.JSON(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-14 17:49:01 +03:00
|
|
|
c.JSON(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) {
|
|
|
|
var conn Connection
|
2018-05-17 18:35:53 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
if err := c.BindJSON(&conn); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_url_key")})
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-14 17:49:01 +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) {
|
|
|
|
var conn Connection
|
2018-05-22 10:34:39 +03:00
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
if err := c.BindJSON(&conn); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_url_key")})
|
2018-05-22 10:34:39 +03:00
|
|
|
return
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
conn.ClientID = GenerateToken()
|
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-14 17:49:01 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("connection_already_created")})
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:49:01 +03:00
|
|
|
client, err, _ := getAPIClient(conn.APIURL, conn.APIKEY)
|
2018-05-28 18:16:13 +03:00
|
|
|
if err != nil {
|
2018-08-14 17:49:01 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
integration := v5.IntegrationModule{
|
|
|
|
Code: transport,
|
|
|
|
IntegrationCode: transport,
|
|
|
|
Active: true,
|
2018-05-23 17:03:37 +03:00
|
|
|
Name: "Telegram",
|
2018-08-14 17:49:01 +03:00
|
|
|
ClientID: conn.ClientID,
|
2018-05-23 17:03:37 +03:00
|
|
|
Logo: fmt.Sprintf(
|
2018-05-28 18:16:13 +03:00
|
|
|
"https://%s/static/telegram_logo.svg",
|
2018-05-23 17:03:37 +03:00
|
|
|
config.HTTPServer.Host,
|
|
|
|
),
|
2018-05-22 10:34:39 +03:00
|
|
|
BaseURL: fmt.Sprintf(
|
|
|
|
"https://%s",
|
|
|
|
config.HTTPServer.Host,
|
|
|
|
),
|
2018-05-17 18:35:53 +03:00
|
|
|
AccountURL: fmt.Sprintf(
|
2018-05-22 10:34:39 +03:00
|
|
|
"https://%s/settings/%s",
|
2018-05-17 18:35:53 +03:00
|
|
|
config.HTTPServer.Host,
|
2018-08-14 17:49:01 +03:00
|
|
|
conn.ClientID,
|
2018-05-17 18:35:53 +03:00
|
|
|
),
|
|
|
|
Actions: map[string]string{"activity": "/actions/activity"},
|
|
|
|
Integrations: &v5.Integrations{
|
|
|
|
MgTransport: &v5.MgTransport{
|
|
|
|
WebhookUrl: fmt.Sprintf(
|
2018-06-01 19:54:25 +03:00
|
|
|
"https://%s/webhook/",
|
2018-05-17 18:35:53 +03:00
|
|
|
config.HTTPServer.Host,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, status, errr := client.IntegrationModuleEdit(integration)
|
|
|
|
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-14 17:49:01 +03:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activity_mg")})
|
|
|
|
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 {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest,
|
|
|
|
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
|
|
|
}
|