Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIFF API and Flex Message API #83

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions linebot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const (
APIEndpointUnlinkUserRichMenu = "/v2/bot/user/%s/richmenu"
APIEndpointDownloadRichMenuImage = "/v2/bot/richmenu/%s/content" // Download: GET / Upload: POST
APIEndpointUploadRichMenuImage = "/v2/bot/richmenu/%s/content" // Download: GET / Upload: POST

APIEndpointGetLIFFAPP = "/liff/v1/apps"
APIEndpointAddLIFFAPP = "/liff/v1/apps"
APIEndpointUpdateLIFFAPP = "/liff/v1/apps/%s/view"
APIEndpointDeleteLIFFAPP = "/liff/v1/apps/%s"
)

// Client type
Expand Down Expand Up @@ -154,6 +159,15 @@ func (client *Client) post(ctx context.Context, endpoint string, body io.Reader)
return client.do(ctx, req)
}

func (client *Client) put(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("PUT", client.url(endpoint), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
return client.do(ctx, req)
}

func (client *Client) delete(ctx context.Context, endpoint string) (*http.Response, error) {
req, err := http.NewRequest("DELETE", client.url(endpoint), nil)
if err != nil {
Expand Down
204 changes: 204 additions & 0 deletions linebot/liff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package linebot

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
)

// LIFFViewType type
type LIFFViewType string

// LIFFViewType constants
const (
LIFFViewTypeCompact LIFFViewType = "compact"
LIFFViewTypeTail LIFFViewType = "tail"
LIFFViewTypeFull LIFFViewType = "full"
)

// LIFFIDResponse type
type LIFFIDResponse struct {
LIFFID string `json:"liffId"`
}

type LIFFAPP struct {
LIFFID string `json:"liffId"`
View View `json:"view"`
}

type ViewRequest struct {
View View `json:"view"`
}

type View struct {
Type LIFFViewType `json:"type"`
Url string `json:"url"`
}

// GetRichMenu method
func (client *Client) GetLIFF() *GetLIFFAllCall {
return &GetLIFFAllCall{
c: client,
}
}

//GetLIFFAllCall type
type GetLIFFAllCall struct {
c *Client
ctx context.Context
}

// WithContext method
func (call *GetLIFFAllCall) WithContext(ctx context.Context) *GetLIFFAllCall {
call.ctx = ctx
return call
}

// Do method
func (call *GetLIFFAllCall) Do() (*LIFFResponse, error) {
res, err := call.c.get(call.ctx, APIEndpointGetLIFFAPP, nil)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToLIFFResponse(res)
}

// AddLIFFCall method
func (client *Client) AddLIFF(view View) *AddLIFFCall {
return &AddLIFFCall{
c: client,
View: view,
}
}

//AddLIFFCall type
type AddLIFFCall struct {
c *Client
ctx context.Context

View View
}

// WithContext method
func (call *AddLIFFCall) WithContext(ctx context.Context) *AddLIFFCall {
call.ctx = ctx
return call
}

func (call *AddLIFFCall) encodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
View View `json:"view"`
}{
View: call.View,
})
}

// Do method
func (call *AddLIFFCall) Do() (*LIFFIDResponse, error) {
var buf bytes.Buffer
if err := call.encodeJSON(&buf); err != nil {
return nil, err
}
res, err := call.c.post(call.ctx, APIEndpointAddLIFFAPP, &buf)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToLIFFIDResponse(res)
}

// DeleteRichMenu method
func (client *Client) UpdateLIFFCall(liffId string, view View) *UpdateLIFFCall {
return &UpdateLIFFCall{
c: client,
LIFFID: liffId,
view: view,
}
}

//UpdateLIFFCall type
type UpdateLIFFCall struct {
c *Client
ctx context.Context

LIFFID string
view View
}

// WithContext method
func (call *UpdateLIFFCall) WithContext(ctx context.Context) *UpdateLIFFCall {
call.ctx = ctx
return call
}

func (call *UpdateLIFFCall) encodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
Type LIFFViewType `json:"type"`
Url string `json:"url"`
}{
Type: call.view.Type,
Url: call.view.Url,
})
}

// Do method
func (call *UpdateLIFFCall) Do() (*BasicResponse, error) {
var buf bytes.Buffer
if err := call.encodeJSON(&buf); err != nil {
return nil, err
}

endpoint := fmt.Sprintf(APIEndpointUpdateLIFFAPP, call.LIFFID)
res, err := call.c.put(call.ctx, endpoint, &buf)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToBasicResponse(res)
}

// DeleteRichMenu method
func (client *Client) DeleteLIFFCall(liffId string) *DeleteLIFFCall {
return &DeleteLIFFCall{
c: client,
LIFFID: liffId,
}
}

//DeleteLIFFCall type
type DeleteLIFFCall struct {
c *Client
ctx context.Context

LIFFID string
}

// WithContext method
func (call *DeleteLIFFCall) WithContext(ctx context.Context) *DeleteLIFFCall {
call.ctx = ctx
return call
}

// Do method
func (call *DeleteLIFFCall) Do() (*BasicResponse, error) {
endpoint := fmt.Sprintf(APIEndpointDeleteLIFFAPP, call.LIFFID)
res, err := call.c.delete(call.ctx, endpoint)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToBasicResponse(res)
}
36 changes: 35 additions & 1 deletion linebot/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ type RichMenuResponse struct {
Areas []AreaDetail `json:"areas"`
}

// LIFFResponse type
type LIFFResponse struct {
Apps []LIFFAPP `json:"apps"`
}

func checkResponse(res *http.Response) error {
if res.StatusCode != http.StatusOK {
decoder := json.NewDecoder(res.Body)
Expand All @@ -95,7 +100,12 @@ func decodeToBasicResponse(res *http.Response) (*BasicResponse, error) {
decoder := json.NewDecoder(res.Body)
result := BasicResponse{}
if err := decoder.Decode(&result); err != nil {
return nil, err
switch {
case err == io.EOF:
return &result, nil
case err != nil:
return nil, err
}
}
return &result, nil
}
Expand Down Expand Up @@ -173,3 +183,27 @@ func decodeToRichMenuIDResponse(res *http.Response) (*RichMenuIDResponse, error)
}
return &result, nil
}

func decodeToLIFFResponse(res *http.Response) (*LIFFResponse, error) {
if err := checkResponse(res); err != nil {
return nil, err
}
decoder := json.NewDecoder(res.Body)
result := &LIFFResponse{}
if err := decoder.Decode(result); err != nil {
return nil, err
}
return result, nil
}

func decodeToLIFFIDResponse(res *http.Response) (*LIFFIDResponse, error) {
if err := checkResponse(res); err != nil {
return nil, err
}
decoder := json.NewDecoder(res.Body)
result := LIFFIDResponse{}
if err := decoder.Decode(&result); err != nil {
return nil, err
}
return &result, nil
}