2018-05-17 18:35:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2018-05-28 18:16:13 +03:00
|
|
|
"strings"
|
2018-05-17 18:35:53 +03:00
|
|
|
|
|
|
|
"github.com/getsentry/raven-go"
|
2018-05-23 18:03:11 +03:00
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2018-05-18 18:11:09 +03:00
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
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-05-18 18:11:09 +03:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
"gopkg.in/yaml.v2"
|
2018-05-17 18:35:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-05-23 18:03:11 +03:00
|
|
|
validPath = regexp.MustCompile(`^/(save|settings|telegram)/([a-zA-Z0-9-:_+]+)$`)
|
2018-05-18 18:11:09 +03:00
|
|
|
localizer *i18n.Localizer
|
|
|
|
bundle = &i18n.Bundle{DefaultLanguage: language.English}
|
|
|
|
matcher = language.NewMatcher([]language.Tag{
|
|
|
|
language.English,
|
|
|
|
language.Russian,
|
|
|
|
language.Spanish,
|
|
|
|
})
|
2018-05-17 18:35:53 +03:00
|
|
|
)
|
|
|
|
|
2018-05-18 18:11:09 +03:00
|
|
|
func init() {
|
|
|
|
bundle.RegisterUnmarshalFunc("yml", yaml.Unmarshal)
|
|
|
|
files, err := ioutil.ReadDir("translate")
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if !f.IsDir() {
|
|
|
|
bundle.MustLoadMessageFile("translate/" + f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setLocale(al string) {
|
|
|
|
tag, _ := language.MatchStrings(matcher, al)
|
|
|
|
localizer = i18n.NewLocalizer(bundle, tag.String())
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
// Response struct
|
|
|
|
type Response struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func setWrapperRoutes() {
|
|
|
|
http.HandleFunc("/", connectHandler)
|
|
|
|
http.HandleFunc("/settings/", makeHandler(settingsHandler))
|
|
|
|
http.HandleFunc("/save/", saveHandler)
|
|
|
|
http.HandleFunc("/create/", createHandler)
|
|
|
|
http.HandleFunc("/actions/activity", activityHandler)
|
2018-05-25 18:09:38 +03:00
|
|
|
http.HandleFunc("/add-bot/", addBotHandler)
|
|
|
|
http.HandleFunc("/activity-bot/", activityBotHandler)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) {
|
2018-05-23 18:03:11 +03:00
|
|
|
tm, err := template.ParseFiles("templates/layout.html", "templates/"+tmpl+".html")
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 17:16:21 +03:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2018-05-23 18:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tm.Execute(w, &c)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 17:16:21 +03:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fn(w, r, m[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-18 18:11:09 +03:00
|
|
|
setLocale(r.Header.Get("Accept-Language"))
|
2018-05-17 18:35:53 +03:00
|
|
|
p := Connection{}
|
2018-05-18 18:11:09 +03:00
|
|
|
|
|
|
|
res := struct {
|
|
|
|
Conn *Connection
|
|
|
|
Locale map[string]interface{}
|
|
|
|
}{
|
|
|
|
&p,
|
|
|
|
map[string]interface{}{
|
2018-05-25 18:09:38 +03:00
|
|
|
"ButtonSave": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "button_save"}),
|
2018-05-22 10:34:39 +03:00
|
|
|
"ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}),
|
2018-05-24 11:33:04 +03:00
|
|
|
"Title": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "title"}),
|
2018-05-18 18:11:09 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
renderTemplate(w, "home", &res)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-25 15:56:31 +03:00
|
|
|
setLocale(r.Header.Get("Accept-Language"))
|
2018-05-17 18:35:53 +03:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(err.Error())
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var b Bot
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &b)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(err.Error())
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Token == "" {
|
2018-05-23 18:03:11 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "no_bot_token"}), http.StatusBadRequest)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
c := getConnectionById(b.ConnectionID)
|
2018-05-25 18:09:38 +03:00
|
|
|
if c.MGURL == "" || c.MGToken == "" {
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(b.Token, "MGURL or MGToken is empty")
|
2018-05-25 18:09:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
cl := getBotByToken(b.Token)
|
2018-05-17 18:35:53 +03:00
|
|
|
if cl.ID != 0 {
|
2018-05-23 18:03:11 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bot, err := GetBotInfo(b.Token)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(b.Token, err.Error())
|
2018-05-23 18:03:11 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_token"}), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.Debug = false
|
|
|
|
|
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())
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
if !wr.Ok {
|
|
|
|
logger.Error(b.Token, wr.ErrorCode, wr.Result)
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
b.Name = GetBotName(bot)
|
|
|
|
|
|
|
|
ch := v1.Channel{
|
|
|
|
Type: "telegram",
|
|
|
|
Events: []string{
|
|
|
|
"message_sent",
|
|
|
|
"message_updated",
|
|
|
|
"message_deleted",
|
|
|
|
"message_read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = v1.New(c.MGURL, c.MGToken)
|
|
|
|
data, status, err := client.ActivateTransportChannel(ch)
|
|
|
|
if status != http.StatusCreated {
|
2018-05-23 18:03:11 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
|
2018-05-17 18:35:53 +03:00
|
|
|
logger.Error(c.APIURL, status, err.Error(), data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Channel = data.ChannelID
|
|
|
|
b.Active = true
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
err = c.createBot(b)
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
2018-05-24 18:08:48 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
|
2018-05-24 17:16:21 +03:00
|
|
|
logger.Error(c.APIURL, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonString, err := json.Marshal(b)
|
|
|
|
if err != nil {
|
2018-05-24 18:08:48 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
|
2018-05-24 17:16:21 +03:00
|
|
|
logger.Error(c.APIURL, err.Error())
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2018-05-18 18:11:09 +03:00
|
|
|
w.Write(jsonString)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(err.Error())
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var b Bot
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &b)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(err.Error())
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
c := getConnectionById(b.ConnectionID)
|
2018-05-25 18:09:38 +03:00
|
|
|
if c.MGURL == "" || c.MGToken == "" {
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
|
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-05-17 18:35:53 +03:00
|
|
|
ch := v1.Channel{
|
|
|
|
ID: getBotChannelByToken(b.Token),
|
|
|
|
Type: "telegram",
|
|
|
|
Events: []string{
|
|
|
|
"message_sent",
|
|
|
|
"message_updated",
|
|
|
|
"message_deleted",
|
|
|
|
"message_read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = v1.New(c.MGURL, c.MGToken)
|
|
|
|
|
|
|
|
if b.Active {
|
|
|
|
data, status, err := client.DeactivateTransportChannel(ch.ID)
|
|
|
|
if status > http.StatusOK {
|
2018-05-23 18:03:11 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusBadRequest)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(b.ID, status, err.Error(), data)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data, status, err := client.ActivateTransportChannel(ch)
|
|
|
|
if status > http.StatusCreated {
|
2018-05-23 18:03:11 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(b.ID, status, err.Error(), data)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.setBotActivity()
|
|
|
|
if err != nil {
|
2018-05-25 18:09:38 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-28 18:16:13 +03:00
|
|
|
logger.Error(b.ID, err.Error())
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
|
2018-05-18 18:11:09 +03:00
|
|
|
setLocale(r.Header.Get("Accept-Language"))
|
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
p := getConnection(uid)
|
2018-05-17 18:35:53 +03:00
|
|
|
if p.ID == 0 {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2018-05-29 10:08:38 +03:00
|
|
|
return
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bots := Bots{}
|
2018-05-24 18:08:48 +03:00
|
|
|
err := bots.getBotsByClientID(uid)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
|
|
|
|
res := struct {
|
2018-05-18 18:11:09 +03:00
|
|
|
Conn *Connection
|
|
|
|
Bots Bots
|
|
|
|
Locale map[string]interface{}
|
2018-05-17 18:35:53 +03:00
|
|
|
}{
|
|
|
|
p,
|
|
|
|
bots,
|
2018-05-18 18:11:09 +03:00
|
|
|
map[string]interface{}{
|
2018-05-25 18:09:38 +03:00
|
|
|
"ButtonSave": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "button_save"}),
|
2018-05-22 10:34:39 +03:00
|
|
|
"ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}),
|
2018-05-18 18:11:09 +03:00
|
|
|
"TabSettings": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_settings"}),
|
|
|
|
"TabBots": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_bots"}),
|
|
|
|
"TableName": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_name"}),
|
|
|
|
"TableToken": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_token"}),
|
2018-05-23 17:03:37 +03:00
|
|
|
"AddBot": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "add_bot"}),
|
2018-05-18 18:11:09 +03:00
|
|
|
"TableActivity": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_activity"}),
|
2018-05-24 11:33:04 +03:00
|
|
|
"Title": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "title"}),
|
2018-05-18 18:11:09 +03:00
|
|
|
},
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(w, "form", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-25 15:56:31 +03:00
|
|
|
setLocale(r.Header.Get("Accept-Language"))
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Connection
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &c)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
err = validateCrmSettings(c)
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
logger.Error(c.APIURL, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
_, err, code := getAPIClient(c.APIURL, c.APIKEY)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
err = c.saveConnection()
|
|
|
|
if err != nil {
|
2018-05-24 18:08:48 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-24 17:16:21 +03:00
|
|
|
logger.Error(c.APIURL, err.Error())
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-05-23 18:03:11 +03:00
|
|
|
w.Write([]byte(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "successful"})))
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func createHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-18 18:11:09 +03:00
|
|
|
setLocale(r.Header.Get("Accept-Language"))
|
2018-05-22 10:34:39 +03:00
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-22 10:34:39 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Connection
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &c)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
2018-05-22 10:34:39 +03:00
|
|
|
return
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-05-22 10:34:39 +03:00
|
|
|
c.ClientID = GenerateToken()
|
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
err = validateCrmSettings(c)
|
2018-05-17 18:35:53 +03:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
logger.Error(c.APIURL, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
cl := getConnectionByURL(c.APIURL)
|
2018-05-17 18:35:53 +03:00
|
|
|
if cl.ID != 0 {
|
2018-05-18 18:11:09 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "connection_already_created"}), http.StatusBadRequest)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
client, err, code := getAPIClient(c.APIURL, c.APIKEY)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), code)
|
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-05-17 18:35:53 +03:00
|
|
|
ClientID: c.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,
|
|
|
|
c.ClientID,
|
|
|
|
),
|
|
|
|
Actions: map[string]string{"activity": "/actions/activity"},
|
|
|
|
Integrations: &v5.Integrations{
|
|
|
|
MgTransport: &v5.MgTransport{
|
|
|
|
WebhookUrl: fmt.Sprintf(
|
2018-05-22 10:34:39 +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-05-25 15:56:31 +03:00
|
|
|
raven.CaptureErrorAndWait(errr.RuntimeErr, nil)
|
2018-05-24 17:16:21 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_integration"}), http.StatusInternalServerError)
|
2018-05-17 18:35:53 +03:00
|
|
|
logger.Error(c.APIURL, status, errr.RuntimeErr, data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status >= http.StatusBadRequest {
|
|
|
|
http.Error(w, errr.ApiErr, http.StatusBadRequest)
|
|
|
|
logger.Error(c.APIURL, status, errr.ApiErr, data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.MGURL = data.Info["baseUrl"]
|
|
|
|
c.MGToken = data.Info["token"]
|
2018-05-23 18:03:11 +03:00
|
|
|
c.Active = true
|
2018-05-17 18:35:53 +03:00
|
|
|
|
|
|
|
err = c.createConnection()
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-24 18:08:48 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
|
2018-05-17 18:35:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:03:11 +03:00
|
|
|
res := struct {
|
|
|
|
Url string
|
|
|
|
Message string
|
|
|
|
}{
|
|
|
|
Url: "/settings/" + c.ClientID,
|
|
|
|
Message: localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "successful"}),
|
|
|
|
}
|
|
|
|
|
|
|
|
jss, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
2018-05-24 17:16:21 +03:00
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-25 18:09:38 +03:00
|
|
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
|
2018-05-24 17:16:21 +03:00
|
|
|
return
|
2018-05-23 18:03:11 +03:00
|
|
|
}
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
w.WriteHeader(http.StatusFound)
|
2018-05-23 18:03:11 +03:00
|
|
|
w.Write(jss)
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func activityHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
res := Response{Success: false}
|
|
|
|
|
|
|
|
if r.Method != http.MethodPost {
|
2018-05-18 18:11:09 +03:00
|
|
|
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "set_method"})
|
2018-05-24 17:16:21 +03:00
|
|
|
jsonString, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
w.Write(jsonString)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-18 18:11:09 +03:00
|
|
|
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"})
|
2018-05-24 17:16:21 +03:00
|
|
|
jsonString, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
w.Write(jsonString)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rec Connection
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &rec)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-18 18:11:09 +03:00
|
|
|
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"})
|
2018-05-24 17:16:21 +03:00
|
|
|
jsonString, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
w.Write(jsonString)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rec.setConnectionActivity(); err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
2018-05-18 18:11:09 +03:00
|
|
|
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"})
|
2018-05-24 17:16:21 +03:00
|
|
|
jsonString, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
w.Write(jsonString)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Success = true
|
2018-05-24 17:16:21 +03:00
|
|
|
jsonString, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
w.Write(jsonString)
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:09:38 +03:00
|
|
|
func validateCrmSettings(c Connection) error {
|
2018-05-17 18:35:53 +03:00
|
|
|
if c.APIURL == "" || c.APIKEY == "" {
|
2018-05-18 18:11:09 +03:00
|
|
|
return errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "missing_url_key"}))
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if res, _ := regexp.MatchString(`https://?[\da-z\.-]+\.(retailcrm\.(ru|pro)|ecomlogic\.com)`, c.APIURL); !res {
|
2018-05-18 18:11:09 +03:00
|
|
|
return errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url"}))
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-28 18:16:13 +03:00
|
|
|
|
2018-05-28 18:08:15 +03:00
|
|
|
func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bytes, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg EventMessage
|
|
|
|
err = json.Unmarshal(bytes, &msg)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b := getBotByChannel(msg.ChannelID)
|
|
|
|
if b.ID == 0 {
|
|
|
|
logger.Error(msg.ChannelID, "missing")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.Active {
|
|
|
|
logger.Error(msg.ChannelID, "deactivated")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bot, err := tgbotapi.NewBotAPI(b.Token)
|
|
|
|
if err != nil {
|
|
|
|
raven.CaptureErrorAndWait(err, nil)
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.Debug = true
|
|
|
|
|
|
|
|
if msg.Type == "message_sent" {
|
|
|
|
msg, err := bot.Send(tgbotapi.NewMessage(msg.ChatID, msg.Message))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("%v", msg)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("Message sent"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type == "message_updated" {
|
|
|
|
msg, err := bot.Send(tgbotapi.NewEditMessageText(msg.ChatID, msg.ExternalID, msg.Message))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("%v", msg)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("Message updated"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type == "message_deleted" {
|
|
|
|
msg, err := bot.Send(tgbotapi.NewDeleteMessage(msg.ChatID, msg.ExternalID))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("%v", msg)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("Message deleted"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
func getAPIClient(url, key string) (*v5.Client, error, int) {
|
|
|
|
client := v5.New(url, key)
|
|
|
|
|
|
|
|
cr, status, errr := client.APICredentials()
|
|
|
|
if errr.RuntimeErr != nil {
|
|
|
|
raven.CaptureErrorAndWait(errr.RuntimeErr, nil)
|
|
|
|
logger.Error(url, status, errr.RuntimeErr, cr)
|
|
|
|
return nil, errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"})), http.StatusInternalServerError
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cr.Success {
|
|
|
|
logger.Error(url, status, errr.ApiErr, cr)
|
|
|
|
return nil, errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url_key"})), http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := checkCredentials(cr.Credentials); len(res) != 0 {
|
|
|
|
logger.Error(url, status, res)
|
|
|
|
return nil,
|
|
|
|
errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "missing_credentials",
|
|
|
|
TemplateData: map[string]interface{}{
|
|
|
|
"Credentials": strings.Join(res, ", "),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCredentials(credential []string) []string {
|
|
|
|
rc := []string{
|
|
|
|
"/api/integration-modules/{code}",
|
|
|
|
"/api/integration-modules/{code}/edit",
|
|
|
|
}
|
|
|
|
|
|
|
|
for kn, vn := range rc {
|
|
|
|
for _, vc := range credential {
|
|
|
|
if vn == vc {
|
|
|
|
if len(rc) == 1 {
|
|
|
|
rc = rc[:0]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rc = append(rc[:kn], rc[kn+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc
|
|
|
|
}
|