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

     1  // Copyright 2016 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  	"encoding/json"
    19  )
    20  
    21  // TemplateType type
    22  type TemplateType string
    23  
    24  // TemplateType constants
    25  const (
    26  	TemplateTypeButtons       TemplateType = "buttons"
    27  	TemplateTypeConfirm       TemplateType = "confirm"
    28  	TemplateTypeCarousel      TemplateType = "carousel"
    29  	TemplateTypeImageCarousel TemplateType = "image_carousel"
    30  )
    31  
    32  // ImageAspectRatioType type
    33  type ImageAspectRatioType string
    34  
    35  // ImageAspectRatioType constants
    36  const (
    37  	ImageAspectRatioTypeRectangle ImageAspectRatioType = "rectangle"
    38  	ImageAspectRatioTypeSquare    ImageAspectRatioType = "square"
    39  )
    40  
    41  // ImageSizeType type
    42  type ImageSizeType string
    43  
    44  // ImageSizeType constants
    45  const (
    46  	ImageSizeTypeCover   ImageSizeType = "cover"
    47  	ImageSizeTypeContain ImageSizeType = "contain"
    48  )
    49  
    50  // Template interface
    51  type Template interface {
    52  	json.Marshaler
    53  	Template()
    54  }
    55  
    56  // ButtonsTemplate type
    57  type ButtonsTemplate struct {
    58  	ThumbnailImageURL    string
    59  	ImageAspectRatio     ImageAspectRatioType
    60  	ImageSize            ImageSizeType
    61  	ImageBackgroundColor string
    62  	Title                string
    63  	Text                 string
    64  	Actions              []TemplateAction
    65  	DefaultAction        TemplateAction
    66  }
    67  
    68  // MarshalJSON method of ButtonsTemplate
    69  func (t *ButtonsTemplate) MarshalJSON() ([]byte, error) {
    70  	return json.Marshal(&struct {
    71  		Type                 TemplateType         `json:"type"`
    72  		ThumbnailImageURL    string               `json:"thumbnailImageUrl,omitempty"`
    73  		ImageAspectRatio     ImageAspectRatioType `json:"imageAspectRatio,omitempty"`
    74  		ImageSize            ImageSizeType        `json:"imageSize,omitempty"`
    75  		ImageBackgroundColor string               `json:"imageBackgroundColor,omitempty"`
    76  		Title                string               `json:"title,omitempty"`
    77  		Text                 string               `json:"text"`
    78  		Actions              []TemplateAction     `json:"actions"`
    79  		DefaultAction        TemplateAction       `json:"defaultAction,omitempty"`
    80  	}{
    81  		Type:                 TemplateTypeButtons,
    82  		ThumbnailImageURL:    t.ThumbnailImageURL,
    83  		ImageAspectRatio:     t.ImageAspectRatio,
    84  		ImageSize:            t.ImageSize,
    85  		ImageBackgroundColor: t.ImageBackgroundColor,
    86  		Title:                t.Title,
    87  		Text:                 t.Text,
    88  		Actions:              t.Actions,
    89  		DefaultAction:        t.DefaultAction,
    90  	})
    91  }
    92  
    93  // WithImageOptions method, ButtonsTemplate can set imageAspectRatio, imageSize and imageBackgroundColor
    94  func (t *ButtonsTemplate) WithImageOptions(imageAspectRatio ImageAspectRatioType, imageSize ImageSizeType, imageBackgroundColor string) *ButtonsTemplate {
    95  	t.ImageAspectRatio = imageAspectRatio
    96  	t.ImageSize = imageSize
    97  	t.ImageBackgroundColor = imageBackgroundColor
    98  	return t
    99  }
   100  
   101  // WithDefaultAction method, ButtonsTemplate can set defaultAction
   102  func (t *ButtonsTemplate) WithDefaultAction(defaultAction TemplateAction) *ButtonsTemplate {
   103  	t.DefaultAction = defaultAction
   104  	return t
   105  }
   106  
   107  // ConfirmTemplate type
   108  type ConfirmTemplate struct {
   109  	Text    string
   110  	Actions []TemplateAction
   111  }
   112  
   113  // MarshalJSON method of ConfirmTemplate
   114  func (t *ConfirmTemplate) MarshalJSON() ([]byte, error) {
   115  	return json.Marshal(&struct {
   116  		Type    TemplateType     `json:"type"`
   117  		Text    string           `json:"text"`
   118  		Actions []TemplateAction `json:"actions"`
   119  	}{
   120  		Type:    TemplateTypeConfirm,
   121  		Text:    t.Text,
   122  		Actions: t.Actions,
   123  	})
   124  }
   125  
   126  // CarouselTemplate type
   127  type CarouselTemplate struct {
   128  	Columns          []*CarouselColumn
   129  	ImageAspectRatio ImageAspectRatioType
   130  	ImageSize        ImageSizeType
   131  }
   132  
   133  // CarouselColumn type
   134  type CarouselColumn struct {
   135  	ThumbnailImageURL    string           `json:"thumbnailImageUrl,omitempty"`
   136  	ImageBackgroundColor string           `json:"imageBackgroundColor,omitempty"`
   137  	Title                string           `json:"title,omitempty"`
   138  	Text                 string           `json:"text"`
   139  	Actions              []TemplateAction `json:"actions"`
   140  	DefaultAction        TemplateAction   `json:"defaultAction,omitempty"`
   141  }
   142  
   143  // MarshalJSON method of CarouselTemplate
   144  func (t *CarouselTemplate) MarshalJSON() ([]byte, error) {
   145  	return json.Marshal(&struct {
   146  		Type             TemplateType         `json:"type"`
   147  		Columns          []*CarouselColumn    `json:"columns"`
   148  		ImageAspectRatio ImageAspectRatioType `json:"imageAspectRatio,omitempty"`
   149  		ImageSize        ImageSizeType        `json:"imageSize,omitempty"`
   150  	}{
   151  		Type:             TemplateTypeCarousel,
   152  		Columns:          t.Columns,
   153  		ImageAspectRatio: t.ImageAspectRatio,
   154  		ImageSize:        t.ImageSize,
   155  	})
   156  }
   157  
   158  // WithImageOptions method, CarouselTemplate can set imageAspectRatio and imageSize
   159  func (t *CarouselTemplate) WithImageOptions(imageAspectRatio ImageAspectRatioType, imageSize ImageSizeType) *CarouselTemplate {
   160  	t.ImageAspectRatio = imageAspectRatio
   161  	t.ImageSize = imageSize
   162  	return t
   163  }
   164  
   165  // WithImageOptions method, CarouselColumn can set imageBackgroundColor
   166  func (t *CarouselColumn) WithImageOptions(imageBackgroundColor string) *CarouselColumn {
   167  	t.ImageBackgroundColor = imageBackgroundColor
   168  	return t
   169  }
   170  
   171  // WithDefaultAction method, CarouselColumn can set defaultAction
   172  func (t *CarouselColumn) WithDefaultAction(defaultAction TemplateAction) *CarouselColumn {
   173  	t.DefaultAction = defaultAction
   174  	return t
   175  }
   176  
   177  // ImageCarouselTemplate type
   178  type ImageCarouselTemplate struct {
   179  	Columns []*ImageCarouselColumn
   180  }
   181  
   182  // ImageCarouselColumn type
   183  type ImageCarouselColumn struct {
   184  	ImageURL string         `json:"imageUrl"`
   185  	Action   TemplateAction `json:"action"`
   186  }
   187  
   188  // MarshalJSON method of ImageCarouselTemplate
   189  func (t *ImageCarouselTemplate) MarshalJSON() ([]byte, error) {
   190  	return json.Marshal(&struct {
   191  		Type    TemplateType           `json:"type"`
   192  		Columns []*ImageCarouselColumn `json:"columns"`
   193  	}{
   194  		Type:    TemplateTypeImageCarousel,
   195  		Columns: t.Columns,
   196  	})
   197  }
   198  
   199  // Template implements Template interface
   200  func (*ConfirmTemplate) Template() {}
   201  
   202  // Template implements Template interface
   203  func (*ButtonsTemplate) Template() {}
   204  
   205  // Template implements Template interface
   206  func (*CarouselTemplate) Template() {}
   207  
   208  // Template implements Template interface
   209  func (*ImageCarouselTemplate) Template() {}
   210  
   211  // NewConfirmTemplate function
   212  func NewConfirmTemplate(text string, left, right TemplateAction) *ConfirmTemplate {
   213  	return &ConfirmTemplate{
   214  		Text:    text,
   215  		Actions: []TemplateAction{left, right},
   216  	}
   217  }
   218  
   219  // NewButtonsTemplate function
   220  // `thumbnailImageURL` and `title` are optional. they can be empty.
   221  func NewButtonsTemplate(thumbnailImageURL, title, text string, actions ...TemplateAction) *ButtonsTemplate {
   222  	return &ButtonsTemplate{
   223  		ThumbnailImageURL: thumbnailImageURL,
   224  		Title:             title,
   225  		Text:              text,
   226  		Actions:           actions,
   227  	}
   228  }
   229  
   230  // NewCarouselTemplate function
   231  func NewCarouselTemplate(columns ...*CarouselColumn) *CarouselTemplate {
   232  	return &CarouselTemplate{
   233  		Columns: columns,
   234  	}
   235  }
   236  
   237  // NewCarouselColumn function
   238  // `thumbnailImageURL` and `title` are optional. they can be empty.
   239  func NewCarouselColumn(thumbnailImageURL, title, text string, actions ...TemplateAction) *CarouselColumn {
   240  	return &CarouselColumn{
   241  		ThumbnailImageURL: thumbnailImageURL,
   242  		Title:             title,
   243  		Text:              text,
   244  		Actions:           actions,
   245  	}
   246  }
   247  
   248  // NewImageCarouselTemplate function
   249  func NewImageCarouselTemplate(columns ...*ImageCarouselColumn) *ImageCarouselTemplate {
   250  	return &ImageCarouselTemplate{
   251  		Columns: columns,
   252  	}
   253  }
   254  
   255  // NewImageCarouselColumn function
   256  func NewImageCarouselColumn(imageURL string, action TemplateAction) *ImageCarouselColumn {
   257  	return &ImageCarouselColumn{
   258  		ImageURL: imageURL,
   259  		Action:   action,
   260  	}
   261  }