github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/liff.go (about)

     1  // Copyright 2018 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package linebot
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  )
    24  
    25  // LIFFViewType type
    26  type LIFFViewType string
    27  
    28  // LIFFViewType constants
    29  const (
    30  	LIFFViewTypeCompact LIFFViewType = "compact"
    31  	LIFFViewTypeTall    LIFFViewType = "tall"
    32  	LIFFViewTypeFull    LIFFViewType = "full"
    33  )
    34  
    35  // LIFFViewScopeType type
    36  type LIFFViewScopeType string
    37  
    38  // LIFFViewScopeType constants
    39  const (
    40  	LIFFViewScopeTypeOpenID           LIFFViewScopeType = "openid"
    41  	LIFFViewScopeTypeEmail            LIFFViewScopeType = "email"
    42  	LIFFViewScopeTypeProfile          LIFFViewScopeType = "profile"
    43  	LIFFViewScopeTypeChatMessageWrite LIFFViewScopeType = "chat_message.write"
    44  )
    45  
    46  // LIFFApp type
    47  type LIFFApp struct {
    48  	LIFFID               string              `json:"liffId"`
    49  	View                 View                `json:"view"`
    50  	Description          string              `json:"description,omitempty"`
    51  	Features             *LIFFAppFeatures    `json:"features,omitempty"`
    52  	PermanentLinkPattern string              `json:"permanentLinkPattern,omitempty"`
    53  	Scope                []LIFFViewScopeType `json:"scope,omitempty"`
    54  	BotPrompt            string              `json:"botprompt,omitempty"`
    55  }
    56  
    57  // View type
    58  type View struct {
    59  	Type       LIFFViewType `json:"type"`
    60  	URL        string       `json:"url"`
    61  	ModlueMode bool         `json:"moduleMode,omitempty"`
    62  }
    63  
    64  // LIFFAppFeatures type
    65  type LIFFAppFeatures struct {
    66  	BLE    bool `json:"ble,omitempty"`
    67  	QRCode bool `json:"qrCode,omitempty"`
    68  }
    69  
    70  // GetLIFF method
    71  func (client *Client) GetLIFF() *GetLIFFAllCall {
    72  	return &GetLIFFAllCall{
    73  		c: client,
    74  	}
    75  }
    76  
    77  // GetLIFFAllCall type
    78  type GetLIFFAllCall struct {
    79  	c   *Client
    80  	ctx context.Context
    81  }
    82  
    83  // WithContext method
    84  func (call *GetLIFFAllCall) WithContext(ctx context.Context) *GetLIFFAllCall {
    85  	call.ctx = ctx
    86  	return call
    87  }
    88  
    89  // Do method
    90  func (call *GetLIFFAllCall) Do() (*LIFFAppsResponse, error) {
    91  	res, err := call.c.get(call.ctx, call.c.endpointBase, APIEndpointGetAllLIFFApps, nil)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	defer closeResponse(res)
    96  	return decodeToLIFFResponse(res)
    97  }
    98  
    99  // AddLIFF method
   100  func (client *Client) AddLIFF(app LIFFApp) *AddLIFFCall {
   101  	return &AddLIFFCall{
   102  		c:   client,
   103  		app: app,
   104  	}
   105  }
   106  
   107  // AddLIFFCall type
   108  type AddLIFFCall struct {
   109  	c   *Client
   110  	ctx context.Context
   111  
   112  	app LIFFApp
   113  }
   114  
   115  // WithContext method
   116  func (call *AddLIFFCall) WithContext(ctx context.Context) *AddLIFFCall {
   117  	call.ctx = ctx
   118  	return call
   119  }
   120  
   121  func (call *AddLIFFCall) encodeJSON(w io.Writer) error {
   122  	enc := json.NewEncoder(w)
   123  	return enc.Encode(&struct {
   124  		View                 View                `json:"view"`
   125  		Description          string              `json:"description,omitempty"`
   126  		Features             *LIFFAppFeatures    `json:"features,omitempty"`
   127  		PermanentLinkPattern string              `json:"permanentLinkPattern,omitempty"`
   128  		Scope                []LIFFViewScopeType `json:"scope,omitempty"`
   129  		BotPrompt            string              `json:"botPrompt,omitempty"`
   130  	}{
   131  		View:                 call.app.View,
   132  		Description:          call.app.Description,
   133  		Features:             call.app.Features,
   134  		PermanentLinkPattern: call.app.PermanentLinkPattern,
   135  		Scope:                call.app.Scope,
   136  		BotPrompt:            call.app.BotPrompt,
   137  	})
   138  }
   139  
   140  // Do method
   141  func (call *AddLIFFCall) Do() (*LIFFIDResponse, error) {
   142  	var buf bytes.Buffer
   143  	if err := call.encodeJSON(&buf); err != nil {
   144  		return nil, err
   145  	}
   146  	res, err := call.c.post(call.ctx, APIEndpointAddLIFFApp, &buf)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	defer closeResponse(res)
   151  	return decodeToLIFFIDResponse(res)
   152  }
   153  
   154  // UpdateLIFF method
   155  func (client *Client) UpdateLIFF(liffID string, app LIFFApp) *UpdateLIFFCall {
   156  	return &UpdateLIFFCall{
   157  		c:      client,
   158  		liffID: liffID,
   159  		app:    app,
   160  	}
   161  }
   162  
   163  // UpdateLIFFCall type
   164  type UpdateLIFFCall struct {
   165  	c   *Client
   166  	ctx context.Context
   167  
   168  	liffID string
   169  	app    LIFFApp
   170  }
   171  
   172  // WithContext method
   173  func (call *UpdateLIFFCall) WithContext(ctx context.Context) *UpdateLIFFCall {
   174  	call.ctx = ctx
   175  	return call
   176  }
   177  
   178  func (call *UpdateLIFFCall) encodeJSON(w io.Writer) error {
   179  	enc := json.NewEncoder(w)
   180  	return enc.Encode(&struct {
   181  		View                 View                `json:"view"`
   182  		Description          string              `json:"description,omitempty"`
   183  		Features             *LIFFAppFeatures    `json:"features,omitempty"`
   184  		PermanentLinkPattern string              `json:"permanentLinkPattern,omitempty"`
   185  		Scope                []LIFFViewScopeType `json:"scope,omitempty"`
   186  		BotPrompt            string              `json:"botPrompt,omitempty"`
   187  	}{
   188  		View:                 call.app.View,
   189  		Description:          call.app.Description,
   190  		Features:             call.app.Features,
   191  		PermanentLinkPattern: call.app.PermanentLinkPattern,
   192  		Scope:                call.app.Scope,
   193  		BotPrompt:            call.app.BotPrompt,
   194  	})
   195  }
   196  
   197  // Do method
   198  func (call *UpdateLIFFCall) Do() (*BasicResponse, error) {
   199  	var buf bytes.Buffer
   200  	if err := call.encodeJSON(&buf); err != nil {
   201  		return nil, err
   202  	}
   203  
   204  	endpoint := fmt.Sprintf(APIEndpointUpdateLIFFApp, call.liffID)
   205  	res, err := call.c.put(call.ctx, endpoint, &buf)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	defer closeResponse(res)
   210  	return decodeToBasicResponse(res)
   211  }
   212  
   213  // DeleteLIFF method
   214  func (client *Client) DeleteLIFF(liffID string) *DeleteLIFFCall {
   215  	return &DeleteLIFFCall{
   216  		c:      client,
   217  		liffID: liffID,
   218  	}
   219  }
   220  
   221  // DeleteLIFFCall type
   222  type DeleteLIFFCall struct {
   223  	c   *Client
   224  	ctx context.Context
   225  
   226  	liffID string
   227  }
   228  
   229  // WithContext method
   230  func (call *DeleteLIFFCall) WithContext(ctx context.Context) *DeleteLIFFCall {
   231  	call.ctx = ctx
   232  	return call
   233  }
   234  
   235  // Do method
   236  func (call *DeleteLIFFCall) Do() (*BasicResponse, error) {
   237  	endpoint := fmt.Sprintf(APIEndpointDeleteLIFFApp, call.liffID)
   238  	res, err := call.c.delete(call.ctx, endpoint)
   239  	if err != nil {
   240  		return nil, err
   241  	}
   242  	defer closeResponse(res)
   243  	return decodeToBasicResponse(res)
   244  }