start write twitch api

This commit is contained in:
Jay 2018-09-11 17:58:08 +08:00
parent fcc12ed82c
commit bf325401d4
3 changed files with 65 additions and 11 deletions

View File

@ -11,14 +11,17 @@ import (
"git.trj.tw/golang/mtfosbot/module/config"
)
// TextMessage - line text message object
type TextMessage struct {
Type string `json:"type"`
Text string `json:"text"`
}
// ImageMessage - line image message object
type ImageMessage struct {
Type string `json:"type"`
OriginalContentUrl string `json:"originalContentUrl"`
PreviewImageUrl string `json:"previewImageUrl"`
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
}
type pushBody struct {
@ -30,10 +33,10 @@ type replyBody struct {
Messages []interface{} `json:"messages"`
}
var baseUrl = "https://api.line.me/"
var baseURL = "https://api.line.me/"
func getUrl(p string) (string, bool) {
u, err := url.Parse(baseUrl)
func getURL(p string) (string, bool) {
u, err := url.Parse(baseURL)
if err != nil {
return "", false
}
@ -87,7 +90,7 @@ func PushMessage(target string, message interface{}) {
byteReader := bytes.NewReader(dataByte)
apiUrl, ok := getUrl(url)
apiURL, ok := getURL(url)
if !ok {
fmt.Println("url parser fail")
return
@ -95,7 +98,7 @@ func PushMessage(target string, message interface{}) {
reqObj := apis.RequestObj{
Method: "POST",
Url: apiUrl,
Url: apiURL,
Headers: getHeaders(),
Body: byteReader,
}
@ -112,7 +115,8 @@ func PushMessage(target string, message interface{}) {
}
}
func RelayMessage(replyToken string, message interface{}) {
// ReplyMessage -
func ReplyMessage(replyToken string, message interface{}) {
if len(replyToken) == 0 {
return
}
@ -146,7 +150,7 @@ func RelayMessage(replyToken string, message interface{}) {
byteReader := bytes.NewReader(dataByte)
apiUrl, ok := getUrl(url)
apiURL, ok := getURL(url)
if !ok {
fmt.Println("url parser fail")
return
@ -154,7 +158,7 @@ func RelayMessage(replyToken string, message interface{}) {
reqObj := apis.RequestObj{
Method: "POST",
Url: apiUrl,
Url: apiURL,
Headers: getHeaders(),
Body: byteReader,
}

View File

@ -0,0 +1,28 @@
package twitch
import (
"fmt"
"net/url"
)
var baseURL = "https://api.twitch.tv"
func getURL(p string) (string, bool) {
u, err := url.Parse(baseURL)
if err != nil {
return "", false
}
ref, err := u.Parse(p)
if err != nil {
return "", false
}
str := ref.String()
return str, true
}
func getHeaders(token string) map[string]string {
m := make(map[string]string)
m["Content-Type"] = "application/json"
m["Authorization"] = fmt.Sprintf("Bearer %s", token)
return m
}

View File

@ -6,11 +6,13 @@ import (
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/apis/line"
)
var idRegex = []*regexp.Regexp{
@ -20,6 +22,7 @@ var idRegex = []*regexp.Regexp{
regexp.MustCompile(`\/videos\/(\d+)`),
}
// PageData - facebook fan page data
type PageData struct {
ID string
Text string
@ -126,7 +129,26 @@ func getPageHTML(page *model.FacebookPage) {
lastData := pageData[0]
t := int32(time.Now().Unix())
if lastData.Time+600 > t {
if (t-600) < lastData.Time && lastData.ID != page.LastPost {
err = page.UpdatePost(lastData.ID)
if err != nil {
return
}
for _, v := range page.Groups {
if v.Notify {
tmpl := v.Tmpl
if len(tmpl) > 0 {
tmpl = strings.Replace(tmpl, "{link}", lastData.Link, -1)
tmpl = strings.Replace(tmpl, "{txt}", lastData.Text, -1)
} else {
tmpl = fmt.Sprintf("%s\n%s", lastData.Text, lastData.Link)
}
msg := line.TextMessage{
Text: tmpl,
}
line.PushMessage(v.ID, msg)
}
}
}
}