mtfosbot/module/line-message/message-type.go

142 lines
2.4 KiB
Go
Raw Normal View History

2018-09-13 10:18:59 +00:00
package linemsg
import (
2018-10-01 16:11:32 +00:00
"fmt"
"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"
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
}
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")
2018-09-19 16:17:00 +00:00
}