1
0
mirror of synced 2024-11-22 12:26:02 +03:00
mg-transport-telegram/routing.go

309 lines
6.4 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"github.com/gin-gonic/gin"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/retailcrm/api-client-go/v5"
"github.com/retailcrm/mg-transport-api-client-go/v1"
)
func connectHandler(c *gin.Context) {
2018-06-01 12:08:22 +03:00
rx := regexp.MustCompile(`/+$`)
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
Locale map[string]string
2018-05-18 18:11:09 +03:00
}{
&p,
getLocale(),
2018-05-18 18:11:09 +03:00
}
c.HTML(http.StatusOK, "home", &res)
}
func addBotHandler(c *gin.Context) {
var b Bot
if err := c.ShouldBindJSON(&b); err != nil {
c.Error(err)
return
}
if b.Token == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("no_bot_token")})
return
}
2018-05-31 19:44:23 +03:00
cl, err := getBotByToken(b.Token)
if err != nil {
c.Error(err)
2018-05-25 18:09:38 +03:00
return
}
2018-05-31 19:44:23 +03:00
if cl.ID != 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("bot_already_created")})
return
}
bot, err := tgbotapi.NewBotAPI(b.Token)
if err != nil {
logger.Error(b.Token, err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_token")})
return
}
2018-06-01 12:08:22 +03:00
bot.Debug = config.Debug
2018-05-25 18:09:38 +03:00
wr, err := bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token))
if err != nil {
logger.Error(b.Token, err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")})
return
}
2018-05-25 18:09:38 +03:00
if !wr.Ok {
logger.Error(b.Token, wr.ErrorCode, wr.Result)
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_creating_webhook")})
2018-05-25 18:09:38 +03:00
return
}
b.Name = bot.Self.FirstName
ch := v1.Channel{
Type: "telegram",
Events: []string{
"message_sent",
"message_updated",
"message_deleted",
"message_read",
},
}
conn := getConnectionById(b.ConnectionID)
2018-05-31 18:17:19 +03:00
var client = v1.New(conn.MGURL, conn.MGToken)
data, status, err := client.ActivateTransportChannel(ch)
if status != http.StatusCreated {
logger.Error(conn.APIURL, status, err.Error(), data)
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activating_channel")})
return
}
b.Channel = data.ChannelID
err = conn.createBot(b)
if err != nil {
c.Error(err)
2018-05-24 17:16:21 +03:00
return
}
jsonString, err := json.Marshal(b)
if err != nil {
c.Error(err)
return
}
c.JSON(http.StatusCreated, jsonString)
}
func deleteBotHandler(c *gin.Context) {
var b Bot
if err := c.ShouldBindJSON(&b); err != nil {
c.Error(err)
return
}
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
}
var client = v1.New(conn.MGURL, conn.MGToken)
data, status, err := client.DeactivateTransportChannel(getBotChannelByToken(b.Token))
if status > http.StatusOK {
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_deactivating_channel")})
logger.Error(b.ID, status, err.Error(), data)
return
}
err = b.deleteBot()
if err != nil {
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{})
}
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)
if p.ID == 0 {
c.Redirect(http.StatusFound, "/")
2018-05-29 10:08:38 +03:00
return
}
bots := p.getBotsByClientID()
res := struct {
2018-05-18 18:11:09 +03:00
Conn *Connection
Bots Bots
Locale map[string]string
}{
p,
bots,
getLocale(),
}
c.HTML(http.StatusOK, "form", &res)
}
func saveHandler(c *gin.Context) {
var conn Connection
if err := c.BindJSON(&conn); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_url_key")})
return
}
_, err, code := getAPIClient(conn.APIURL, conn.APIKEY)
2018-05-28 18:16:13 +03:00
if err != nil {
c.AbortWithStatusJSON(code, gin.H{"error": err.Error()})
2018-05-28 18:16:13 +03:00
return
}
err = conn.saveConnection()
if err != nil {
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"message": getLocalizedMessage("successful")})
}
func createHandler(c *gin.Context) {
var conn Connection
2018-05-22 10:34:39 +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
}
conn.ClientID = GenerateToken()
2018-05-22 10:34:39 +03:00
cl := getConnectionByURL(conn.APIURL)
if cl.ID != 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("connection_already_created")})
return
}
client, err, _ := getAPIClient(conn.APIURL, conn.APIKEY)
2018-05-28 18:16:13 +03:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
integration := v5.IntegrationModule{
Code: transport,
IntegrationCode: transport,
Active: true,
Name: "Telegram",
ClientID: conn.ClientID,
Logo: fmt.Sprintf(
2018-05-28 18:16:13 +03:00
"https://%s/static/telegram_logo.svg",
config.HTTPServer.Host,
),
2018-05-22 10:34:39 +03:00
BaseURL: fmt.Sprintf(
"https://%s",
config.HTTPServer.Host,
),
AccountURL: fmt.Sprintf(
2018-05-22 10:34:39 +03:00
"https://%s/settings/%s",
config.HTTPServer.Host,
conn.ClientID,
),
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/",
config.HTTPServer.Host,
),
},
},
}
data, status, errr := client.IntegrationModuleEdit(integration)
if errr.RuntimeErr != nil {
c.Error(errr.RuntimeErr)
return
}
if status >= http.StatusBadRequest {
c.JSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("error_activity_mg")})
logger.Error(conn.APIURL, status, errr.ApiErr, data)
return
}
conn.MGURL = data.Info["baseUrl"]
conn.MGToken = data.Info["token"]
conn.Active = true
err = conn.createConnection()
if err != nil {
c.Error(err)
return
}
c.JSON(
http.StatusCreated,
gin.H{
"url": "/settings/" + conn.ClientID,
"message": getLocalizedMessage("successful"),
},
)
}
func activityHandler(c *gin.Context) {
var rec v5.ActivityCallback
if err := c.ShouldBindJSON(&rec); err != nil {
c.Error(err)
return
}
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
}
conn.Active = rec.Activity.Active && !rec.Activity.Freeze
2018-05-28 18:16:13 +03:00
if err := conn.setConnectionActivity(); err != nil {
c.Error(err)
return
2018-05-28 18:16:13 +03:00
}
c.JSON(http.StatusOK, gin.H{"success": true})
2018-05-28 18:16:13 +03:00
}