Skip to content

Commit

Permalink
Merge pull request #5 from juunini/develope
Browse files Browse the repository at this point in the history
change function "send"
  • Loading branch information
juunini authored Aug 22, 2020
2 parents be28af2 + be786de commit 923f4f8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 60 deletions.
20 changes: 15 additions & 5 deletions notify/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}
22 changes: 7 additions & 15 deletions notify/send_image.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 1 addition & 12 deletions notify/send_local_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
)
Expand All @@ -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
}

Expand Down
22 changes: 7 additions & 15 deletions notify/send_sticker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package notify

import (
"net/http"
"net/url"
"strconv"
"strings"
Expand All @@ -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
}
18 changes: 5 additions & 13 deletions notify/send_text.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 923f4f8

Please sign in to comment.