mtfosbot/module/apis/line/line.go

177 lines
3.1 KiB
Go
Raw Normal View History

2018-09-10 10:13:27 +00:00
package line
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"git.trj.tw/golang/mtfosbot/module/apis"
"git.trj.tw/golang/mtfosbot/module/config"
)
2018-09-11 09:58:08 +00:00
// TextMessage - line text message object
2018-09-10 10:13:27 +00:00
type TextMessage struct {
Type string `json:"type"`
Text string `json:"text"`
}
2018-09-11 09:58:08 +00:00
// ImageMessage - line image message object
2018-09-10 10:13:27 +00:00
type ImageMessage struct {
Type string `json:"type"`
2018-09-11 09:58:08 +00:00
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
2018-09-10 10:13:27 +00:00
}
type pushBody struct {
To string `json:"to"`
Messages []interface{} `json:"messages"`
}
type replyBody struct {
ReplyToken string `json:"replyToken"`
Messages []interface{} `json:"messages"`
}
2018-09-11 09:58:08 +00:00
var baseURL = "https://api.line.me/"
2018-09-10 10:13:27 +00:00
2018-09-11 09:58:08 +00:00
func getURL(p string) (string, bool) {
u, err := url.Parse(baseURL)
2018-09-10 10:13:27 +00:00
if err != nil {
return "", false
}
ref, err := u.Parse(p)
if err != nil {
return "", false
}
str := ref.String()
return str, true
}
func getHeaders() map[string]string {
m := make(map[string]string)
conf := config.GetConf()
m["Content-Type"] = "application/json"
m["Authorization"] = fmt.Sprintf("Bearer %s", conf.Line.Access)
return m
}
// PushMessage -
func PushMessage(target string, message interface{}) {
2018-09-10 15:12:18 +00:00
if len(target) == 0 {
return
}
2018-09-10 10:13:27 +00:00
url := "/v2/bot/message/push"
body := &pushBody{
To: target,
}
switch message.(type) {
case ImageMessage:
2018-09-10 13:54:01 +00:00
m := (message.(ImageMessage))
m.Type = "image"
message = m
2018-09-10 10:13:27 +00:00
break
case TextMessage:
2018-09-10 13:54:01 +00:00
m := (message.(TextMessage))
m.Type = "text"
message = m
2018-09-10 10:13:27 +00:00
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)
2018-09-11 09:58:08 +00:00
apiURL, ok := getURL(url)
2018-09-10 10:13:27 +00:00
if !ok {
fmt.Println("url parser fail")
return
}
reqObj := apis.RequestObj{
Method: "POST",
2018-09-11 16:29:38 +00:00
URL: apiURL,
2018-09-10 10:13:27 +00:00
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
}
}
2018-09-10 15:12:18 +00:00
2018-09-11 09:58:08 +00:00
// ReplyMessage -
func ReplyMessage(replyToken string, message interface{}) {
2018-09-10 15:12:18 +00:00
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)
2018-09-11 09:58:08 +00:00
apiURL, ok := getURL(url)
2018-09-10 15:12:18 +00:00
if !ok {
fmt.Println("url parser fail")
return
}
reqObj := apis.RequestObj{
Method: "POST",
2018-09-11 16:29:38 +00:00
URL: apiURL,
2018-09-10 15:12:18 +00:00
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
}
}