add facebook page model method

This commit is contained in:
Jay 2018-09-10 23:12:18 +08:00
parent 145fbcd42e
commit fcc12ed82c
3 changed files with 112 additions and 8 deletions

View File

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

View File

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

View File

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