2018-05-17 18:35:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-15 17:56:36 +03:00
|
|
|
"fmt"
|
2018-08-17 16:31:55 +03:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
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,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 16:31:55 +03:00
|
|
|
|
|
|
|
func telegramWebhookHandler(c *gin.Context) {
|
|
|
|
token := c.Param("token")
|
|
|
|
b, err := getBotByToken(token)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.ID == 0 {
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := getConnectionById(b.ConnectionID)
|
|
|
|
if !conn.Active {
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var update tgbotapi.Update
|
|
|
|
if err := c.ShouldBindJSON(&update); err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("mgWebhookHandler request: %v", update)
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = v1.New(conn.MGURL, conn.MGToken)
|
|
|
|
|
|
|
|
if update.Message != nil {
|
|
|
|
if update.Message.Text == "" {
|
|
|
|
setLocale(update.Message.From.LanguageCode)
|
|
|
|
update.Message.Text = getLocalizedMessage(getMessageID(update.Message))
|
|
|
|
}
|
|
|
|
|
|
|
|
nickname := update.Message.From.UserName
|
|
|
|
user := getUserByExternalID(update.Message.From.ID)
|
|
|
|
|
|
|
|
if update.Message.From.UserName == "" {
|
|
|
|
nickname = update.Message.From.FirstName
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Expired(config.UpdateInterval) || user.ID == 0 {
|
|
|
|
fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileID != user.UserPhotoID && fileURL != "" {
|
|
|
|
picURL, err := UploadUserAvatar(fileURL)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.UserPhotoID = fileID
|
|
|
|
user.UserPhotoURL = picURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.ExternalID == 0 {
|
|
|
|
user.ExternalID = update.Message.From.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
err = user.save()
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lang := update.Message.From.LanguageCode
|
|
|
|
|
|
|
|
if len(update.Message.From.LanguageCode) > 2 {
|
|
|
|
lang = update.Message.From.LanguageCode[:2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("telegramWebhookHandler user %v", user)
|
|
|
|
}
|
|
|
|
|
|
|
|
snd := v1.SendData{
|
|
|
|
Message: v1.SendMessage{
|
|
|
|
Message: v1.Message{
|
|
|
|
ExternalID: strconv.Itoa(update.Message.MessageID),
|
|
|
|
Type: "text",
|
|
|
|
Text: update.Message.Text,
|
|
|
|
},
|
|
|
|
SentAt: time.Now(),
|
|
|
|
},
|
|
|
|
User: v1.User{
|
|
|
|
ExternalID: strconv.Itoa(update.Message.From.ID),
|
|
|
|
Nickname: nickname,
|
|
|
|
Firstname: update.Message.From.FirstName,
|
|
|
|
Avatar: user.UserPhotoURL,
|
|
|
|
Lastname: update.Message.From.LastName,
|
|
|
|
Language: lang,
|
|
|
|
},
|
|
|
|
Channel: b.Channel,
|
|
|
|
ExternalChatID: strconv.FormatInt(update.Message.Chat.ID, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
if update.Message.ReplyToMessage != nil {
|
|
|
|
snd.Quote = &v1.SendMessageRequestQuote{ExternalID: strconv.Itoa(update.Message.ReplyToMessage.MessageID)}
|
|
|
|
}
|
|
|
|
|
|
|
|
data, st, err := client.Messages(snd)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(b.Token, err.Error(), st, data)
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("telegramWebhookHandler Type: SendMessage, Bot: %v, Message: %v, Response: %v", b.ID, snd, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if update.EditedMessage != nil {
|
|
|
|
if update.EditedMessage.Text == "" {
|
|
|
|
setLocale(update.EditedMessage.From.LanguageCode)
|
|
|
|
update.EditedMessage.Text = getLocalizedMessage(getMessageID(update.Message))
|
|
|
|
}
|
|
|
|
|
|
|
|
snd := v1.UpdateData{
|
|
|
|
Message: v1.UpdateMessage{
|
|
|
|
Message: v1.Message{
|
|
|
|
ExternalID: strconv.Itoa(update.EditedMessage.MessageID),
|
|
|
|
Type: "text",
|
|
|
|
Text: update.EditedMessage.Text,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Channel: b.Channel,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, st, err := client.UpdateMessages(snd)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(b.Token, err.Error(), st, data)
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("telegramWebhookHandler Type: UpdateMessage, Bot: %v, Message: %v, Response: %v", b.ID, snd, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func mgWebhookHandler(c *gin.Context) {
|
|
|
|
clientID := c.GetHeader("Clientid")
|
|
|
|
if clientID == "" {
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := getConnection(clientID)
|
|
|
|
if !conn.Active {
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg v1.WebhookRequest
|
|
|
|
if err := c.ShouldBindJSON(&msg); err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("mgWebhookHandler request: %v", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
uid, _ := strconv.Atoi(msg.Data.ExternalMessageID)
|
|
|
|
cid, _ := strconv.ParseInt(msg.Data.ExternalChatID, 10, 64)
|
|
|
|
|
|
|
|
b := getBot(conn.ID, msg.Data.ChannelID)
|
|
|
|
if b.ID == 0 {
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bot, err := tgbotapi.NewBotAPI(b.Token)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type == "message_sent" {
|
|
|
|
m := tgbotapi.NewMessage(cid, msg.Data.Content)
|
|
|
|
|
|
|
|
if msg.Data.QuoteExternalID != "" {
|
|
|
|
qid, err := strconv.Atoi(msg.Data.QuoteExternalID)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.ReplyToMessageID = qid
|
|
|
|
}
|
|
|
|
|
|
|
|
msgSend, err := bot.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("mgWebhookHandler sent %v", msgSend)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"external_message_id": strconv.Itoa(msgSend.MessageID)})
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type == "message_updated" {
|
|
|
|
msgSend, err := bot.Send(tgbotapi.NewEditMessageText(cid, uid, msg.Data.Content))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("mgWebhookHandler update %v", msgSend)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type == "message_deleted" {
|
|
|
|
msgSend, err := bot.Send(tgbotapi.NewDeleteMessage(cid, uid))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
logger.Debugf("mgWebhookHandler delete %v", msgSend)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
|
|
}
|
|
|
|
}
|