From 3bb3d9bd4f552b540ff6704b3cc3abf529024fdd Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Mon, 4 Jun 2018 16:48:04 +0300 Subject: [PATCH] add user struct and migrate, check activity connection, upload avatar to s3 --- config.go | 15 ++- config.yml.dist | 9 ++ database.go | 8 ++ migrations/1528208070_app.down.sql | 1 + migrations/1528208070_app.up.sql | 13 +++ models.go | 10 ++ repository.go | 11 ++ routing.go | 6 +- telegram.go | 155 +++++++++++++++++++++++++---- 9 files changed, 203 insertions(+), 25 deletions(-) create mode 100644 migrations/1528208070_app.down.sql create mode 100644 migrations/1528208070_app.up.sql diff --git a/config.go b/config.go index a7de236..7a4ede1 100644 --- a/config.go +++ b/config.go @@ -4,8 +4,8 @@ import ( "io/ioutil" "path/filepath" - logging "github.com/op/go-logging" - yaml "gopkg.in/yaml.v2" + "github.com/op/go-logging" + "gopkg.in/yaml.v2" ) // TransportConfig struct @@ -15,6 +15,17 @@ type TransportConfig struct { SentryDSN string `yaml:"sentry_dsn"` HTTPServer HTTPServerConfig `yaml:"http_server"` Debug bool `yaml:"debug"` + UpdateTime int `yaml:"update_time"` + ConfigAWS ConfigAWS `yaml:"config_aws"` +} + +// ConfigAWS struct +type ConfigAWS struct { + AccessKeyID string `yaml:"access_key_id"` + SecretAccessKey string `yaml:"secret_access_key"` + Region string `yaml:"region"` + Bucket string `yaml:"bucket"` + ContentType string `yaml:"content_type"` } // DatabaseConfig struct diff --git a/config.yml.dist b/config.yml.dist index 357105b..d0ffe8b 100644 --- a/config.yml.dist +++ b/config.yml.dist @@ -10,3 +10,12 @@ sentry_dsn: ~ log_level: 5 debug: false + +update_time: 24 + +config_aws: + access_key_id: ~ + secret_access_key: ~ + region: ~ + bucket: ~ + content_type: image/jpeg diff --git a/database.go b/database.go index a412335..bc074a6 100644 --- a/database.go +++ b/database.go @@ -35,10 +35,18 @@ func NewDb(config *TransportConfig) *Orm { setCreatedAt := func(scope *gorm.Scope) { if scope.HasColumn("CreatedAt") { scope.SetColumn("CreatedAt", time.Now()) + scope.SetColumn("UpdatedAt", time.Now()) + } + } + + setUpdatedAt := func(scope *gorm.Scope) { + if scope.HasColumn("UpdatedAt") { + scope.SetColumn("UpdatedAt", time.Now()) } } db.Callback().Create().Replace("gorm:update_time_stamp", setCreatedAt) + db.Callback().Update().Register("update_time_stamp", setUpdatedAt) return &Orm{ DB: db, diff --git a/migrations/1528208070_app.down.sql b/migrations/1528208070_app.down.sql new file mode 100644 index 0000000..66c6be7 --- /dev/null +++ b/migrations/1528208070_app.down.sql @@ -0,0 +1 @@ +drop table user_tg; \ No newline at end of file diff --git a/migrations/1528208070_app.up.sql b/migrations/1528208070_app.up.sql new file mode 100644 index 0000000..21ea3ce --- /dev/null +++ b/migrations/1528208070_app.up.sql @@ -0,0 +1,13 @@ +create table user_tg +( + id serial not null + constraint user_tg_pkey + primary key, + external_id integer not null, + user_photo varchar(255), + user_photo_id varchar(100), + created_at timestamp with time zone, + updated_at timestamp with time zone, + constraint user_tg_key unique(external_id, user_photo, user_photo_id) +); + diff --git a/models.go b/models.go index aaa5236..f576dba 100644 --- a/models.go +++ b/models.go @@ -28,5 +28,15 @@ type Bot struct { Active bool `json:"active,omitempty"` } +// UserTG model +type UserTG struct { + ID int `gorm:"primary_key"` + ExternalID int `gorm:"external_id;not null;unique"` + UserPhotoURL string `gorm:"user_photo type:varchar(255);unique"` + UserPhotoID string `gorm:"user_photo_id type:varchar(100);unique"` + CreatedAt time.Time + UpdatedAt time.Time +} + //Bots list type Bots []Bot diff --git a/repository.go b/repository.go index 2d426c3..7eb94c9 100644 --- a/repository.go +++ b/repository.go @@ -78,3 +78,14 @@ func getConnectionById(id int) *Connection { return &connection } + +func (u *UserTG) saveUser() error { + return orm.DB.Save(u).Error +} + +func getUserByExternalID(eid int) *UserTG { + user := UserTG{ExternalID: eid} + orm.DB.First(&user) + + return &user +} diff --git a/routing.go b/routing.go index 6607831..74e5b96 100644 --- a/routing.go +++ b/routing.go @@ -152,7 +152,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { return } - bot, err := GetBotInfo(b.Token) + bot, err := tgbotapi.NewBotAPI(b.Token) if err != nil { logger.Error(b.Token, err.Error()) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_token"}), http.StatusBadRequest) @@ -278,8 +278,8 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) { err = b.setBotActivity() if err != nil { raven.CaptureErrorAndWait(err, nil) - logger.Error(b.ID, err.Error()) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) + logger.Error(b.ID, err.Error()) return } @@ -530,7 +530,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) { } c := getConnection(rec.ClientId) - c.Active = rec.Activity.Active + c.Active = rec.Activity.Active && !rec.Activity.Freeze if err := c.setConnectionActivity(); err != nil { raven.CaptureErrorAndWait(err, nil) diff --git a/telegram.go b/telegram.go index e7d433b..be8dd94 100644 --- a/telegram.go +++ b/telegram.go @@ -2,11 +2,18 @@ package main import ( "encoding/json" + "errors" "io/ioutil" "net/http" "strconv" "time" + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/getsentry/raven-go" "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/retailcrm/mg-transport-api-client-go/v1" @@ -17,15 +24,6 @@ func setTransportRoutes() { http.HandleFunc("/webhook/", mgWebhookHandler) } -// GetBotInfo function -func GetBotInfo(token string) (*tgbotapi.BotAPI, error) { - bot, err := tgbotapi.NewBotAPI(token) - if err != nil { - return nil, err - } - return bot, nil -} - // GetBotName function func GetBotName(bot *tgbotapi.BotAPI) string { return bot.Self.FirstName @@ -40,21 +38,15 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string return } - if b.ID == 0 { - logger.Error(token, "missing") - w.WriteHeader(http.StatusBadRequest) - return - } - - if !b.Active { - logger.Error(token, "deactivated") + if b.ID == 0 || !b.Active { + logger.Error(token, "missing or deactivated") w.WriteHeader(http.StatusBadRequest) return } c := getConnectionById(b.ConnectionID) - if c.MGURL == "" || c.MGToken == "" { - logger.Error(token, "MGURL or MGToken is empty") + if !c.Active { + logger.Error(c.ClientID, " connection deativated") w.WriteHeader(http.StatusBadRequest) return } @@ -64,8 +56,8 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string bytes, err := ioutil.ReadAll(r.Body) if err != nil { raven.CaptureErrorAndWait(err, nil) - w.WriteHeader(http.StatusInternalServerError) logger.Error(token, err) + w.WriteHeader(http.StatusInternalServerError) return } @@ -81,6 +73,44 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string return } + user := getUserByExternalID(update.Message.From.ID) + + if time.Now().After(user.UpdatedAt.Add(time.Hour*time.Duration(config.UpdateTime))) || user.ID == 0 { + + fileID, fileURL, err := getFileIDAndURL(b.Token, update.Message.From.ID) + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + if fileID != user.UserPhotoID && fileURL != "" { + picURL, err := UploadUserAvatar(fileURL) + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + user.UserPhotoID = fileID + user.UserPhotoURL = picURL + } + + if user.ExternalID == 0 { + user.ExternalID = update.Message.From.ID + } + + err = user.saveUser() + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + } + var client = v1.New(c.MGURL, c.MGToken) if update.Message != nil { @@ -98,6 +128,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string ExternalID: strconv.Itoa(update.Message.From.ID), Nickname: update.Message.From.UserName, Firstname: update.Message.From.FirstName, + Avatar: user.UserPhotoURL, Lastname: update.Message.From.LastName, Language: update.Message.From.LanguageCode, }, @@ -154,6 +185,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { if err != nil { raven.CaptureErrorAndWait(err, nil) logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) return } @@ -166,6 +198,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { if err != nil { raven.CaptureErrorAndWait(err, nil) logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) return } @@ -175,11 +208,23 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { b := getBotByChannel(msg.Data.ChannelID) if b.ID == 0 { logger.Error(msg.Data.ChannelID, "missing") + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Bot missing")) return } if !b.Active { logger.Error(msg.Data.ChannelID, "deactivated") + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Bot deactivated")) + return + } + + c := getConnectionById(b.ConnectionID) + if !c.Active { + logger.Error(c.ClientID, " connection deativated") + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Connection deactivated")) return } @@ -187,6 +232,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { if err != nil { raven.CaptureErrorAndWait(err, nil) logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) return } @@ -252,3 +298,72 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Message deleted")) } } + +func getFileIDAndURL(token string, userID int) (fileID, fileURL string, err error) { + bot, err := tgbotapi.NewBotAPI(token) + if err != nil { + return + } + + bot.Debug = config.Debug + + res, err := bot.GetUserProfilePhotos( + tgbotapi.UserProfilePhotosConfig{ + UserID: userID, + Limit: 1, + }, + ) + if err != nil { + return + } + + if len(res.Photos) > 0 { + fileID = res.Photos[0][len(res.Photos[0])-1].FileID + fileURL, err = bot.GetFileDirectURL(fileID) + if err != nil { + return + } + return + } + + return +} + +func UploadUserAvatar(url string) (picURLs3 string, err error) { + s3Config := &aws.Config{ + Credentials: credentials.NewStaticCredentials( + config.ConfigAWS.AccessKeyID, + config.ConfigAWS.SecretAccessKey, + ""), + Region: aws.String(config.ConfigAWS.Region), + } + + s := session.Must(session.NewSession(s3Config)) + uploader := s3manager.NewUploader(s) + + resp, err := http.Get(url) + if err != nil { + return + } + + if resp.StatusCode >= http.StatusBadRequest { + return "", errors.New(fmt.Sprintf("get: %v code: %v", url, resp.StatusCode)) + } + + defer resp.Body.Close() + + result, err := uploader.Upload(&s3manager.UploadInput{ + Bucket: aws.String(config.ConfigAWS.Bucket), + Key: aws.String(fmt.Sprintf("%v.jpg", GenerateToken())), + Body: resp.Body, + ContentType: aws.String(config.ConfigAWS.ContentType), + ACL: aws.String("public-read"), + }) + if err != nil { + return + } + + picURLs3 = result.Location + + return +}