add facebook page api
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.trj.tw/golang/mtfosbot/model"
|
||||
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
||||
"git.trj.tw/golang/mtfosbot/module/context"
|
||||
"git.trj.tw/golang/mtfosbot/module/utils"
|
||||
"github.com/gin-gonic/contrib/sessions"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@@ -100,3 +103,49 @@ func GetSessionData(c *context.Context) {
|
||||
}
|
||||
c.Success(user)
|
||||
}
|
||||
|
||||
// GetLineMessageLog -
|
||||
func GetLineMessageLog(c *context.Context) {
|
||||
numP := 1
|
||||
if p, ok := c.GetQuery("p"); ok {
|
||||
if i, err := strconv.Atoi(p); err == nil {
|
||||
numP = i
|
||||
}
|
||||
}
|
||||
numMax := 20
|
||||
if max, ok := c.GetQuery("max"); ok {
|
||||
if m, err := strconv.Atoi(max); err == nil {
|
||||
numMax = m
|
||||
}
|
||||
}
|
||||
|
||||
g := c.DefaultQuery("group", "")
|
||||
u := c.DefaultQuery("user", "")
|
||||
|
||||
count, err := model.GetLineMessageLogCount()
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
|
||||
page := utils.CalcPage(count, numP, numMax)
|
||||
|
||||
logs, err := model.GetLineMessageLogList(g, u, page.Offset, page.Limit)
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
|
||||
resMap := make([]map[string]interface{}, 0)
|
||||
|
||||
for _, v := range logs {
|
||||
m := utils.ToMap(v.LineMessageLog)
|
||||
m["group_name"] = v.GroupName
|
||||
m["user_name"] = v.UserName
|
||||
resMap = append(resMap, m)
|
||||
}
|
||||
|
||||
c.Success(map[string]interface{}{
|
||||
"list": resMap,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package private
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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,
|
||||
})
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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,
|
||||
}
|
||||
line.PushMessage(g.ID, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.Success(nil)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"git.trj.tw/golang/mtfosbot/router/api"
|
||||
"git.trj.tw/golang/mtfosbot/router/google"
|
||||
"git.trj.tw/golang/mtfosbot/router/line"
|
||||
"git.trj.tw/golang/mtfosbot/router/private"
|
||||
"git.trj.tw/golang/mtfosbot/router/rimg"
|
||||
"git.trj.tw/golang/mtfosbot/router/twitch"
|
||||
"github.com/gin-contrib/cors"
|
||||
@@ -59,9 +60,17 @@ func SetRoutes(r *gin.Engine) {
|
||||
{
|
||||
apiGroup.POST("/login", context.PatchCtx(api.UserLogin))
|
||||
apiGroup.POST("/logout", context.PatchCtx(api.UserLogout))
|
||||
apiGroup.GET("/line_msg", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineMessageLog))
|
||||
apiGroup.GET("/session", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetSessionData))
|
||||
apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus))
|
||||
}
|
||||
|
||||
privateAPIGroup := apiGroup.Group("/private", context.PatchCtx(private.VerifyKey))
|
||||
{
|
||||
privateAPIGroup.GET("/pages", context.PatchCtx(private.GetFacebookPageIDs))
|
||||
privateAPIGroup.POST("/pageposts", context.PatchCtx(private.UpdateFacebookPagePost))
|
||||
}
|
||||
|
||||
apiTwitchGroup := apiGroup.Group("/twitch", context.PatchCtx(api.CheckSession))
|
||||
{
|
||||
apiTwitchGroup.GET("/channels", context.PatchCtx(api.GetChannels), context.PatchCtx(api.GetChannelList))
|
||||
@@ -73,7 +82,6 @@ func SetRoutes(r *gin.Engine) {
|
||||
twitchChannelGroup.GET("/opay/setting", context.PatchCtx(api.GetDonateSetting))
|
||||
twitchChannelGroup.PUT("/opay/setting", context.PatchCtx(api.UpdateDonateSetting))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
r.POST("/line", context.PatchCtx(line.GetRawBody), context.PatchCtx(line.VerifyLine), context.PatchCtx(line.GetLineMessage))
|
||||
|
||||
Reference in New Issue
Block a user