mtfosbot/module/apis/line/line.go

290 lines
5.1 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-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"
"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
}
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
}
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-19 16:17:00 +00:00
urlPath := "/v2/bot/message/push"
2018-09-10 10:13:27 +00:00
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 {
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 {
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 {
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
}
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,
}
switch message.(type) {
2018-09-20 17:14:08 +00:00
case *ImageMessage:
m := (message.(*ImageMessage))
2018-09-10 15:12:18 +00:00
m.Type = "image"
message = m
break
2018-09-20 17:14:08 +00:00
case *TextMessage:
m := (message.(*TextMessage))
2018-09-10 15:12:18 +00:00
m.Type = "text"
message = m
break
default:
2018-09-20 17:14:08 +00:00
fmt.Println("input type error")
2018-09-10 15:12:18 +00:00
return
}
body.Messages = append(body.Messages, message)
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,
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 {
return
}
}
2018-09-19 16:17:00 +00:00
// GetUserInfo -
func GetUserInfo(u, g string) (user *LineUserInfo, err error) {
urlPath := fmt.Sprintf("/v2/bot/group/%s/member/%s", g, u)
header := getHeaders()
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 -
func GetContentHead(id string) (mime string, err error) {
urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id)
header := getHeaders()
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 -
func DownloadContent(id string, w io.Writer) (err error) {
urlPath := fmt.Sprintf("/v2/bot/message/%s/content", id)
header := getHeaders()
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
}