1
0
mirror of synced 2024-11-22 20:36:01 +03:00

minor fixes

This commit is contained in:
DmitryZagorulko 2018-05-24 18:08:48 +03:00
parent adff7e14b4
commit 91e1bc6a36
4 changed files with 41 additions and 66 deletions

View File

@ -1,17 +1,17 @@
package main package main
func getConnection(uid string) (*Connection, error) { func getConnection(uid string) *Connection {
var connection Connection var connection Connection
orm.DB.First(&connection, "client_id = ?", uid) orm.DB.First(&connection, "client_id = ?", uid)
return &connection, nil return &connection
} }
func getConnectionByURL(urlCrm string) (*Connection, error) { func getConnectionByURL(urlCrm string) *Connection {
var connection Connection var connection Connection
orm.DB.First(&connection, "api_url = ?", urlCrm) orm.DB.First(&connection, "api_url = ?", urlCrm)
return &connection, nil return &connection
} }
func (c *Connection) setConnectionActivity() error { func (c *Connection) setConnectionActivity() error {
@ -26,11 +26,11 @@ func (c *Connection) saveConnection() error {
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error
} }
func getBotByToken(token string) (*Bot, error) { func getBotByToken(token string) *Bot {
var bot Bot var bot Bot
orm.DB.First(&bot, "token = ?", token) orm.DB.First(&bot, "token = ?", token)
return &bot, nil return &bot
} }
func (b *Bot) createBot() error { func (b *Bot) createBot() error {

View File

@ -112,7 +112,7 @@ 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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
return return
} }
@ -121,7 +121,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &b) err = json.Unmarshal(body, &b)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
return return
} }
@ -130,12 +130,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
cl, err := getBotByToken(b.Token) cl := getBotByToken(b.Token)
if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "no_bot_token"}), http.StatusBadRequest)
return
}
if cl.ID != 0 { if cl.ID != 0 {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest)
return return
@ -166,13 +161,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
b.Name = GetBotName(bot) b.Name = GetBotName(bot)
c, err := getConnection(b.ClientID) c := getConnection(b.ClientID)
if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ClientID, err.Error())
return
}
if c.MGURL == "" || c.MGToken == "" { if c.MGURL == "" || c.MGToken == "" {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ClientID, "MGURL or MGToken is empty") logger.Error(b.ClientID, "MGURL or MGToken is empty")
@ -202,14 +191,16 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
err = b.createBot() err = b.createBot()
if err != nil { if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_bot"}), http.StatusBadRequest) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
return return
} }
jsonString, err := json.Marshal(b) jsonString, err := json.Marshal(b)
if err != nil { if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_bot"}), http.StatusBadRequest) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
return return
} }
@ -222,7 +213,7 @@ 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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -231,7 +222,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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -246,13 +237,7 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
}, },
} }
c, err := getConnection(b.ClientID) c := getConnection(b.ClientID)
if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ClientID, err.Error())
return
}
if c.MGURL == "" || c.MGToken == "" { if c.MGURL == "" || c.MGToken == "" {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ClientID, "MGURL or MGToken is empty") logger.Error(b.ClientID, "MGURL or MGToken is empty")
@ -280,7 +265,7 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
err = b.setBotActivity() err = b.setBotActivity()
if err != nil { if err != nil {
logger.Error(b.ClientID, err.Error()) logger.Error(b.ClientID, err.Error())
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -290,19 +275,18 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) { func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
setLocale(r.Header.Get("Accept-Language")) setLocale(r.Header.Get("Accept-Language"))
p, err := getConnection(uid) p := getConnection(uid)
if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(p.ClientID, err.Error())
return
}
if p.ID == 0 { if p.ID == 0 {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
bots := Bots{} bots := Bots{}
bots.getBotsByClientID(uid) err := bots.getBotsByClientID(uid)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return
}
res := struct { res := struct {
Conn *Connection Conn *Connection
@ -331,7 +315,7 @@ 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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -340,7 +324,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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -353,7 +337,8 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
err = c.saveConnection() err = c.saveConnection()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
return return
} }
@ -368,7 +353,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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -377,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 {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -390,13 +375,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
cl, err := getConnectionByURL(c.APIURL) cl := getConnectionByURL(c.APIURL)
if err != nil {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(cl.ClientID, err.Error())
return
}
if cl.ID != 0 { if cl.ID != 0 {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "connection_already_created"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "connection_already_created"}), http.StatusBadRequest)
return return
@ -469,7 +448,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
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.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
return return
} }
@ -575,9 +554,9 @@ func validate(c Connection) error {
} }
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) { func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) {
b, err := getBotByToken(token) b := getBotByToken(token)
if err != nil { if b.ID == 0 {
logger.Error(token, err) logger.Error(token, "missing")
return return
} }
@ -589,7 +568,6 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
bot, err := GetBotInfo(token) bot, err := GetBotInfo(token)
if err != nil { if err != nil {
logger.Error(token, err) logger.Error(token, err)
return
} }
bot.Debug = false bot.Debug = false
@ -608,12 +586,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
return return
} }
c, err := getConnection(b.ClientID) c := getConnection(b.ClientID)
if err != nil {
logger.Error(token, err.Error())
return
}
if c.MGURL == "" || c.MGToken == "" { if c.MGURL == "" || c.MGToken == "" {
logger.Error(token, "MGURL or MGToken is empty") logger.Error(token, "MGURL or MGToken is empty")
return return

View File

@ -24,4 +24,5 @@ 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
error_creating_webhook: Error while creating webhook error_creating_webhook: Error while creating webhook
error_creating_bot: Error while creating bot error_adding_bot: Error while adding bot
error_save: An error occurred while saving, contact technical support

View File

@ -24,4 +24,5 @@ missing_url_key: Отсутствует URL или apiKey
incorrect_url: Введите корректный URL CRM incorrect_url: Введите корректный URL CRM
incorrect_token: Установите корректный токен incorrect_token: Установите корректный токен
error_creating_webhook: Ошибка при создании webhook error_creating_webhook: Ошибка при создании webhook
error_creating_bot: Ошибка при создании бота error_adding_bot: Ошибка при добавлении бота
error_save: Ошибка при сохранении, обратитесь в службу технической поддержки