add line api object

This commit is contained in:
Jay 2019-01-23 14:49:05 +08:00
parent 58ca729d68
commit b2a6f95229
4 changed files with 226 additions and 61 deletions

View File

@ -0,0 +1,87 @@
package line
// Component -
type Component struct {
Type string `json:"type"`
}
// BoxComponent -
type BoxComponent struct {
Component
Layout string `json:"layout"`
Contents []interface{} `json:"contents"`
Spacing NullString `json:"spacing,omitempty"`
Action interface{} `json:"action,omitempty"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
}
// ButtonComponent -
type ButtonComponent struct {
Component
Action interface{} `json:"action"`
Height NullString `json:"height,omitempty"`
Style NullString `json:"style,omitempty"`
Color NullString `json:"color,omitempty"`
Gravity NullString `json:"gravity,omitempty"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
}
// FillerComponent -
type FillerComponent struct {
Component
}
// IconComponent -
type IconComponent struct {
Component
URL string `json:"url"`
Margin NullString `json:"margin,omitempty"`
Size NullString `json:"size,omitempty"`
AspectRatio NullString `json:"aspectRatio,omitempty"`
}
// ImageComponent -
type ImageComponent struct {
Component
URL string `json:"url"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
Align NullString `json:"align,omitempty"`
Gravity NullString `json:"gravity,omitempty"`
Size NullString `json:"size,omitempty"`
AspectRatio NullString `json:"aspectRatio,omitempty"`
AspectMode NullString `json:"aspectMode,omitempty"`
BackgroundColor NullString `json:"backgroundColor,omitempty"`
Action interface{} `json:"action,omitempty"`
}
// SeparatorComponent -
type SeparatorComponent struct {
Component
Margin NullString `json:"margin,omitempty"`
Color NullString `json:"color,omitempty"`
}
// SpacerComponent -
type SpacerComponent struct {
Component
Size NullString `json:"size,omitempty"`
}
// TextComponent -
type TextComponent struct {
Component
Text string `json:"text"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
Size NullString `json:"size,omitempty"`
Align NullString `json:"align,omitempty"`
Gravity NullString `json:"gravity,omitempty"`
Wrap bool `json:"wrap"`
MaxLines NullInt `json:"maxLines,omitempty"`
Weight NullString `json:"weight,omitempty"`
Color NullString `json:"color,omitempty"`
Action interface{} `json:"action,omitempty"`
}

View File

@ -0,0 +1,52 @@
package line
import "errors"
// BubbleContainer -
type BubbleContainer struct {
Type string `json:"type"`
Direction NullString `json:"direction"`
Header *BoxComponent `json:"header,omitempty"`
Hero *ImageComponent `json:"hero,omitempty"`
Body *BoxComponent `json:"body,omitempty"`
Footer *BoxComponent `json:"footer,omitempty"`
Styles interface{} `json:"styles,omitempty"`
}
// CarouselContainer -
type CarouselContainer struct {
Type string `json:"type"`
Contents []*BubbleContainer `json:"contents"`
}
// NewBubbleContainer -
func NewBubbleContainer(direction string) *BubbleContainer {
obj := &BubbleContainer{}
obj.Type = "bubble"
switch direction {
case "ltr":
case "rtl":
obj.Direction = String(direction)
default:
obj.Direction = String("ltr")
}
return obj
}
// NewCarouselContainer -
func NewCarouselContainer() *CarouselContainer {
obj := &CarouselContainer{}
obj.Type = "carousel"
return obj
}
// AddContent -
func (p *CarouselContainer) AddContent(obj ...*BubbleContainer) (err error) {
if len(obj) == 0 {
return errors.New("no item")
}
return
}

View File

@ -16,6 +16,22 @@ import (
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// NullString -
type NullString *string
// String -
func String(s string) NullString {
return &s
}
// NullInt -
type NullInt *int
// Int -
func Int(i int) NullInt {
return &i
}
// Line -
type Line struct {
URL string
@ -23,31 +39,6 @@ type Line struct {
AccessToken string
}
// Message -
type Message struct {
Type string `json:"type"`
}
// TextMessage -
type TextMessage struct {
Message
Text string `json:"text"`
}
// ImageMessage -
type ImageMessage struct {
Message
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
}
// VideoMessage -
type VideoMessage struct {
Message
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
}
// UserInfo -
type UserInfo struct {
DisplayName string `json:"displayName"`
@ -103,42 +94,6 @@ func (p *Line) getHeader() (header map[string]string) {
return
}
// NewTextMessage -
func NewTextMessage(text string) (msg TextMessage, err error) {
msg = TextMessage{}
if len(text) == 0 {
return msg, errors.New("message is empty")
}
msg.Type = "text"
msg.Text = text
return
}
// NewImageMessage -
func NewImageMessage(originalURL, previewURL string) (msg ImageMessage, err error) {
msg = ImageMessage{}
if len(originalURL) == 0 || len(previewURL) == 0 {
return msg, errors.New("original url or preview url is empty")
}
msg.Type = "image"
msg.OriginalContentURL = originalURL
msg.PreviewImageURL = previewURL
return
}
// NewVideoMessage -
func NewVideoMessage(originalURL, previewURL string) (msg VideoMessage, err error) {
msg = VideoMessage{}
if len(originalURL) == 0 || len(previewURL) == 0 {
return msg, errors.New("original url or preview url is empty")
}
msg.Type = "video"
msg.OriginalContentURL = originalURL
msg.PreviewImageURL = previewURL
return
}
func checkMessageStruct(msg interface{}) (valid bool) {
switch msg.(type) {
case TextMessage:

View File

@ -0,0 +1,71 @@
package line
import "errors"
// Message -
type Message struct {
Type string `json:"type"`
}
// TextMessage -
type TextMessage struct {
Message
Text string `json:"text"`
}
// ImageMessage -
type ImageMessage struct {
Message
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
}
// VideoMessage -
type VideoMessage struct {
Message
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
}
// FlexMessage -
type FlexMessage struct {
Message
AltText string `json:"altText"`
Contents interface{} `json:"contents"`
}
// NewTextMessage -
func NewTextMessage(text string) (msg TextMessage, err error) {
msg = TextMessage{}
if len(text) == 0 {
return msg, errors.New("message is empty")
}
msg.Type = "text"
msg.Text = text
return
}
// NewImageMessage -
func NewImageMessage(originalURL, previewURL string) (msg ImageMessage, err error) {
msg = ImageMessage{}
if len(originalURL) == 0 || len(previewURL) == 0 {
return msg, errors.New("original url or preview url is empty")
}
msg.Type = "image"
msg.OriginalContentURL = originalURL
msg.PreviewImageURL = previewURL
return
}
// NewVideoMessage -
func NewVideoMessage(originalURL, previewURL string) (msg VideoMessage, err error) {
msg = VideoMessage{}
if len(originalURL) == 0 || len(previewURL) == 0 {
return msg, errors.New("original url or preview url is empty")
}
msg.Type = "video"
msg.OriginalContentURL = originalURL
msg.PreviewImageURL = previewURL
return
}