diff --git a/notify/send.go b/notify/send.go index f1a6a28..4544fd8 100644 --- a/notify/send.go +++ b/notify/send.go @@ -3,12 +3,22 @@ package notify import ( "encoding/json" "fmt" + "io" "net/http" ) const lineNotifyApiURL = "https://notify-api.line.me/api/notify" -func sendToLineServer(req *http.Request, accessToken string) (err error) { +func sendToLineServer(body io.Reader, accessToken, contentType string) (err error) { + req, err := http.NewRequest( + "POST", + lineNotifyApiURL, + body, + ) + if err != nil { + return + } + req.Header.Set("Content-Type", contentType) req.Header.Set("Authorization", "Bearer "+accessToken) client := &http.Client{} @@ -21,18 +31,18 @@ func sendToLineServer(req *http.Request, accessToken string) (err error) { return } - var body struct { + var responseBody struct { Status int `json:"status"` Message string `json:"message"` } - if err = json.NewDecoder(res.Body).Decode(&body); err != nil { + if err = json.NewDecoder(res.Body).Decode(&responseBody); err != nil { return } defer res.Body.Close() - if body.Status != 200 { - err = fmt.Errorf("%d: %s", body.Status, body.Message) + if responseBody.Status != 200 { + err = fmt.Errorf("%d: %s", responseBody.Status, responseBody.Message) } return } diff --git a/notify/send_image.go b/notify/send_image.go index 30452a6..9121446 100644 --- a/notify/send_image.go +++ b/notify/send_image.go @@ -1,27 +1,19 @@ package notify import ( - "net/http" "net/url" "strings" ) // SendImage : send line notify simple text with image func SendImage(accessToken, message, imageURL string) (err error) { - req, err := http.NewRequest( - "POST", - lineNotifyApiURL, - strings.NewReader(url.Values{ - "message": []string{message}, - "imageThumbnail": []string{imageURL}, - "imageFullsize": []string{imageURL}, - }.Encode()), - ) - if err != nil { - return - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + body := strings.NewReader(url.Values{ + "message": []string{message}, + "imageThumbnail": []string{imageURL}, + "imageFullsize": []string{imageURL}, + }.Encode()) + contentType := "application/x-www-form-urlencoded" - err = sendToLineServer(req, accessToken) + err = sendToLineServer(body, accessToken, contentType) return } diff --git a/notify/send_local_image.go b/notify/send_local_image.go index 0086af2..87bb4b7 100644 --- a/notify/send_local_image.go +++ b/notify/send_local_image.go @@ -4,7 +4,6 @@ import ( "bytes" "io" "mime/multipart" - "net/http" "os" "strings" ) @@ -13,17 +12,7 @@ import ( func SendLocalImage(accessToken, message, imagePath string) (err error) { body, contentType, err := makeMultipartBody(message, imagePath) - req, err := http.NewRequest( - "POST", - lineNotifyApiURL, - &body, - ) - if err != nil { - return - } - req.Header.Set("Content-Type", contentType) - - err = sendToLineServer(req, accessToken) + err = sendToLineServer(&body, accessToken, contentType) return } diff --git a/notify/send_sticker.go b/notify/send_sticker.go index 8e8e2c8..145a002 100644 --- a/notify/send_sticker.go +++ b/notify/send_sticker.go @@ -1,7 +1,6 @@ package notify import ( - "net/http" "net/url" "strconv" "strings" @@ -13,20 +12,13 @@ import ( // // https://devdocs.line.me/files/sticker_list.pdf func SendSticker(accessToken, message string, stickerPackageId, stickerId int) (err error) { - req, err := http.NewRequest( - "POST", - lineNotifyApiURL, - strings.NewReader(url.Values{ - "message": []string{message}, - "stickerPackageId": []string{strconv.Itoa(stickerPackageId)}, - "stickerId": []string{strconv.Itoa(stickerId)}, - }.Encode()), - ) - if err != nil { - return - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + body := strings.NewReader(url.Values{ + "message": []string{message}, + "stickerPackageId": []string{strconv.Itoa(stickerPackageId)}, + "stickerId": []string{strconv.Itoa(stickerId)}, + }.Encode()) + contentType := "application/x-www-form-urlencoded" - err = sendToLineServer(req, accessToken) + err = sendToLineServer(body, accessToken, contentType) return } diff --git a/notify/send_text.go b/notify/send_text.go index bb597b0..e56a6c3 100644 --- a/notify/send_text.go +++ b/notify/send_text.go @@ -1,25 +1,17 @@ package notify import ( - "net/http" "net/url" "strings" ) // SendText : send line notify simple text func SendText(accessToken, message string) (err error) { - req, err := http.NewRequest( - "POST", - lineNotifyApiURL, - strings.NewReader(url.Values{ - "message": []string{message}, - }.Encode()), - ) - if err != nil { - return - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + body := strings.NewReader(url.Values{ + "message": []string{message}, + }.Encode()) + contentType := "application/x-www-form-urlencoded" - err = sendToLineServer(req, accessToken) + err = sendToLineServer(body, accessToken, contentType) return }