2018-09-13 10:18:59 +00:00
|
|
|
package linemsg
|
|
|
|
|
|
|
|
import (
|
2018-10-01 16:11:32 +00:00
|
|
|
"fmt"
|
2019-05-28 03:17:22 +00:00
|
|
|
"log"
|
|
|
|
"net/url"
|
2018-10-01 16:11:32 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2018-09-19 16:17:00 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/model"
|
|
|
|
"git.trj.tw/golang/mtfosbot/module/apis/line"
|
2018-10-01 16:11:32 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/config"
|
2019-05-28 03:17:22 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/es"
|
2018-09-13 10:18:59 +00:00
|
|
|
lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object"
|
2018-09-14 16:01:36 +00:00
|
|
|
msgcmd "git.trj.tw/golang/mtfosbot/module/message-command"
|
2018-09-13 10:18:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func messageType(e *lineobj.EventObject) {
|
|
|
|
msg := e.Message
|
|
|
|
mtype, ok := msg["type"]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if t, ok := mtype.(string); ok {
|
|
|
|
switch t {
|
|
|
|
case "text":
|
2018-09-20 17:14:08 +00:00
|
|
|
textMsg(e)
|
2018-09-13 10:18:59 +00:00
|
|
|
break
|
|
|
|
case "image":
|
2018-10-01 16:11:32 +00:00
|
|
|
imageMsg(e)
|
2018-09-13 10:18:59 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func textMsg(e *lineobj.EventObject) {
|
|
|
|
msg := e.Message
|
|
|
|
mtxt, ok := msg["text"]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// group action
|
|
|
|
if e.Source.Type == "group" {
|
2018-09-14 16:01:36 +00:00
|
|
|
if txt, ok := mtxt.(string); ok {
|
|
|
|
msgcmd.ParseLineMsg(txt, e.ReplyToken, e.Source)
|
2018-09-19 16:17:00 +00:00
|
|
|
saveTextMsgToLog(txt, e.Source)
|
2018-09-13 10:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-09-19 16:17:00 +00:00
|
|
|
|
2018-10-01 16:11:32 +00:00
|
|
|
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)
|
2018-09-19 16:17:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if userData == nil {
|
2018-10-01 16:11:32 +00:00
|
|
|
tmpu, err := line.GetUserInfo(uid, gid)
|
2018-09-19 16:17:00 +00:00
|
|
|
if err != nil || tmpu == nil {
|
2018-10-01 16:11:32 +00:00
|
|
|
return nil, err
|
2018-09-19 16:17:00 +00:00
|
|
|
}
|
|
|
|
userData = &model.LineUser{}
|
|
|
|
userData.ID = tmpu.UserID
|
|
|
|
userData.Name = tmpu.DisplayName
|
|
|
|
err = userData.Add()
|
|
|
|
if err != nil {
|
2018-10-01 16:11:32 +00:00
|
|
|
return nil, err
|
2018-09-19 16:17:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 16:11:32 +00:00
|
|
|
return userData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveTextMsgToLog(txt string, s *lineobj.SourceObject) {
|
|
|
|
u, err := getSourceUser(s.UserID, s.GroupID)
|
|
|
|
if err != nil || u == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-28 03:17:22 +00:00
|
|
|
go saveLineMessageLogToES(s.GroupID, s.UserID, txt, "text")
|
2018-10-01 16:11:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-28 03:17:22 +00:00
|
|
|
furl, err := url.Parse(conf.URL)
|
|
|
|
if err == nil {
|
|
|
|
furl, err = furl.Parse(fmt.Sprintf("/image/line_log_image/%s", fname))
|
|
|
|
if err == nil {
|
|
|
|
fname = furl.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go saveLineMessageLogToES(s.GroupID, s.UserID, fname, "image")
|
2018-10-01 16:11:32 +00:00
|
|
|
model.AddLineMessageLog(s.GroupID, s.UserID, fname, "image")
|
2018-09-19 16:17:00 +00:00
|
|
|
}
|
2019-05-28 03:17:22 +00:00
|
|
|
|
|
|
|
func saveLineMessageLogToES(gid, uid, content, msgType string) {
|
|
|
|
lineGroup, err := model.GetLineGroup(gid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("get line group error :: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lineUser, err := model.GetLineUserByID(uid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("get line user error :: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logMsg := make(map[string]interface{})
|
|
|
|
|
|
|
|
logMsg["message"] = content
|
|
|
|
logMsg["type"] = msgType
|
|
|
|
logMsg["group"] = lineGroup.ID
|
|
|
|
logMsg["group_name"] = lineGroup.Name
|
|
|
|
logMsg["user"] = lineUser.ID
|
|
|
|
logMsg["user_name"] = lineUser.Name
|
|
|
|
|
|
|
|
err = es.PutLog("log", logMsg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("put log fail :: ", err)
|
|
|
|
}
|
|
|
|
}
|