mtfosbot/module/apis/line/line.go

324 lines
6.0 KiB
Go
Raw Normal View History

2018-09-10 10:13:27 +00:00
package line
import (
"bytes"
"encoding/json"
2018-09-19 16:17:00 +00:00
"errors"
2018-09-10 10:13:27 +00:00
"fmt"
2018-10-01 16:11:32 +00:00
"io"
2018-09-19 16:17:00 +00:00
"io/ioutil"
2018-12-06 02:56:20 +00:00
"log"
2018-09-10 10:13:27 +00:00
"net/http"
"net/url"
2018-09-19 16:17:00 +00:00
"strings"
2018-09-10 10:13:27 +00:00
"git.trj.tw/golang/mtfosbot/module/apis"
)
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
}
2018-12-14 07:55:49 +00:00
// VideoMessage - line video message object
type VideoMessage struct {
Type string `json:"type"`
OriginalContentURL string `json:"OriginalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
}
2018-09-19 16:17:00 +00:00
// LineUserInfo -
type LineUserInfo struct {
DisplayName string `json:"displayName"`
UserID string `json:"userId"`
}
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
}
2019-06-25 09:02:03 +00:00
func getHeaders(token string) map[string]string {
2018-09-10 10:13:27 +00:00
m := make(map[string]string)
m["Content-Type"] = "application/json"
2019-06-25 09:02:03 +00:00
m["Authorization"] = fmt.Sprintf("Bearer %s", token)
2018-09-10 10:13:27 +00:00
return m
}
2018-12-14 07:55:49 +00:00
func checkMessageObject(m interface{}) interface{} {
if m == nil {
return nil
}
var obj interface{}
switch m.(type) {
case ImageMessage:
tmp := (m.(ImageMessage))
tmp.Type = "image"
obj = tmp
break
case TextMessage:
tmp := (m.(TextMessage))
tmp.Type = "text"
obj = tmp
break
case VideoMessage:
tmp := (m.(VideoMessage))
tmp.Type = "video"
obj = tmp
break
default:
return nil
}
return obj
}
2018-09-10 10:13:27 +00:00
// PushMessage -
2019-06-25 09:02:03 +00:00
func PushMessage(accessToken, target string, message ...interface{}) {
2018-12-06 03:05:39 +00:00
log.Println("push target :::: ", target)
if len(target) == 0 || len(message) == 0 {
2018-09-10 15:12:18 +00:00
return
}
2018-09-19 16:17:00 +00:00
urlPath := "/v2/bot/message/push"
2018-09-10 10:13:27 +00:00
body := &pushBody{
To: target,
}
checked := make([]interface{}, 0)
for _, v := range message {
tmp := checkMessageObject(v)
if tmp == nil {
continue
}
checked = append(checked, tmp)
}
2018-12-14 07:55:49 +00:00
body.Messages = append(body.Messages, checked...)
if len(body.Messages) > 5 {
body.Messages = body.Messages[:5]
}
2018-09-10 10:13:27 +00:00
dataByte, err := json.Marshal(body)
if err != nil {
2018-12-06 03:05:39 +00:00
log.Println("to json error ::::", err)
2018-09-10 10:13:27 +00:00
return
}
byteReader := bytes.NewReader(dataByte)
2018-09-19 16:17:00 +00:00
apiURL, ok := getURL(urlPath)
2018-09-10 10:13:27 +00:00
if !ok {
2018-12-06 03:05:39 +00:00
log.Println("get url fail ::::::")
2018-09-10 10:13:27 +00:00
return
}
reqObj := apis.RequestObj{
Method: "POST",
2018-09-11 16:29:38 +00:00
URL: apiURL,
2019-06-25 09:02:03 +00:00
Headers: getHeaders(accessToken),
2018-09-10 10:13:27 +00:00
Body: byteReader,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
2018-12-06 03:05:39 +00:00
log.Println("get req fail :::::: ", err)
2018-09-10 10:13:27 +00:00
return
}
_, err = http.DefaultClient.Do(req)
if err != nil {
2018-12-06 02:56:20 +00:00
log.Println("send to line error :::: ", err)
2018-09-10 10:13:27 +00:00
return
}
}
2018-09-10 15:12:18 +00:00
2018-09-11 09:58:08 +00:00
// ReplyMessage -
2019-06-25 09:02:03 +00:00
func ReplyMessage(accessToken, replyToken string, message ...interface{}) {
if len(replyToken) == 0 || len(message) == 0 {
2018-09-10 15:12:18 +00:00
return
}
2018-09-19 16:17:00 +00:00
urlPath := "/v2/bot/message/reply"
2018-09-10 15:12:18 +00:00
body := &replyBody{
ReplyToken: replyToken,
}
checked := make([]interface{}, 0)
for _, v := range message {
tmp := checkMessageObject(v)
if tmp == nil {
continue
}
checked = append(checked, tmp)
}
2018-09-10 15:12:18 +00:00
body.Messages = append(body.Messages, checked...)
if len(body.Messages) > 5 {
body.Messages = body.Messages[:5]
}
2018-09-10 15:12:18 +00:00
dataByte, err := json.Marshal(body)
if err != nil {
return
}
byteReader := bytes.NewReader(dataByte)
2018-09-19 16:17:00 +00:00
apiURL, ok := getURL(urlPath)
2018-09-10 15:12:18 +00:00
if !ok {
return
}
reqObj := apis.RequestObj{
Method: "POST",
2018-09-11 16:29:38 +00:00
URL: apiURL,
2019-06-25 09:02:03 +00:00
Headers: getHeaders(accessToken),
2018-09-10 15:12:18 +00:00
Body: byteReader,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
return
}
_, err = http.DefaultClient.Do(req)
if err != nil {
return
}
}
2018-09-19 16:17:00 +00:00
// GetUserInfo -
2019-06-25 09:02:03 +00:00
func GetUserInfo(accessToken, u, g string) (user *LineUserInfo, err error) {
2018-09-19 16:17:00 +00:00
urlPath := fmt.Sprintf("/v2/bot/group/%s/member/%s", g, u)
2019-06-25 09:02:03 +00:00
header := getHeaders(accessToken)
2018-09-19 16:17:00 +00:00
apiURL, ok := getURL(urlPath)
if !ok {
return nil, errors.New("url parser fail")
}
reqObj := apis.RequestObj{
Method: "GET",
URL: apiURL,
Headers: header,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("api response not 200")
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
return nil, errors.New("response body not json")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(bodyBytes, &user)
if err != nil {
return nil, err
}
return
}
2018-10-01 16:11:32 +00:00
// GetContentHead -
2019-06-25 09:02:03 +00:00
func GetContentHead(accessToken, id string) (mime string, err error) {
2018-10-01 16:11:32 +00:00
urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id)
2019-06-25 09:02:03 +00:00
header := getHeaders(accessToken)
2018-10-01 16:11:32 +00:00
u, ok := getURL(urlPath)
if !ok {
return "", errors.New("get url fail")
}
reqObj := apis.RequestObj{
Method: "HEAD",
URL: u,
Headers: header,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
mime = resp.Header.Get("Content-Type")
return
}
// DownloadContent -
2019-06-25 09:02:03 +00:00
func DownloadContent(accessToken, id string, w io.Writer) (err error) {
2018-10-01 16:11:32 +00:00
urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id)
2019-06-25 09:02:03 +00:00
header := getHeaders(accessToken)
2018-10-01 16:11:32 +00:00
u, ok := getURL(urlPath)
if !ok {
return errors.New("get url fail")
}
reqObj := apis.RequestObj{
Method: "GET",
URL: u,
Headers: header,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(w, resp.Body)
return
}