From 116c43b0cb1d86b4cc80a7ab1b035ab7c65cc589 Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 20 Sep 2018 00:17:00 +0800 Subject: [PATCH] add line message log --- model/line_message_log.go | 7 ++++ model/line_user.go | 12 ++++++ module/apis/line/line.go | 63 +++++++++++++++++++++++++++-- module/line-message/message-type.go | 25 ++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-) diff --git a/model/line_message_log.go b/model/line_message_log.go index 15c12f1..3ec36ae 100644 --- a/model/line_message_log.go +++ b/model/line_message_log.go @@ -11,3 +11,10 @@ type LineMessageLog struct { Ctime time.Time `db:"ctime" cc:"ctime"` 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 +} diff --git a/model/line_user.go b/model/line_user.go index 4399f57..56aabcd 100644 --- a/model/line_user.go +++ b/model/line_user.go @@ -9,3 +9,15 @@ type LineUser struct { Ctime time.Time `db:"ctime" cc:"ctime"` 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 +} diff --git a/module/apis/line/line.go b/module/apis/line/line.go index 1f684d9..59a1186 100644 --- a/module/apis/line/line.go +++ b/module/apis/line/line.go @@ -3,9 +3,12 @@ package line import ( "bytes" "encoding/json" + "errors" "fmt" + "io/ioutil" "net/http" "net/url" + "strings" "git.trj.tw/golang/mtfosbot/module/apis" "git.trj.tw/golang/mtfosbot/module/config" @@ -24,6 +27,12 @@ type ImageMessage struct { PreviewImageURL string `json:"previewImageUrl"` } +// LineUserInfo - +type LineUserInfo struct { + DisplayName string `json:"displayName"` + UserID string `json:"userId"` +} + type pushBody struct { To string `json:"to"` Messages []interface{} `json:"messages"` @@ -61,7 +70,7 @@ func PushMessage(target string, message interface{}) { if len(target) == 0 { return } - url := "/v2/bot/message/push" + urlPath := "/v2/bot/message/push" body := &pushBody{ To: target, @@ -90,7 +99,7 @@ func PushMessage(target string, message interface{}) { byteReader := bytes.NewReader(dataByte) - apiURL, ok := getURL(url) + apiURL, ok := getURL(urlPath) if !ok { fmt.Println("url parser fail") return @@ -120,7 +129,7 @@ func ReplyMessage(replyToken string, message interface{}) { if len(replyToken) == 0 { return } - url := "/v2/bot/message/reply" + urlPath := "/v2/bot/message/reply" body := &replyBody{ ReplyToken: replyToken, @@ -150,7 +159,7 @@ func ReplyMessage(replyToken string, message interface{}) { byteReader := bytes.NewReader(dataByte) - apiURL, ok := getURL(url) + apiURL, ok := getURL(urlPath) if !ok { fmt.Println("url parser fail") return @@ -174,3 +183,49 @@ func ReplyMessage(replyToken string, message interface{}) { 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 +} diff --git a/module/line-message/message-type.go b/module/line-message/message-type.go index a21d085..17127ef 100644 --- a/module/line-message/message-type.go +++ b/module/line-message/message-type.go @@ -1,6 +1,8 @@ package linemsg 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" msgcmd "git.trj.tw/golang/mtfosbot/module/message-command" ) @@ -34,7 +36,30 @@ func textMsg(e *lineobj.EventObject) { if e.Source.Type == "group" { if txt, ok := mtxt.(string); ok { msgcmd.ParseLineMsg(txt, e.ReplyToken, e.Source) + saveTextMsgToLog(txt, e.Source) } } 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) +}