From 1516cc05fad010e010b02ce6f39f1c1c14eb83d5 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 31 May 2018 19:44:23 +0300 Subject: [PATCH] fix activity callback --- repository.go | 14 ++++++-------- routing.go | 30 +++++++++++++++++++++++------- telegram.go | 13 ++++++++++--- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/repository.go b/repository.go index 1492e24..2d426c3 100644 --- a/repository.go +++ b/repository.go @@ -32,18 +32,16 @@ func (c *Connection) createBot(b Bot) error { return orm.DB.Model(c).Association("Bots").Append(&b).Error } -func getConnectionByBotToken(token string) (*Connection, error) { - var c Connection - err := orm.DB.Where("active = ?", true). - Preload("Bots", "token = ?", token). - First(&c).Error +func getBotByToken(token string) (*Bot, error) { + var bot Bot + err := orm.DB.First(&bot, "token = ?", token).Error if gorm.IsRecordNotFoundError(err) { - return &c, nil + return &bot, nil } else { - return &c, err + return &bot, err } - return &c, nil + return &bot, nil } func getBotByChannel(ch uint64) *Bot { diff --git a/routing.go b/routing.go index 6802706..5db0f45 100644 --- a/routing.go +++ b/routing.go @@ -133,7 +133,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { return } - cb, err := getConnectionByBotToken(b.Token) + cl, err := getBotByToken(b.Token) if err != nil { raven.CaptureErrorAndWait(err, nil) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) @@ -141,7 +141,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { return } - if len(cb.Bots) != 0 { + if cl.ID != 0 { http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest) return } @@ -214,6 +214,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { } func activityBotHandler(w http.ResponseWriter, r *http.Request) { + setLocale(r.Header.Get("Accept-Language")) body, err := ioutil.ReadAll(r.Body) if err != nil { raven.CaptureErrorAndWait(err, nil) @@ -475,7 +476,19 @@ func createHandler(w http.ResponseWriter, r *http.Request) { w.Write(jss) } +type ActivityCallback struct { + ClientId string `json:"clientId"` + Activity Activity `json:"activity"` + SystemUrl string `json:"systemUrl,omitempty"` +} + +type Activity struct { + Active bool `json:"active"` + Freeze bool `json:"freeze"` +} + func activityHandler(w http.ResponseWriter, r *http.Request) { + setLocale(r.Header.Get("Accept-Language")) w.Header().Set("Content-Type", "application/json") res := Response{Success: false} @@ -494,7 +507,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { raven.CaptureErrorAndWait(err, nil) - res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"}) + res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "wrong_data"}) jsonString, err := json.Marshal(res) if err != nil { raven.CaptureErrorAndWait(err, nil) @@ -505,12 +518,12 @@ func activityHandler(w http.ResponseWriter, r *http.Request) { return } - var rec Connection + var rec ActivityCallback err = json.Unmarshal(body, &rec) if err != nil { raven.CaptureErrorAndWait(err, nil) - res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"}) + res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "wrong_data"}) jsonString, err := json.Marshal(res) if err != nil { raven.CaptureErrorAndWait(err, nil) @@ -521,9 +534,12 @@ func activityHandler(w http.ResponseWriter, r *http.Request) { return } - if err := rec.setConnectionActivity(); err != nil { + c := getConnection(rec.ClientId) + c.Active = rec.Activity.Active + + if err := c.setConnectionActivity(); err != nil { raven.CaptureErrorAndWait(err, nil) - res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"}) + res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "wrong_data"}) jsonString, err := json.Marshal(res) if err != nil { raven.CaptureErrorAndWait(err, nil) diff --git a/telegram.go b/telegram.go index 11916a7..03f5593 100644 --- a/telegram.go +++ b/telegram.go @@ -32,7 +32,7 @@ func GetBotName(bot *tgbotapi.BotAPI) string { } func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) { - c, err := getConnectionByBotToken(token) + b, err := getBotByToken(token) if err != nil { raven.CaptureErrorAndWait(err, nil) logger.Error(token, err.Error()) @@ -40,18 +40,25 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string return } - if len(c.Bots) == 0 { + if b.ID == 0 { logger.Error(token, "missing") w.WriteHeader(http.StatusBadRequest) return } - if !c.Bots[0].Active { + if !b.Active { logger.Error(token, "deactivated") w.WriteHeader(http.StatusBadRequest) return } + c := getConnectionById(b.ConnectionID) + if c.MGURL == "" || c.MGToken == "" { + logger.Error(token, "MGURL or MGToken is empty") + w.WriteHeader(http.StatusBadRequest) + return + } + var update tgbotapi.Update bytes, err := ioutil.ReadAll(r.Body)