From bfeb3f3fbe8185d9325f5bbb87ef4f320da3a564 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 8 Jun 2018 17:40:50 +0300 Subject: [PATCH] fix getUserByExternalID --- models.go | 4 ++-- repository.go | 4 ++-- telegram.go | 21 ++++++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/models.go b/models.go index 79f067f..da5d99c 100644 --- a/models.go +++ b/models.go @@ -32,8 +32,8 @@ type Bot struct { type Users struct { ID int `gorm:"primary_key"` ExternalID int `gorm:"external_id;not null;unique"` - UserPhotoURL string `gorm:"user_photo_url type:varchar(255);unique"` - UserPhotoID string `gorm:"user_photo_id type:varchar(100);unique"` + UserPhotoURL string `gorm:"user_photo_url type:varchar(255)"` + UserPhotoID string `gorm:"user_photo_id type:varchar(100)"` CreatedAt time.Time UpdatedAt time.Time } diff --git a/repository.go b/repository.go index 24b22b6..7ccb82a 100644 --- a/repository.go +++ b/repository.go @@ -88,8 +88,8 @@ func (u *Users) save() error { } func getUserByExternalID(eid int) *Users { - user := Users{ExternalID: eid} - orm.DB.First(&user) + user := Users{} + orm.DB.First(&user, "external_id = ?", eid) return &user } diff --git a/telegram.go b/telegram.go index c531c2d..94e4607 100644 --- a/telegram.go +++ b/telegram.go @@ -39,15 +39,15 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string } if b.ID == 0 || !b.Active { - logger.Error(token, "missing or deactivated") - w.WriteHeader(http.StatusBadRequest) + logger.Error(token, "telegramWebhookHandler: missing or deactivated") + w.WriteHeader(http.StatusOK) return } c := getConnectionById(b.ConnectionID) if !c.Active { - logger.Error(c.ClientID, "connection deativated") - w.WriteHeader(http.StatusBadRequest) + logger.Error(c.ClientID, "telegramWebhookHandler: connection deactivated") + w.WriteHeader(http.StatusOK) return } @@ -76,7 +76,6 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string user := getUserByExternalID(update.Message.From.ID) if user.Expired(config.UpdateInterval) || user.ID == 0 { - fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID) if err != nil { raven.CaptureErrorAndWait(err, nil) @@ -111,6 +110,10 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string } } + if config.Debug { + logger.Debugf("telegramWebhookHandler user %v", user) + } + var client = v1.New(c.MGURL, c.MGToken) if update.Message != nil { @@ -208,7 +211,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { b := getBotByChannel(msg.Data.ChannelID) if b.ID == 0 || !b.Active { - logger.Error(msg.Data.ChannelID, "missing or deactivated") + logger.Error(msg.Data.ChannelID, "mgWebhookHandler: missing or deactivated") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("missing or deactivated")) return @@ -216,7 +219,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { c := getConnectionById(b.ConnectionID) if !c.Active { - logger.Error(c.ClientID, "connection deativated") + logger.Error(c.ClientID, "mgWebhookHandler: connection deactivated") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Connection deactivated")) return @@ -312,6 +315,10 @@ func GetFileIDAndURL(token string, userID int) (fileID, fileURL string, err erro return } + if config.Debug { + logger.Debugf("GetFileIDAndURL Photos: %v", res.Photos) + } + if len(res.Photos) > 0 { fileID = res.Photos[0][len(res.Photos[0])-1].FileID fileURL, err = bot.GetFileDirectURL(fileID)