1
0
mirror of synced 2024-11-25 21:26:02 +03:00

Merge pull request #8 from gwinn/master

Fix activity callback
This commit is contained in:
Alex Lushpai 2018-05-31 19:47:32 +03:00 committed by GitHub
commit 41914051e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 18 deletions

View File

@ -32,18 +32,16 @@ func (c *Connection) createBot(b Bot) error {
return orm.DB.Model(c).Association("Bots").Append(&b).Error return orm.DB.Model(c).Association("Bots").Append(&b).Error
} }
func getConnectionByBotToken(token string) (*Connection, error) { func getBotByToken(token string) (*Bot, error) {
var c Connection var bot Bot
err := orm.DB.Where("active = ?", true). err := orm.DB.First(&bot, "token = ?", token).Error
Preload("Bots", "token = ?", token).
First(&c).Error
if gorm.IsRecordNotFoundError(err) { if gorm.IsRecordNotFoundError(err) {
return &c, nil return &bot, nil
} else { } else {
return &c, err return &bot, err
} }
return &c, nil return &bot, nil
} }
func getBotByChannel(ch uint64) *Bot { func getBotByChannel(ch uint64) *Bot {

View File

@ -133,7 +133,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
cb, err := getConnectionByBotToken(b.Token) cl, err := getBotByToken(b.Token)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) 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 return
} }
if len(cb.Bots) != 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
} }
@ -214,6 +214,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) {
setLocale(r.Header.Get("Accept-Language"))
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)
@ -475,7 +476,19 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
w.Write(jss) 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) { func activityHandler(w http.ResponseWriter, r *http.Request) {
setLocale(r.Header.Get("Accept-Language"))
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
res := Response{Success: false} res := Response{Success: false}
@ -494,7 +507,7 @@ func activityHandler(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)
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"}) res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "wrong_data"})
jsonString, err := json.Marshal(res) jsonString, err := json.Marshal(res)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
@ -505,12 +518,12 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
var rec Connection var rec ActivityCallback
err = json.Unmarshal(body, &rec) err = json.Unmarshal(body, &rec)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(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) jsonString, err := json.Marshal(res)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
@ -521,9 +534,12 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
return 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) 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) jsonString, err := json.Marshal(res)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)

View File

@ -32,7 +32,7 @@ func GetBotName(bot *tgbotapi.BotAPI) string {
} }
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) { func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) {
c, err := getConnectionByBotToken(token) b, err := getBotByToken(token)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
logger.Error(token, err.Error()) logger.Error(token, err.Error())
@ -40,18 +40,25 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
return return
} }
if len(c.Bots) == 0 { if b.ID == 0 {
logger.Error(token, "missing") logger.Error(token, "missing")
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
if !c.Bots[0].Active { if !b.Active {
logger.Error(token, "deactivated") logger.Error(token, "deactivated")
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return 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 var update tgbotapi.Update
bytes, err := ioutil.ReadAll(r.Body) bytes, err := ioutil.ReadAll(r.Body)