diff --git a/config.go b/config.go index 8190ae0..a7de236 100644 --- a/config.go +++ b/config.go @@ -14,6 +14,7 @@ type TransportConfig struct { Database DatabaseConfig `yaml:"database"` SentryDSN string `yaml:"sentry_dsn"` HTTPServer HTTPServerConfig `yaml:"http_server"` + Debug bool `yaml:"debug"` } // DatabaseConfig struct diff --git a/config.yml.dist b/config.yml.dist index 605ba80..357105b 100644 --- a/config.yml.dist +++ b/config.yml.dist @@ -8,3 +8,5 @@ http_server: sentry_dsn: ~ log_level: 5 + +debug: false diff --git a/config_test.yml.dist b/config_test.yml.dist index 1132bce..090e513 100644 --- a/config_test.yml.dist +++ b/config_test.yml.dist @@ -8,3 +8,5 @@ http_server: sentry_dsn: ~ log_level: 5 + +debug: false diff --git a/routing.go b/routing.go index 5db0f45..435ed05 100644 --- a/routing.go +++ b/routing.go @@ -92,7 +92,13 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl func connectHandler(w http.ResponseWriter, r *http.Request) { setLocale(r.Header.Get("Accept-Language")) - p := Connection{} + + account := r.URL.Query() + rx := regexp.MustCompile(`/+$`) + ra := rx.ReplaceAllString(account.Get("account"), ``) + p := Connection{ + APIURL: ra, + } res := struct { Conn *Connection @@ -153,7 +159,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { return } - bot.Debug = false + bot.Debug = config.Debug wr, err := bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token)) if err != nil { @@ -439,7 +445,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) { } if status >= http.StatusBadRequest { - http.Error(w, errr.ApiErr, http.StatusBadRequest) + http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activity_mg"}), http.StatusBadRequest) logger.Error(c.APIURL, status, errr.ApiErr, data) return } diff --git a/telegram.go b/telegram.go index 03f5593..c0b6d6c 100644 --- a/telegram.go +++ b/telegram.go @@ -96,7 +96,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string Lastname: update.Message.From.LastName, Language: update.Message.From.LanguageCode, }, - Channel: c.Bots[0].Channel, + Channel: b.Channel, } data, st, err := client.Messages(snd) @@ -106,6 +106,10 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string w.WriteHeader(http.StatusInternalServerError) return } + + if config.Debug { + logger.Debugf("Bot: %v, Message: %v, Response: %v", b.ID, snd, data) + } } if update.EditedMessage != nil { @@ -117,7 +121,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string Text: update.EditedMessage.Text, }, }, - Channel: c.Bots[0].Channel, + Channel: b.Channel, } data, st, err := client.UpdateMessages(snd) @@ -127,6 +131,10 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string w.WriteHeader(http.StatusInternalServerError) return } + + if config.Debug { + logger.Debugf("Bot: %v, Message: %v, Response: %v", b.ID, snd, data) + } } w.WriteHeader(http.StatusOK) @@ -177,7 +185,10 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { return } - logger.Debugf("%v", msg) + if config.Debug { + logger.Debugf("%v", msg) + } + w.WriteHeader(http.StatusOK) w.Write([]byte("Message sent")) } @@ -191,7 +202,10 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { return } - logger.Debugf("%v", msg) + if config.Debug { + logger.Debugf("%v", msg) + } + w.WriteHeader(http.StatusOK) w.Write([]byte("Message updated")) } @@ -205,7 +219,10 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { return } - logger.Debugf("%v", msg) + if config.Debug { + logger.Debugf("%v", msg) + } + w.WriteHeader(http.StatusOK) w.Write([]byte("Message deleted")) } diff --git a/templates/form.html b/templates/form.html index 2e235c1..058b6a3 100644 --- a/templates/form.html +++ b/templates/form.html @@ -12,7 +12,7 @@
- +
diff --git a/templates/home.html b/templates/home.html index 0de3b89..ac7b2d5 100644 --- a/templates/home.html +++ b/templates/home.html @@ -4,7 +4,8 @@
- +
diff --git a/translate/translate.en.yml b/translate/translate.en.yml index b135680..a1d1b5a 100644 --- a/translate/translate.en.yml +++ b/translate/translate.en.yml @@ -27,3 +27,4 @@ error_creating_webhook: Error while creating webhook error_adding_bot: Error while adding bot error_save: An error occurred while saving, contact technical support missing_credentials: "Necessary credentials: {{.Credentials}}" +error_activity_mg: Check the activity with MessageGateway in CRM settings \ No newline at end of file diff --git a/translate/translate.ru.yml b/translate/translate.ru.yml index 2e20293..4aa4b70 100644 --- a/translate/translate.ru.yml +++ b/translate/translate.ru.yml @@ -27,3 +27,4 @@ error_creating_webhook: Ошибка при создании webhook error_adding_bot: Ошибка при добавлении бота error_save: Ошибка при сохранении, обратитесь в службу технической поддержки missing_credentials: "Необходимые методы: {{.Credentials}}" +error_activity_mg: Проверьте активность интеграции с MessageGateway в настройках CRM