add line image log

This commit is contained in:
Jay
2018-10-02 00:11:32 +08:00
parent 6d7c9e92d6
commit 76b26ee4a3
7 changed files with 159 additions and 15 deletions
+81 -6
View File
@@ -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")
}