add line message log

This commit is contained in:
Jay 2018-09-20 00:17:00 +08:00
parent ee12feda5d
commit 116c43b0cb
4 changed files with 103 additions and 4 deletions

View File

@ -11,3 +11,10 @@ type LineMessageLog struct {
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"`
} }
// 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)`
err = x.Get(&msglog, query, g, u, msg)
return
}

View File

@ -9,3 +9,15 @@ type LineUser struct {
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"`
} }
// GetLineUserByID -
func GetLineUserByID(id string) (u *LineUser, err error) {
err = x.Get(&u, `select * from "public"."line_user" where "id" = $1`, id)
return
}
// Add -
func (p *LineUser) Add() (err error) {
_, err = x.NamedExec(`insert into "public"."line_user" ("id", "name") values (:id, :name)`, p)
return
}

View File

@ -3,9 +3,12 @@ package line
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"git.trj.tw/golang/mtfosbot/module/apis" "git.trj.tw/golang/mtfosbot/module/apis"
"git.trj.tw/golang/mtfosbot/module/config" "git.trj.tw/golang/mtfosbot/module/config"
@ -24,6 +27,12 @@ type ImageMessage struct {
PreviewImageURL string `json:"previewImageUrl"` PreviewImageURL string `json:"previewImageUrl"`
} }
// LineUserInfo -
type LineUserInfo struct {
DisplayName string `json:"displayName"`
UserID string `json:"userId"`
}
type pushBody struct { type pushBody struct {
To string `json:"to"` To string `json:"to"`
Messages []interface{} `json:"messages"` Messages []interface{} `json:"messages"`
@ -61,7 +70,7 @@ func PushMessage(target string, message interface{}) {
if len(target) == 0 { if len(target) == 0 {
return return
} }
url := "/v2/bot/message/push" urlPath := "/v2/bot/message/push"
body := &pushBody{ body := &pushBody{
To: target, To: target,
@ -90,7 +99,7 @@ func PushMessage(target string, message interface{}) {
byteReader := bytes.NewReader(dataByte) byteReader := bytes.NewReader(dataByte)
apiURL, ok := getURL(url) apiURL, ok := getURL(urlPath)
if !ok { if !ok {
fmt.Println("url parser fail") fmt.Println("url parser fail")
return return
@ -120,7 +129,7 @@ func ReplyMessage(replyToken string, message interface{}) {
if len(replyToken) == 0 { if len(replyToken) == 0 {
return return
} }
url := "/v2/bot/message/reply" urlPath := "/v2/bot/message/reply"
body := &replyBody{ body := &replyBody{
ReplyToken: replyToken, ReplyToken: replyToken,
@ -150,7 +159,7 @@ func ReplyMessage(replyToken string, message interface{}) {
byteReader := bytes.NewReader(dataByte) byteReader := bytes.NewReader(dataByte)
apiURL, ok := getURL(url) apiURL, ok := getURL(urlPath)
if !ok { if !ok {
fmt.Println("url parser fail") fmt.Println("url parser fail")
return return
@ -174,3 +183,49 @@ func ReplyMessage(replyToken string, message interface{}) {
return return
} }
} }
// GetUserInfo -
func GetUserInfo(u, g string) (user *LineUserInfo, err error) {
urlPath := fmt.Sprintf("/v2/bot/group/%s/member/%s", g, u)
header := getHeaders()
apiURL, ok := getURL(urlPath)
if !ok {
return nil, errors.New("url parser fail")
}
reqObj := apis.RequestObj{
Method: "GET",
URL: apiURL,
Headers: header,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("api response not 200")
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
return nil, errors.New("response body not json")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(bodyBytes, &user)
if err != nil {
return nil, err
}
return
}

View File

@ -1,6 +1,8 @@
package linemsg package linemsg
import ( import (
"git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/apis/line"
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"
) )
@ -34,7 +36,30 @@ func textMsg(e *lineobj.EventObject) {
if e.Source.Type == "group" { if e.Source.Type == "group" {
if txt, ok := mtxt.(string); ok { if txt, ok := mtxt.(string); ok {
msgcmd.ParseLineMsg(txt, e.ReplyToken, e.Source) msgcmd.ParseLineMsg(txt, e.ReplyToken, e.Source)
saveTextMsgToLog(txt, e.Source)
} }
} }
return return
} }
func saveTextMsgToLog(txt string, s *lineobj.SourceObject) {
userData, err := model.GetLineUserByID(s.UserID)
if err != nil {
return
}
if userData == nil {
tmpu, err := line.GetUserInfo(s.UserID, s.GroupID)
if err != nil || tmpu == nil {
return
}
userData = &model.LineUser{}
userData.ID = tmpu.UserID
userData.Name = tmpu.DisplayName
err = userData.Add()
if err != nil {
return
}
}
model.AddLineMessageLog(s.GroupID, s.UserID, txt)
}