From fcc12ed82c1f98ec7dd370426009ee79a0fad66d Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 10 Sep 2018 23:12:18 +0800 Subject: [PATCH] add facebook page model method --- model/facebook_page.go | 36 +++++++++++++++++--- module/apis/line/line.go | 62 +++++++++++++++++++++++++++++++++++ module/background/facebook.go | 22 ++++++++++--- 3 files changed, 112 insertions(+), 8 deletions(-) diff --git a/model/facebook_page.go b/model/facebook_page.go index 487de89..9db35d0 100644 --- a/model/facebook_page.go +++ b/model/facebook_page.go @@ -2,12 +2,18 @@ package model import "time" +type FBGroups struct { + *LineGroup + Tmpl string `db:"tmpl"` +} + // FacebookPage - struct type FacebookPage struct { - ID string `db:"id" cc:"id"` - LastPost string `db:"lastpost" cc:"lastpost"` - Ctime time.Time `db:"ctime" cc:"ctime"` - Mtime time.Time `db:"mtime" cc:"ctime"` + ID string `db:"id" cc:"id"` + LastPost string `db:"lastpost" cc:"lastpost"` + Ctime time.Time `db:"ctime" cc:"ctime"` + Mtime time.Time `db:"mtime" cc:"ctime"` + Groups []*FBGroups `db:"-"` } // GetAllFacebookPage - @@ -18,3 +24,25 @@ func GetAllFacebookPage() (pages []*FacebookPage, err error) { } return } + +func (p *FacebookPage) UpdatePost(postID string) (err error) { + query := `update "public"."facebook_page" set "lastpost" = $1 where id = $2` + _, err = x.Exec(query, postID, p.ID) + if err != nil { + return + } + p.LastPost = postID + return +} + +func (p *FacebookPage) GetGroups() (err error) { + query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p + left join "public"."line_fb_rt" rt + on rt."facebook" = p.id + left join "public"."line_group" g + on g.id = rt."line" + where + p.id = $1` + err = x.Select(&p.Groups, query, p.ID) + return +} diff --git a/module/apis/line/line.go b/module/apis/line/line.go index e40d1eb..0a0ecd7 100644 --- a/module/apis/line/line.go +++ b/module/apis/line/line.go @@ -55,6 +55,9 @@ func getHeaders() map[string]string { // PushMessage - func PushMessage(target string, message interface{}) { + if len(target) == 0 { + return + } url := "/v2/bot/message/push" body := &pushBody{ @@ -108,3 +111,62 @@ func PushMessage(target string, message interface{}) { return } } + +func RelayMessage(replyToken string, message interface{}) { + if len(replyToken) == 0 { + return + } + url := "/v2/bot/message/reply" + + body := &replyBody{ + ReplyToken: replyToken, + } + + switch message.(type) { + case ImageMessage: + m := (message.(ImageMessage)) + m.Type = "image" + message = m + break + case TextMessage: + m := (message.(TextMessage)) + m.Type = "text" + message = m + break + default: + return + } + + body.Messages = append(body.Messages, message) + dataByte, err := json.Marshal(body) + if err != nil { + fmt.Println("json encoding error") + return + } + + byteReader := bytes.NewReader(dataByte) + + apiUrl, ok := getUrl(url) + if !ok { + fmt.Println("url parser fail") + return + } + + reqObj := apis.RequestObj{ + Method: "POST", + Url: apiUrl, + Headers: getHeaders(), + Body: byteReader, + } + + req, err := apis.GetRequest(reqObj) + if err != nil { + return + } + + _, err = http.DefaultClient.Do(req) + if err != nil { + fmt.Println("post api fail") + return + } +} diff --git a/module/background/facebook.go b/module/background/facebook.go index a394737..e16126e 100644 --- a/module/background/facebook.go +++ b/module/background/facebook.go @@ -6,6 +6,7 @@ import ( "regexp" "sort" "strconv" + "time" "github.com/PuerkitoBio/goquery" @@ -22,7 +23,7 @@ var idRegex = []*regexp.Regexp{ type PageData struct { ID string Text string - Time int + Time int32 Link string } @@ -45,6 +46,13 @@ func readFacebookPage() { } func getPageHTML(page *model.FacebookPage) { + err := page.GetGroups() + if err != nil { + fmt.Println(err) + fmt.Println("get group fail") + return + } + resp, err := http.Get(fmt.Sprintf("https://www.facebook.com/%s", page.ID)) if err != nil { return @@ -88,7 +96,8 @@ func getPageHTML(page *model.FacebookPage) { } fmt.Printf("Time: %s / Text: %s / ID: %s \n", time, text, postID) - timeInt, err := strconv.Atoi(time) + timeInt, err := strconv.ParseInt(time, 10, 32) + if err != nil { fmt.Println("convert time to int error") return @@ -100,7 +109,7 @@ func getPageHTML(page *model.FacebookPage) { data := &PageData{ ID: postID, Text: text, - Time: timeInt, + Time: int32(timeInt), Link: pageLink, } @@ -114,5 +123,10 @@ func getPageHTML(page *model.FacebookPage) { sort.Sort(sort.Reverse(byTime(pageData))) - // lastData := pageData[0] + lastData := pageData[0] + t := int32(time.Now().Unix()) + + if lastData.Time+600 > t { + + } }