add telegram webhook, improve template, minor fixes
This commit is contained in:
parent
071de2eeb1
commit
74b5205768
14
config.go
14
config.go
@ -10,10 +10,11 @@ import (
|
|||||||
|
|
||||||
// TransportConfig struct
|
// TransportConfig struct
|
||||||
type TransportConfig struct {
|
type TransportConfig struct {
|
||||||
LogLevel logging.Level `yaml:"log_level"`
|
LogLevel logging.Level `yaml:"log_level"`
|
||||||
Database DatabaseConfig `yaml:"database"`
|
Database DatabaseConfig `yaml:"database"`
|
||||||
SentryDSN string `yaml:"sentry_dsn"`
|
SentryDSN string `yaml:"sentry_dsn"`
|
||||||
HTTPServer HTTPServerConfig `yaml:"http_server"`
|
HTTPServer HTTPServerConfig `yaml:"http_server"`
|
||||||
|
TelegramConfig TelegramConfig `yaml:"telegram"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseConfig struct
|
// DatabaseConfig struct
|
||||||
@ -32,6 +33,11 @@ type HTTPServerConfig struct {
|
|||||||
Listen string `yaml:"listen"`
|
Listen string `yaml:"listen"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TelegramConfig struct
|
||||||
|
type TelegramConfig struct {
|
||||||
|
Debug bool `yaml:"debug"`
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig read configuration file
|
// LoadConfig read configuration file
|
||||||
func LoadConfig(path string) *TransportConfig {
|
func LoadConfig(path string) *TransportConfig {
|
||||||
var err error
|
var err error
|
||||||
|
179
routing.go
179
routing.go
@ -8,8 +8,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/getsentry/raven-go"
|
"github.com/getsentry/raven-go"
|
||||||
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||||
"github.com/retailcrm/api-client-go/v5"
|
"github.com/retailcrm/api-client-go/v5"
|
||||||
"github.com/retailcrm/mg-transport-api-client-go/v1"
|
"github.com/retailcrm/mg-transport-api-client-go/v1"
|
||||||
@ -19,7 +22,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
templates = template.Must(template.ParseFiles("templates/layout.html", "templates/form.html", "templates/home.html"))
|
templates = template.Must(template.ParseFiles("templates/layout.html", "templates/form.html", "templates/home.html"))
|
||||||
validPath = regexp.MustCompile("^/(save|settings)/([a-zA-Z0-9]+)$")
|
validPath = regexp.MustCompile(`^/(save|settings|telegram)/([a-zA-Z0-9-:_+]+)$`)
|
||||||
localizer *i18n.Localizer
|
localizer *i18n.Localizer
|
||||||
bundle = &i18n.Bundle{DefaultLanguage: language.English}
|
bundle = &i18n.Bundle{DefaultLanguage: language.English}
|
||||||
matcher = language.NewMatcher([]language.Tag{
|
matcher = language.NewMatcher([]language.Tag{
|
||||||
@ -62,10 +65,16 @@ func setWrapperRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) {
|
func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) {
|
||||||
err := templates.ExecuteTemplate(w, tmpl+".html", c)
|
tm, err := template.ParseFiles("templates/layout.html", "templates/"+tmpl+".html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tm.Execute(w, &c)
|
||||||
|
if err != nil {
|
||||||
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +110,7 @@ func connectHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,25 +118,41 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err = json.Unmarshal(body, &b)
|
err = json.Unmarshal(body, &b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Token == "" {
|
if b.Token == "" {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "no_bot_token"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "no_bot_token"}), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cl, _ := getBotByToken(b.Token)
|
cl, _ := getBotByToken(b.Token)
|
||||||
if cl.ID != 0 {
|
if cl.ID != 0 {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bot, err := GetBotInfo(b.Token)
|
bot, err := GetBotInfo(b.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(b.Token, err.Error())
|
logger.Error(b.Token, err.Error())
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_token"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_token"}), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bot.Debug = false
|
||||||
|
|
||||||
|
_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token))
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(b.Token, err.Error())
|
||||||
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = bot.GetWebhookInfo()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(b.Token, err.Error())
|
||||||
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,14 +160,14 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
c, err := getConnection(b.ClientID)
|
c, err := getConnection(b.ClientID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
|
||||||
logger.Error(b.ClientID, err.Error())
|
logger.Error(b.ClientID, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.MGURL == "" || c.MGToken == "" {
|
if c.MGURL == "" || c.MGToken == "" {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
|
||||||
logger.Error(b.ClientID)
|
logger.Error(b.ClientID, "MGURL or MGToken is empty")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +184,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
var client = v1.New(c.MGURL, c.MGToken)
|
var client = v1.New(c.MGURL, c.MGToken)
|
||||||
data, status, err := client.ActivateTransportChannel(ch)
|
data, status, err := client.ActivateTransportChannel(ch)
|
||||||
if status != http.StatusCreated {
|
if status != http.StatusCreated {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
|
||||||
logger.Error(c.APIURL, status, err.Error(), data)
|
logger.Error(c.APIURL, status, err.Error(), data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -170,7 +195,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
err = b.createBot()
|
err = b.createBot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,7 +207,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +215,7 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err = json.Unmarshal(body, &b)
|
err = json.Unmarshal(body, &b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,14 +232,14 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
c, err := getConnection(b.ClientID)
|
c, err := getConnection(b.ClientID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
|
||||||
logger.Error(b.ClientID, err.Error())
|
logger.Error(b.ClientID, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.MGURL == "" || c.MGToken == "" {
|
if c.MGURL == "" || c.MGToken == "" {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
|
||||||
logger.Error(b.ClientID, "not find account")
|
logger.Error(b.ClientID, "MGURL or MGToken is empty")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,14 +248,14 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if b.Active {
|
if b.Active {
|
||||||
data, status, err := client.DeactivateTransportChannel(ch.ID)
|
data, status, err := client.DeactivateTransportChannel(ch.ID)
|
||||||
if status > http.StatusOK {
|
if status > http.StatusOK {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusBadRequest)
|
||||||
logger.Error(b.ClientID, status, err.Error(), data)
|
logger.Error(b.ClientID, status, err.Error(), data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
data, status, err := client.ActivateTransportChannel(ch)
|
data, status, err := client.ActivateTransportChannel(ch)
|
||||||
if status > http.StatusCreated {
|
if status > http.StatusCreated {
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
|
||||||
logger.Error(b.ClientID, status, err.Error(), data)
|
logger.Error(b.ClientID, status, err.Error(), data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -239,7 +264,7 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
err = b.setBotActivity()
|
err = b.setBotActivity()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +277,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
|
|||||||
p, err := getConnection(uid)
|
p, err := getConnection(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,7 +319,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
|
|||||||
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,7 +327,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err = json.Unmarshal(body, &c)
|
err = json.Unmarshal(body, &c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,12 +341,12 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
err = c.saveConnection()
|
err = c.saveConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("/settings/" + r.FormValue("clientId")))
|
w.Write([]byte(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "successful"})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func createHandler(w http.ResponseWriter, r *http.Request) {
|
func createHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -329,7 +354,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,7 +362,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err = json.Unmarshal(body, &c)
|
err = json.Unmarshal(body, &c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -416,16 +441,30 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
c.MGURL = data.Info["baseUrl"]
|
c.MGURL = data.Info["baseUrl"]
|
||||||
c.MGToken = data.Info["token"]
|
c.MGToken = data.Info["token"]
|
||||||
|
c.Active = true
|
||||||
|
|
||||||
err = c.createConnection()
|
err = c.createConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
logger.Error(c.APIURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusFound)
|
w.WriteHeader(http.StatusFound)
|
||||||
w.Write([]byte("/settings/" + c.ClientID))
|
w.Write(jss)
|
||||||
}
|
}
|
||||||
|
|
||||||
func activityHandler(w http.ResponseWriter, r *http.Request) {
|
func activityHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -483,3 +522,83 @@ func validate(c Connection) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) {
|
||||||
|
t := time.Now()
|
||||||
|
b, err := getBotByToken(token)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(token, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !b.Active {
|
||||||
|
logger.Error(token, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bot, err := GetBotInfo(token)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(token, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bot.Debug = false
|
||||||
|
|
||||||
|
bytes, _ := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
|
var update tgbotapi.Update
|
||||||
|
json.Unmarshal(bytes, &update)
|
||||||
|
|
||||||
|
c, err := getConnection(b.ClientID)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(token, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.MGURL == "" || c.MGToken == "" {
|
||||||
|
logger.Error(token, "MGURL or MGToken is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var client = v1.New(c.MGURL, c.MGToken)
|
||||||
|
|
||||||
|
if update.Message != nil {
|
||||||
|
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: update.Message.From.UserName,
|
||||||
|
Firstname: update.Message.From.FirstName,
|
||||||
|
},
|
||||||
|
Channel: b.Channel,
|
||||||
|
}
|
||||||
|
|
||||||
|
data, status, err := client.Messages(snd)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(token, err.Error(), status, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if update.EditedMessage != nil {
|
||||||
|
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, status, err := client.UpdateMessages(snd)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(token, err.Error(), status, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("Time working: ", time.Since(t))
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
func setTransportRoutes() {
|
func setTransportRoutes() {
|
||||||
http.HandleFunc("/add-bot/", addBotHandler)
|
http.HandleFunc("/add-bot/", addBotHandler)
|
||||||
http.HandleFunc("/activity-bot/", activityBotHandler)
|
http.HandleFunc("/activity-bot/", activityBotHandler)
|
||||||
|
http.HandleFunc("/telegram/", makeHandler(telegramWebhookHandler))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBotInfo function
|
// GetBotInfo function
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
{{template "header"}}
|
{{define "body"}}
|
||||||
<div class="row indent-top">
|
<div class="row indent-top">
|
||||||
<div class="col s6 offset-s3">
|
<div class="col s12">
|
||||||
<ul class="tabs" id="tab">
|
<ul class="tabs" id="tab">
|
||||||
<li class="tab col s6"><a class="active" href="#tab1">{{.Locale.TabSettings}}</a></li>
|
<li class="tab col s6"><a class="active" href="#tab1">{{.Locale.TabSettings}}</a></li>
|
||||||
<li class="tab col s6"><a class="" href="#tab2">{{.Locale.TabBots}}</a></li>
|
<li class="tab col s6"><a class="" href="#tab2">{{.Locale.TabBots}}</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="col s6 offset-s3">
|
|
||||||
<div id="msg" class="indent-top"></div>
|
|
||||||
</div>
|
|
||||||
<div id="tab1" class="col s12">
|
<div id="tab1" class="col s12">
|
||||||
<div class="row indent-top">
|
<div class="row indent-top">
|
||||||
<form id="save" class="col s8 offset-s2" action="/save/" method="POST">
|
<form id="save" class="tab-el-center" action="/save/" method="POST">
|
||||||
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}">
|
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="input-field col s12">
|
<div class="input-field col s12">
|
||||||
@ -36,7 +33,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div id="tab2" class="col s12">
|
<div id="tab2" class="col s12">
|
||||||
<div class="row indent-top">
|
<div class="row indent-top">
|
||||||
<form id="add-bot" class="col s8 offset-s2" action="/add-bot/" method="POST">
|
<form id="add-bot" class="tab-el-center" action="/add-bot/" method="POST">
|
||||||
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}">
|
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="input-field col s12">
|
<div class="input-field col s12">
|
||||||
@ -51,7 +48,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<table id="bots" class="col s8 offset-s2">
|
<table id="bots" class="tab-el-center">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{.Locale.TableName}}</th>
|
<th>{{.Locale.TableName}}</th>
|
||||||
@ -77,4 +74,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{template "footer"}}
|
{{end}}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
{{template "header"}}
|
{{define "body"}}
|
||||||
<div class="row indent-top">
|
<div class="row indent-top home">
|
||||||
<div class="col s6 offset-s3">
|
<form class="tab-el-center" method="POST" id="save-crm" action="/create/">
|
||||||
<div id="msg" class="indent-top"></div>
|
|
||||||
</div>
|
|
||||||
<form class="col s8 offset-s2" method="POST" id="save-crm" action="/create/">
|
|
||||||
<div id="msg"></div>
|
<div id="msg"></div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="input-field col s12">
|
<div class="input-field col s12">
|
||||||
@ -25,4 +22,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{{template "footer"}}
|
{{end}}
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
{{ define "header" }}
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta title="Telegram transport">
|
<meta title="Telegram transport">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
|
<title>{{.Locale.Title}}</title>
|
||||||
<link rel="stylesheet" href="../web/materialize.min.css">
|
<link rel="stylesheet" href="../web/materialize.min.css">
|
||||||
<link rel="stylesheet" href="../web/font.css" >
|
<link rel="stylesheet" href="../web/font.css" >
|
||||||
<link rel="stylesheet" href="../web/style.css" >
|
<link rel="stylesheet" href="../web/style.css" >
|
||||||
@ -13,13 +12,10 @@
|
|||||||
<div class="indent-top center-align">
|
<div class="indent-top center-align">
|
||||||
<img id="logo" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmlld0JveD0iMCAwIDI0MCAyNDAiPgo8ZGVmcz4KCTxsaW5lYXJHcmFkaWVudCBpZD0iYiIgeDE9IjAuNjY2NyIgeTE9IjAuMTY2NyIgeDI9IjAuNDE2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DAuMDQgMTQ0LjQxIDQ4LjM2IDM1LjcyOWM1LjUxODUgMy4wNDQ5IDkuNTAxNCAxLjQ2ODQgMTAuODc2LTUuMTIzNWwxOS42ODUtOTIuNzYzYzIuMDE1NC04LjA4MDItMy4wODAxLTExLjc0NS04LjM1OTQtOS4zNDgybC0xMTUuNTkgNDQuNTcxYy03Ljg5MDEgMy4xNjQ3LTcuODQ0MSA3LjU2NjYtMS40MzgyIDkuNTI4bDI5LjY2MyA5LjI1ODMgNjguNjczLTQzLjMyNWMzLjI0MTktMS45NjU5IDYuMjE3My0wLjkwODk5IDMuNzc1MiAxLjI1ODQiLz4KPC9zdmc+" alt="telegram" >
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{template "body" .}}
|
||||||
|
|
||||||
{{ define "footer" }}
|
|
||||||
</div>
|
</div>
|
||||||
<script src="../web/materialize.min.js"></script>
|
<script src="../web/materialize.min.js"></script>
|
||||||
<script src="../web/jquery-3.3.1.min.js"></script>
|
<script src="../web/jquery-3.3.1.min.js"></script>
|
||||||
<script src="../web/script.js"></script>
|
<script src="../web/script.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
{{ end }}
|
|
||||||
|
@ -5,12 +5,15 @@ table_name: Bot name
|
|||||||
table_token: Bot token
|
table_token: Bot token
|
||||||
table_activity: Activity
|
table_activity: Activity
|
||||||
api_key: API Key
|
api_key: API Key
|
||||||
|
add_bot: Add bot
|
||||||
|
title: Telegram transport for retailCRM
|
||||||
|
successful: Data successfully updated
|
||||||
|
|
||||||
no_bot_token: Enter the bot token
|
no_bot_token: Enter the bot token
|
||||||
wrong_data: Incorrect data
|
wrong_data: Incorrect data
|
||||||
set_method: Set POST method
|
set_method: Set POST method
|
||||||
bot_already_created: Bot already created
|
bot_already_created: Bot already created
|
||||||
not_find_account: Could not find account, please contact technical support
|
not_found_account: The account could not be found, contact technical support
|
||||||
error_activating_channel: Error while activating the channel
|
error_activating_channel: Error while activating the channel
|
||||||
error_deactivating_channel: Error while deactivating the channel
|
error_deactivating_channel: Error while deactivating the channel
|
||||||
incorrect_url_key: Enter the correct CRM url or apiKey
|
incorrect_url_key: Enter the correct CRM url or apiKey
|
||||||
@ -20,5 +23,4 @@ connection_already_created: Connection already created
|
|||||||
missing_url_key: Missing crm url or apiKey
|
missing_url_key: Missing crm url or apiKey
|
||||||
incorrect_url: Enter the correct CRM url
|
incorrect_url: Enter the correct CRM url
|
||||||
incorrect_token: Set correct bot token
|
incorrect_token: Set correct bot token
|
||||||
add_bot: Add bot
|
error_creating_webhook: Error while creating webhook
|
||||||
title: Telegram transport for retailCRM
|
|
||||||
|
@ -5,12 +5,15 @@ table_name: Имя
|
|||||||
table_token: Токен
|
table_token: Токен
|
||||||
table_activity: Активность
|
table_activity: Активность
|
||||||
api_key: API Ключ
|
api_key: API Ключ
|
||||||
|
add_bot: Добавить бота
|
||||||
|
title: Модуль подключения Telegram к retailCRM
|
||||||
|
successful: Данные успешно обновлены
|
||||||
|
|
||||||
no_bot_token: Введите токен
|
no_bot_token: Введите токен
|
||||||
wrong_data: Неверные данные
|
wrong_data: Неверные данные
|
||||||
set_method: Установить метод POST
|
set_method: Установить метод POST
|
||||||
bot_already_created: Бот уже создан
|
bot_already_created: Бот уже создан
|
||||||
not_find_account: Не удалось найти учетную запись, обратитесь в службу технической поддержки
|
not_found_account: Не удалось найти учетную запись, обратитесь в службу технической поддержки
|
||||||
error_activating_channel: Ошибка при активации канала
|
error_activating_channel: Ошибка при активации канала
|
||||||
error_deactivating_channel: Ошибка при отключении канала
|
error_deactivating_channel: Ошибка при отключении канала
|
||||||
incorrect_url_key: Введите корректный URL или apiKey
|
incorrect_url_key: Введите корректный URL или apiKey
|
||||||
@ -20,5 +23,4 @@ connection_already_created: Соединение уже создано
|
|||||||
missing_url_key: Отсутствует URL или apiKey
|
missing_url_key: Отсутствует URL или apiKey
|
||||||
incorrect_url: Введите корректный URL CRM
|
incorrect_url: Введите корректный URL CRM
|
||||||
incorrect_token: Установите корректный токен
|
incorrect_token: Установите корректный токен
|
||||||
add_bot: Добавить бота
|
error_creating_webhook: Ошибка при создании webhook
|
||||||
title: Модуль подключения Telegram к retailCRM
|
|
||||||
|
@ -14,8 +14,8 @@ $("#save").on("submit", function(e) {
|
|||||||
send(
|
send(
|
||||||
$(this).attr('action'),
|
$(this).attr('action'),
|
||||||
formDataToObj($(this).serializeArray()),
|
formDataToObj($(this).serializeArray()),
|
||||||
function () {
|
function (data) {
|
||||||
return 0;
|
M.toast({html: data});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
@ -31,6 +31,7 @@ $("#add-bot").on("submit", function(e) {
|
|||||||
bots.removeClass("hide")
|
bots.removeClass("hide")
|
||||||
}
|
}
|
||||||
$("#bots tbody").append(getBotTemplate(data));
|
$("#bots tbody").append(getBotTemplate(data));
|
||||||
|
$("#token").val("");
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
@ -56,7 +57,6 @@ $(document).on("click", ".activity-bot", function(e) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function send(url, data, callback) {
|
function send(url, data, callback) {
|
||||||
$('#msg').empty();
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: url,
|
url: url,
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
@ -65,12 +65,14 @@ function send(url, data, callback) {
|
|||||||
error: function (res){
|
error: function (res){
|
||||||
if (res.status < 400) {
|
if (res.status < 400) {
|
||||||
if (res.responseText) {
|
if (res.responseText) {
|
||||||
|
let resObj = JSON.parse(res.responseText);
|
||||||
|
localStorage.setItem("createdMsg", resObj.Message);
|
||||||
|
|
||||||
document.location.replace(
|
document.location.replace(
|
||||||
location.protocol.concat("//").concat(window.location.host) + res.responseText
|
location.protocol.concat("//").concat(window.location.host) + resObj.Url
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//$('#msg').html(`<p class="err-msg truncate">${res.responseText}</p>`);
|
|
||||||
M.toast({html: res.responseText})
|
M.toast({html: res.responseText})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,4 +108,12 @@ $( document ).ready(function() {
|
|||||||
if ($("table tbody").children().length === 0) {
|
if ($("table tbody").children().length === 0) {
|
||||||
$("#bots").addClass("hide");
|
$("#bots").addClass("hide");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let createdMsg = localStorage.getItem("createdMsg");
|
||||||
|
if (createdMsg) {
|
||||||
|
setTimeout(function() {
|
||||||
|
M.toast({html: createdMsg});
|
||||||
|
localStorage.removeItem("createdMsg");
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,16 @@
|
|||||||
text-align: right;
|
text-align: right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#tab{
|
||||||
|
width: 50%;
|
||||||
|
margin: 0 auto 23px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-el-center{
|
||||||
|
width: 67%;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
#bots .activity-bot{
|
#bots .activity-bot{
|
||||||
float: right;
|
float: right;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user