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

Add WithImageOptions for buttons/carousel templates #66

Merged
merged 2 commits into from
Jan 10, 2018
Merged
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
46 changes: 46 additions & 0 deletions linebot/send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ func TestPushMessages(t *testing.T) {
Response: &BasicResponse{},
},
},
{
// A buttons template message without title, with image options
Messages: []Message{
NewTemplateMessage(
"this is a buttons template",
NewButtonsTemplate(
"https://example.com/bot/images/image.jpg",
"",
"Please select",
NewPostbackTemplateAction("Buy", "action=buy&itemid=123", ""),
NewPostbackTemplateAction("Buy", "action=buy&itemid=123", "text"),
NewURITemplateAction("View detail", "http://example.com/page/123"),
).WithImageOptions("rectangle", "cover", "#FFFFFF"),
),
},
ResponseCode: 200,
Response: []byte(`{}`),
Want: want{
RequestBody: []byte(`{"to":"U0cc15697597f61dd8b01cea8b027050e","messages":[{"type":"template","altText":"this is a buttons template","template":{"type":"buttons","thumbnailImageUrl":"https://example.com/bot/images/image.jpg","imageAspectRatio":"rectangle","imageSize":"cover","imageBackgroundColor":"#FFFFFF","text":"Please select","actions":[{"type":"postback","label":"Buy","data":"action=buy\u0026itemid=123"},{"type":"postback","label":"Buy","data":"action=buy\u0026itemid=123","text":"text"},{"type":"uri","label":"View detail","uri":"http://example.com/page/123"}]}}]}` + "\n"),
Response: &BasicResponse{},
},
},
{
// A buttons template message without thumbnailImageURL and title
Messages: []Message{
Expand Down Expand Up @@ -251,6 +273,30 @@ func TestPushMessages(t *testing.T) {
Response: &BasicResponse{},
},
},
{
// A carousel template message, with new image options
Messages: []Message{
NewTemplateMessage(
"this is a carousel template with imageAspectRatio, imageSize and imageBackgroundColor",
NewCarouselTemplate(
NewCarouselColumn(
"https://example.com/bot/images/item1.jpg",
"this is menu",
"description",
NewPostbackTemplateAction("Buy", "action=buy&itemid=111", ""),
NewPostbackTemplateAction("Add to cart", "action=add&itemid=111", ""),
NewURITemplateAction("View detail", "http://example.com/page/111"),
).WithImageOptions("#FFFFFF"),
).WithImageOptions("rectangle", "cover"),
),
},
ResponseCode: 200,
Response: []byte(`{}`),
Want: want{
RequestBody: []byte(`{"to":"U0cc15697597f61dd8b01cea8b027050e","messages":[{"type":"template","altText":"this is a carousel template with imageAspectRatio, imageSize and imageBackgroundColor","template":{"type":"carousel","columns":[{"thumbnailImageUrl":"https://example.com/bot/images/item1.jpg","imageBackgroundColor":"#FFFFFF","title":"this is menu","text":"description","actions":[{"type":"postback","label":"Buy","data":"action=buy\u0026itemid=111"},{"type":"postback","label":"Add to cart","data":"action=add\u0026itemid=111"},{"type":"uri","label":"View detail","uri":"http://example.com/page/111"}]}],"imageAspectRatio":"rectangle","imageSize":"cover"}}]}` + "\n"),
Response: &BasicResponse{},
},
},
{
// A imagecarousel template message
Messages: []Message{
Expand Down
101 changes: 78 additions & 23 deletions linebot/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ const (
TemplateActionTypeDatetimePicker TemplateActionType = "datetimepicker"
)

// ImageAspectRatioType type
type ImageAspectRatioType string

// ImageAspectRatioType constants
const (
ImageAspectRatioTypeRectangle ImageAspectRatioType = "rectangle"
ImageAspectRatioTypeSquare ImageAspectRatioType = "square"
)

// ImageSizeType type
type ImageSizeType string

// ImageSizeType constants
const (
ImageSizeTypeCover ImageSizeType = "cover"
ImageSizeTypeContain ImageSizeType = "contain"
)

// Template interface
type Template interface {
json.Marshaler
Expand All @@ -48,29 +66,46 @@ type Template interface {

// ButtonsTemplate type
type ButtonsTemplate struct {
ThumbnailImageURL string
Title string
Text string
Actions []TemplateAction
ThumbnailImageURL string
ImageAspectRatio ImageAspectRatioType
ImageSize ImageSizeType
ImageBackgroundColor string
Title string
Text string
Actions []TemplateAction
}

// MarshalJSON method of ButtonsTemplate
func (t *ButtonsTemplate) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type TemplateType `json:"type"`
ThumbnailImageURL string `json:"thumbnailImageUrl,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text"`
Actions []TemplateAction `json:"actions"`
Type TemplateType `json:"type"`
ThumbnailImageURL string `json:"thumbnailImageUrl,omitempty"`
ImageAspectRatio ImageAspectRatioType `json:"imageAspectRatio,omitempty"`
ImageSize ImageSizeType `json:"imageSize,omitempty"`
ImageBackgroundColor string `json:"imageBackgroundColor,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text"`
Actions []TemplateAction `json:"actions"`
}{
Type: TemplateTypeButtons,
ThumbnailImageURL: t.ThumbnailImageURL,
Title: t.Title,
Text: t.Text,
Actions: t.Actions,
Type: TemplateTypeButtons,
ThumbnailImageURL: t.ThumbnailImageURL,
ImageAspectRatio: t.ImageAspectRatio,
ImageSize: t.ImageSize,
ImageBackgroundColor: t.ImageBackgroundColor,
Title: t.Title,
Text: t.Text,
Actions: t.Actions,
})
}

// WithImageOptions method, ButtonsTemplate can set imageAspectRatio, imageSize and imageBackgroundColor
func (t *ButtonsTemplate) WithImageOptions(imageAspectRatio ImageAspectRatioType, imageSize ImageSizeType, imageBackgroundColor string) *ButtonsTemplate {
t.ImageAspectRatio = imageAspectRatio
t.ImageSize = imageSize
t.ImageBackgroundColor = imageBackgroundColor
return t
}

// ConfirmTemplate type
type ConfirmTemplate struct {
Text string
Expand All @@ -92,28 +127,48 @@ func (t *ConfirmTemplate) MarshalJSON() ([]byte, error) {

// CarouselTemplate type
type CarouselTemplate struct {
Columns []*CarouselColumn
Columns []*CarouselColumn
ImageAspectRatio ImageAspectRatioType
ImageSize ImageSizeType
}

// CarouselColumn type
type CarouselColumn struct {
ThumbnailImageURL string `json:"thumbnailImageUrl,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text"`
Actions []TemplateAction `json:"actions"`
ThumbnailImageURL string `json:"thumbnailImageUrl,omitempty"`
ImageBackgroundColor string `json:"imageBackgroundColor,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text"`
Actions []TemplateAction `json:"actions"`
}

// MarshalJSON method of CarouselTemplate
func (t *CarouselTemplate) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type TemplateType `json:"type"`
Columns []*CarouselColumn `json:"columns"`
Type TemplateType `json:"type"`
Columns []*CarouselColumn `json:"columns"`
ImageAspectRatio ImageAspectRatioType `json:"imageAspectRatio,omitempty"`
ImageSize ImageSizeType `json:"imageSize,omitempty"`
}{
Type: TemplateTypeCarousel,
Columns: t.Columns,
Type: TemplateTypeCarousel,
Columns: t.Columns,
ImageAspectRatio: t.ImageAspectRatio,
ImageSize: t.ImageSize,
})
}

// WithImageOptions method, CarouselTemplate can set imageAspectRatio and imageSize
func (t *CarouselTemplate) WithImageOptions(imageAspectRatio ImageAspectRatioType, imageSize ImageSizeType) *CarouselTemplate {
t.ImageAspectRatio = imageAspectRatio
t.ImageSize = imageSize
return t
}

// WithImageOptions method, CarouselColumn can set imageBackgroundColor
func (t *CarouselColumn) WithImageOptions(imageBackgroundColor string) *CarouselColumn {
t.ImageBackgroundColor = imageBackgroundColor
return t
}

// ImageCarouselTemplate type
type ImageCarouselTemplate struct {
Columns []*ImageCarouselColumn
Expand Down