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: ''
self_key: ''
image_root: '/images'
log_image_root: '/log_images'
line:
secret: ''
access: ''

View File

@ -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)}, ""))
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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"`

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")
}

2
schema

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