diff --git a/config.default.yml b/config.default.yml index e5e8f5a..3d959c0 100644 --- a/config.default.yml +++ b/config.default.yml @@ -2,6 +2,7 @@ port: 10230 url: '' self_key: '' image_root: '/images' +log_image_root: '/log_images' line: secret: '' access: '' diff --git a/main.go b/main.go index 5d216d1..1cb4fbb 100644 --- a/main.go +++ b/main.go @@ -74,6 +74,9 @@ func main() { log.Fatal(err) } } + if !utils.CheckExists(conf.LogImageRoot, true) { + log.Fatal(errors.New("log image root not exists")) + } server.Run(strings.Join([]string{":", strconv.Itoa(config.GetConf().Port)}, "")) } diff --git a/model/line_message_log.go b/model/line_message_log.go index 857f3b2..7162629 100644 --- a/model/line_message_log.go +++ b/model/line_message_log.go @@ -11,6 +11,7 @@ type LineMessageLog struct { Group string `db:"group" cc:"group"` User string `db:"user" cc:"user"` Message string `db:"message" cc:"message"` + Type string `db:"type" cc:"type"` Ctime time.Time `db:"ctime" cc:"ctime"` Mtime time.Time `db:"mtime" cc:"mtime"` } @@ -23,10 +24,10 @@ type LineMessageLogWithUG struct { } // AddLineMessageLog - -func AddLineMessageLog(g, u, msg string) (msglog *LineMessageLog, err error) { - query := `insert into "public"."line_message_log" ("group", "user", "message") values ($1, $2, $3)` +func AddLineMessageLog(g, u, msg, t string) (msglog *LineMessageLog, err error) { + query := `insert into "public"."line_message_log" ("group", "user", "message", "type") values ($1, $2, $3, $4)` msglog = &LineMessageLog{} - err = x.Get(msglog, query, g, u, msg) + err = x.Get(msglog, query, g, u, msg, t) return } diff --git a/module/apis/line/line.go b/module/apis/line/line.go index ea2aa7e..f6cbd4c 100644 --- a/module/apis/line/line.go +++ b/module/apis/line/line.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/ioutil" "net/http" "net/url" @@ -224,3 +225,65 @@ func GetUserInfo(u, g string) (user *LineUserInfo, err error) { return } + +// GetContentHead - +func GetContentHead(id string) (mime string, err error) { + urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id) + header := getHeaders() + u, ok := getURL(urlPath) + if !ok { + return "", errors.New("get url fail") + } + + reqObj := apis.RequestObj{ + Method: "HEAD", + URL: u, + Headers: header, + } + + req, err := apis.GetRequest(reqObj) + if err != nil { + return "", err + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + mime = resp.Header.Get("Content-Type") + + return +} + +// DownloadContent - +func DownloadContent(id string, w io.Writer) (err error) { + urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id) + header := getHeaders() + u, ok := getURL(urlPath) + if !ok { + return errors.New("get url fail") + } + + reqObj := apis.RequestObj{ + Method: "GET", + URL: u, + Headers: header, + } + + req, err := apis.GetRequest(reqObj) + if err != nil { + return err + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + _, err = io.Copy(w, resp.Body) + + return +} diff --git a/module/config/config.go b/module/config/config.go index 8bdcbee..2a980d8 100644 --- a/module/config/config.go +++ b/module/config/config.go @@ -12,11 +12,12 @@ import ( // Config - type Config struct { - Port int `yaml:"port"` - URL string `yaml:"url"` - SelfKey string `yaml:"self_key"` - ImageRoot string `yaml:"image_root"` - Line struct { + Port int `yaml:"port"` + URL string `yaml:"url"` + SelfKey string `yaml:"self_key"` + ImageRoot string `yaml:"image_root"` + LogImageRoot string `yaml:"log_image_root"` + Line struct { Secret string `yaml:"secret"` Access string `yaml:"access"` } `yaml:"line"` diff --git a/module/line-message/message-type.go b/module/line-message/message-type.go index 24252b5..7f6f5c2 100644 --- a/module/line-message/message-type.go +++ b/module/line-message/message-type.go @@ -1,8 +1,13 @@ package linemsg import ( + "fmt" + "os" + "path" + "git.trj.tw/golang/mtfosbot/model" "git.trj.tw/golang/mtfosbot/module/apis/line" + "git.trj.tw/golang/mtfosbot/module/config" lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object" msgcmd "git.trj.tw/golang/mtfosbot/module/message-command" ) @@ -20,6 +25,7 @@ func messageType(e *lineobj.EventObject) { textMsg(e) break case "image": + imageMsg(e) break } } @@ -43,24 +49,93 @@ func textMsg(e *lineobj.EventObject) { return } -func saveTextMsgToLog(txt string, s *lineobj.SourceObject) { - userData, err := model.GetLineUserByID(s.UserID) +func imageMsg(e *lineobj.EventObject) { + msg := e.Message + imgID, ok := msg["id"] + if !ok { + return + } + // group action + if e.Source.Type == "group" { + if id, ok := imgID.(string); ok { + saveImageMsgToLog(id, e.Source) + } + } +} + +func getSourceUser(uid, gid string) (u *model.LineUser, err error) { + userData, err := model.GetLineUserByID(uid) if err != nil { return } if userData == nil { - tmpu, err := line.GetUserInfo(s.UserID, s.GroupID) + tmpu, err := line.GetUserInfo(uid, gid) if err != nil || tmpu == nil { - return + return nil, err } userData = &model.LineUser{} userData.ID = tmpu.UserID userData.Name = tmpu.DisplayName err = userData.Add() if err != nil { - return + return nil, err } } - model.AddLineMessageLog(s.GroupID, s.UserID, txt) + return userData, nil +} + +func saveTextMsgToLog(txt string, s *lineobj.SourceObject) { + u, err := getSourceUser(s.UserID, s.GroupID) + if err != nil || u == nil { + return + } + + model.AddLineMessageLog(s.GroupID, s.UserID, txt, "text") +} + +func saveImageMsgToLog(id string, s *lineobj.SourceObject) { + u, err := getSourceUser(s.UserID, s.GroupID) + if err != nil || u == nil { + return + } + + mime, err := line.GetContentHead(id) + if err != nil || len(mime) == 0 { + return + } + + ext := "" + switch mime { + case "image/jpeg": + ext = ".jpg" + break + case "image/jpg": + ext = ".jpg" + break + case "image/png": + ext = ".png" + break + default: + return + } + + conf := config.GetConf() + + fname := fmt.Sprintf("log_%s%s", id, ext) + + fullPath := path.Join(conf.LogImageRoot, fname) + + w, err := os.Create(fullPath) + if err != nil { + return + } + defer w.Close() + + err = line.DownloadContent(id, w) + if err != nil { + return + } + + model.AddLineMessageLog(s.GroupID, s.UserID, fname, "image") } diff --git a/schema b/schema index 03ae87d..5d3b608 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit 03ae87dcfe0b20adca876c6ab40e83cedbe2af16 +Subproject commit 5d3b60889f849ad999e4e1a7919b545dcfaccc62