2018-09-21 13:37:33 +00:00
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-25 09:02:03 +00:00
|
|
|
"log"
|
2018-09-21 13:37:33 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.trj.tw/golang/mtfosbot/model"
|
|
|
|
"git.trj.tw/golang/mtfosbot/module/apis/line"
|
|
|
|
"git.trj.tw/golang/mtfosbot/module/config"
|
|
|
|
"git.trj.tw/golang/mtfosbot/module/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VerifyKey -
|
|
|
|
func VerifyKey(c *context.Context) {
|
|
|
|
conf := config.GetConf()
|
|
|
|
key := c.GetHeader("X-Mtfos-Key")
|
|
|
|
|
|
|
|
if len(key) == 0 {
|
|
|
|
c.Forbidden(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if key != conf.SelfKey {
|
|
|
|
c.Forbidden(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFacebookPageIDs -
|
|
|
|
func GetFacebookPageIDs(c *context.Context) {
|
|
|
|
pages, err := model.GetAllFacebookPage()
|
|
|
|
if err != nil {
|
|
|
|
c.ServerError(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]string, 0)
|
|
|
|
for _, v := range pages {
|
|
|
|
ids = append(ids, v.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Success(map[string]interface{}{
|
|
|
|
"list": ids,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-22 14:25:09 +00:00
|
|
|
//GetInstagramIDs -
|
|
|
|
func GetInstagramIDs(c *context.Context) {
|
|
|
|
igs, err := model.GetAllInstagram()
|
|
|
|
if err != nil {
|
|
|
|
c.ServerError(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]string, 0, len(igs))
|
|
|
|
for _, v := range igs {
|
|
|
|
ids = append(ids, v.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Success(map[string]interface{}{
|
|
|
|
"list": ids,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-21 13:37:33 +00:00
|
|
|
// UpdateFacebookPagePost -
|
|
|
|
func UpdateFacebookPagePost(c *context.Context) {
|
|
|
|
var err error
|
|
|
|
type pageStruct struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
PostID string `json:"post_id"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
bodyArg := struct {
|
|
|
|
Pages []pageStruct `json:"pages"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err = c.BindData(&bodyArg)
|
|
|
|
if err != nil {
|
|
|
|
c.DataFormat(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range bodyArg.Pages {
|
|
|
|
if len(v.ID) == 0 || len(v.PostID) == 0 || len(v.Link) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err := model.GetFacebookPage(v.ID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if page.LastPost == v.PostID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = page.UpdatePost(v.PostID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = page.GetGroups()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range page.Groups {
|
|
|
|
if g.Notify {
|
2019-06-25 09:02:03 +00:00
|
|
|
bot, err := g.GetBot()
|
|
|
|
if err != nil || bot == nil {
|
|
|
|
log.Println("get group binding bot fail ::: ", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-21 13:37:33 +00:00
|
|
|
tmpl := g.Tmpl
|
|
|
|
if len(tmpl) > 0 {
|
|
|
|
tmpl = strings.Replace(tmpl, "{link}", v.Link, -1)
|
|
|
|
tmpl = strings.Replace(tmpl, "{txt}", v.Text, -1)
|
|
|
|
} else {
|
|
|
|
tmpl = fmt.Sprintf("%s\n%s", v.Text, v.Link)
|
|
|
|
}
|
|
|
|
msg := line.TextMessage{
|
|
|
|
Text: tmpl,
|
|
|
|
}
|
2019-06-25 09:02:03 +00:00
|
|
|
line.PushMessage(bot.AccessToken, g.ID, msg)
|
2018-09-21 13:37:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Success(nil)
|
|
|
|
}
|
2019-05-22 14:25:09 +00:00
|
|
|
|
|
|
|
// UpdateInstagramPost -
|
|
|
|
func UpdateInstagramPost(c *context.Context) {
|
|
|
|
var err error
|
|
|
|
type pageStruct struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
PostID string `json:"post_id"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
bodyArg := struct {
|
|
|
|
IGs []pageStruct `json:"igs"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err = c.BindData(&bodyArg)
|
|
|
|
if err != nil {
|
|
|
|
c.DataFormat(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range bodyArg.IGs {
|
|
|
|
if len(v.ID) == 0 || len(v.PostID) == 0 || len(v.Link) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ig, err := model.GetInstagram(v.ID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ig.LastPost == v.PostID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = ig.UpdatePost(v.PostID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ig.GetGroups()
|
|
|
|
if err != nil {
|
2019-05-22 15:25:25 +00:00
|
|
|
fmt.Println("get group err :: ", err)
|
2019-05-22 14:25:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range ig.Groups {
|
|
|
|
if g.Notify {
|
2019-06-25 09:02:03 +00:00
|
|
|
bot, err := g.GetBot()
|
|
|
|
if err != nil || bot == nil {
|
|
|
|
log.Println("get group binding bot fail :: ", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-05-22 14:25:09 +00:00
|
|
|
tmpl := g.Tmpl
|
|
|
|
if len(tmpl) > 0 {
|
|
|
|
tmpl = strings.Replace(tmpl, "{link}", v.Link, -1)
|
|
|
|
tmpl = strings.Replace(tmpl, "{txt}", v.Text, -1)
|
|
|
|
} else {
|
|
|
|
tmpl = fmt.Sprintf("%s\n%s", v.Text, v.Link)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := line.TextMessage{Text: tmpl}
|
2019-06-25 09:02:03 +00:00
|
|
|
line.PushMessage(bot.AccessToken, g.ID, msg)
|
2019-05-22 14:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Success(nil)
|
|
|
|
}
|