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

View File

@ -2,6 +2,7 @@ port: 10230
url: '' url: ''
self_key: '' self_key: ''
image_root: '/images' image_root: '/images'
log_image_root: '/log_images'
line: line:
secret: '' secret: ''
access: '' access: ''

View File

@ -74,6 +74,9 @@ func main() {
log.Fatal(err) 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)}, "")) server.Run(strings.Join([]string{":", strconv.Itoa(config.GetConf().Port)}, ""))
} }

View File

@ -11,6 +11,7 @@ type LineMessageLog struct {
Group string `db:"group" cc:"group"` Group string `db:"group" cc:"group"`
User string `db:"user" cc:"user"` User string `db:"user" cc:"user"`
Message string `db:"message" cc:"message"` Message string `db:"message" cc:"message"`
Type string `db:"type" cc:"type"`
Ctime time.Time `db:"ctime" cc:"ctime"` Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"mtime"` Mtime time.Time `db:"mtime" cc:"mtime"`
} }
@ -23,10 +24,10 @@ type LineMessageLogWithUG struct {
} }
// AddLineMessageLog - // AddLineMessageLog -
func AddLineMessageLog(g, u, msg string) (msglog *LineMessageLog, err error) { func AddLineMessageLog(g, u, msg, t string) (msglog *LineMessageLog, err error) {
query := `insert into "public"."line_message_log" ("group", "user", "message") values ($1, $2, $3)` query := `insert into "public"."line_message_log" ("group", "user", "message", "type") values ($1, $2, $3, $4)`
msglog = &LineMessageLog{} msglog = &LineMessageLog{}
err = x.Get(msglog, query, g, u, msg) err = x.Get(msglog, query, g, u, msg, t)
return return
} }

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -224,3 +225,65 @@ func GetUserInfo(u, g string) (user *LineUserInfo, err error) {
return 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
}

View File

@ -12,11 +12,12 @@ import (
// Config - // Config -
type Config struct { type Config struct {
Port int `yaml:"port"` Port int `yaml:"port"`
URL string `yaml:"url"` URL string `yaml:"url"`
SelfKey string `yaml:"self_key"` SelfKey string `yaml:"self_key"`
ImageRoot string `yaml:"image_root"` ImageRoot string `yaml:"image_root"`
Line struct { LogImageRoot string `yaml:"log_image_root"`
Line struct {
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
Access string `yaml:"access"` Access string `yaml:"access"`
} `yaml:"line"` } `yaml:"line"`

View File

@ -1,8 +1,13 @@
package linemsg package linemsg
import ( import (
"fmt"
"os"
"path"
"git.trj.tw/golang/mtfosbot/model" "git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/apis/line" "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" lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object"
msgcmd "git.trj.tw/golang/mtfosbot/module/message-command" msgcmd "git.trj.tw/golang/mtfosbot/module/message-command"
) )
@ -20,6 +25,7 @@ func messageType(e *lineobj.EventObject) {
textMsg(e) textMsg(e)
break break
case "image": case "image":
imageMsg(e)
break break
} }
} }
@ -43,24 +49,93 @@ func textMsg(e *lineobj.EventObject) {
return return
} }
func saveTextMsgToLog(txt string, s *lineobj.SourceObject) { func imageMsg(e *lineobj.EventObject) {
userData, err := model.GetLineUserByID(s.UserID) 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 { if err != nil {
return return
} }
if userData == nil { if userData == nil {
tmpu, err := line.GetUserInfo(s.UserID, s.GroupID) tmpu, err := line.GetUserInfo(uid, gid)
if err != nil || tmpu == nil { if err != nil || tmpu == nil {
return return nil, err
} }
userData = &model.LineUser{} userData = &model.LineUser{}
userData.ID = tmpu.UserID userData.ID = tmpu.UserID
userData.Name = tmpu.DisplayName userData.Name = tmpu.DisplayName
err = userData.Add() err = userData.Add()
if err != nil { 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")
} }

2
schema

@ -1 +1 @@
Subproject commit 03ae87dcfe0b20adca876c6ab40e83cedbe2af16 Subproject commit 5d3b60889f849ad999e4e1a7919b545dcfaccc62