Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
hilaily committed Feb 9, 2021
0 parents commit c37f809
Show file tree
Hide file tree
Showing 8 changed files with 609 additions and 0 deletions.
60 changes: 60 additions & 0 deletions card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package larkcard

import "encoding/json"

// Card represent card field in lark card message
// Reference
// https://open.larksuite.com/document/uMzMyEjLzMjMx4yMzITM/ukzNxUjL5cTM14SO3ETN
// https://open.feishu.cn/document/ukTMukTMukTM/ugTNwUjL4UDM14CO1ATN
type Card struct {
Config *Config `json:"config,omitempty"`
Header *Header `json:"header,omitempty"`
Modules []IModule `json:"elements"`
CardLink *OURL `json:"card_link,omitempty"`
I18nModules map[string][]IModule `json:"i18n_elements,omitempty"`
}

func (c *Card) SetCardLink(link *OURL) *Card {
c.CardLink = link
return c
}

// Encode to marshal card structure to []byte
func (c *Card) Encode() []byte {
en, _ := json.Marshal(c)
return en
}

// Header represent header info in cark structure
type Header struct {
Title *OText `json:"title"`
Template *HeaderColor `json:"template,omitempty"`
I18n map[string]string `json:"i18n,omitempty"`
}

// SetI18n
// zh_cn for chinese content, en_us for english, ja_jp for japanese
func (h *Header) SetI18n(i18nInfo map[string]string) *Header {
h.I18n = i18nInfo
return h
}

// SetColor set color for header
// Reference
// https://open.larksuite.com/document/uMzMyEjLzMjMx4yMzITM/uADOxUjLwgTM14CM4ETN
// https://open.feishu.cn/document/ukTMukTMukTM/ukTNwUjL5UDM14SO1ATN
func (h *Header) SetColor(color HeaderColor) *Header {
h.Template = &color
return h
}

// Config set custom config for card
// Reference
// https://open.larksuite.com/document/uMzMyEjLzMjMx4yMzITM/uEDOxUjLxgTM14SM4ETN
// https://open.feishu.cn/document/ukTMukTMukTM/uAjNwUjLwYDM14CM2ATN
type Config struct {
WideScreenMode *bool `json:"wide_screen_mode,omitempty"`
EnableForward *bool `json:"enable_forward,omitempty"`
}

type IModule interface{}
35 changes: 35 additions & 0 deletions create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package larkcard

func Create(config *Config, header *Header, modules ...IModule) *Card {
c := &Card{
Header: header,
Config: config,
}
c.Modules = modules
return c
}

func CreateI18n(config *Config, header *Header, modules map[string][]IModule) *Card {
c := &Card{
Header: header,
Config: config,
}
c.I18nModules = modules
return c
}

func CreateByMD(config *Config, header *Header, mdContent string, href *OURL) *Card {
c := &Card{
Header: header,
Config: config,
}
c.Modules = []IModule{newMODMD(mdContent, href)}
return c
}

func NewHeader(header string) *Header {
t := NewObjTextPlain(header)
return &Header{
Title: t,
}
}
42 changes: 42 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package larkcard

import (
"fmt"
"testing"
)

func TestCreateCard(t *testing.T) {
c := Create(
nil,
NewHeader("test header").SetColor(HeaderGreen),
NewModContent("[飞书](https://www.feishu.cn)整合即时沟通、日历、音视频会议、云文档、云盘、工作台等功能于一体,成就组织和个人,更高效、更愉悦。", true),
NewModInteraction(
NewEleButton("主按钮", ButtonPrimary),
NewEleButton("次按钮"),
NewEleButton("危险按钮", ButtonDanger),
),
NewModContent("深度整合使用率极高的办公工具,企业成员在一处即可实现高效沟通与协作。").SetExtra(
NewEleImage("img_e344c476-1e58-4492-b40d-7dcffe9d6dfg", "hover"),
),
NewModContent("在移动端同样进行便捷的沟通、互动与协作,手机电脑随时随地保持同步。").SetExtra(
NewEleSelectMenu("默认提示文本", [][2]string{
{"选项1", "1"},
{"选项2", "2"},
{"选项3", "3"},
}),
),
NewModContent("ISV产品接入及企业自主开发,更好地对接现有系统,满足不同组织的需求。").SetExtra(
NewEleOverflow(
NewObjOption("打开飞书应用目录", "https://app.feishu.cn"),
NewObjOption("打开飞书开发文档", "https://open.feishu.cn"),
),
),
NewModContent("国际权威安全认证与信息安全管理体系,为企业提供全生命周期安全保障。").SetExtra(
NewEleDatePicker(
DatePickerDate, "", "2020-09-20",
),
),
)
en := c.Encode()
fmt.Println(string(en))
}
185 changes: 185 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package larkcard

import (
"encoding/json"
)

// Reference:
// https://open.larksuite.com/document/uMzMyEjLzMjMx4yMzITM/uETOxUjLxkTM14SM5ETN
// https://open.feishu.cn/document/ukTMukTMukTM/uAzNwUjLwcDM14CM3ATN

func NewEleImage(imgKey, alt string) *EImage {
return &EImage{
Tag: "img",
ImgKey: imgKey,
Alt: NewObjTextPlain(alt),
}
}

func NewEleButton(text string, buttonType ...ButtonType) *EButton {
b := NewEleButtonOrigin(NewObjTextPlain(text))
if len(buttonType) > 0 {
b.SetType(buttonType[0])
}
return b
}
func NewEleButtonOrigin(text *OText) *EButton {
return &EButton{
Tag: "button",
Text: text,
}
}
func NewEleSelectMenu(placeholder string, options [][2]string) *ESelectMenu {
return NewEleSelectMenuOrigin(placeholder, "", options, false)
}

func NewEleSelectMenuOrigin(placeholder, initialOption string, options [][2]string, selectPerson bool) *ESelectMenu {
tag := "select_static"
if selectPerson {
tag = "select_person"
}

ops := make([]*OOption, 0, len(options))
for _, v := range options {
ops = append(ops, NewObjOption(v[0], v[1]))
}

s := &ESelectMenu{
Tag: tag,
Placeholder: NewObjTextPlain(placeholder),
InitialOption: initialOption,
}
if len(ops) > 0 {
s.Options = ops
}
return s
}

func NewEleOverflow(options ...*OOption) *EOverflow {
return &EOverflow{
Tag: "overflow",
Options: options,
}
}

func NewEleDatePicker(datePickerType DatePickerType, placeholder string, initialTime string) *EDatePicker {
d := &EDatePicker{
Tag: datePickerType,
}
if initialTime != "" {
if datePickerType == DatePickerDate {
d.InitialDate = initialTime
} else if datePickerType == DatePickerTime {
d.InitialTime = initialTime
} else if datePickerType == DatePickerDatetime {
d.InitialDatetime = initialTime
}
}
d.Placeholder = placeholder
return d
}

type IElement interface{}

type EImage struct {
Tag string `json:"tag"`
ImgKey string `json:"img_key"`
Alt *OText `json:"alt"`
Preview *bool `json:"preview,omitempty"`
}

func (i *EImage) SetPreview(preview bool) *EImage {
i.Preview = &preview
return i
}

type EButton struct {
Tag string `json:"tag"`
Text *OText `json:"text"`
URL string `json:"url"`
MultiURL *OURL `json:"multi_url,omitempty"`
Type *ButtonType `json:"type,omitempty"`
Value json.RawMessage `json:"value,omitempty"`
Confirm *OConfirm `json:"confirm,omitempty"`
}

func (b *EButton) SetURL(url string) *EButton {
b.URL = url
return b
}

func (b *EButton) SetMultiURL(url *OURL) *EButton {
b.MultiURL = url
return b
}

func (b *EButton) SetType(buttonType ButtonType) *EButton {
b.Type = &buttonType
return b
}

func (b *EButton) SetValue(val []byte) *EButton {
b.Value = val
return b
}

func (b *EButton) SetConfirm(confirm *OConfirm) *EButton {
b.Confirm = confirm
return b
}

type ESelectMenu struct {
Tag string `json:"tag"`
Placeholder *OText `json:"placeholder"`
InitialOption string `json:"initial_option,omitempty"`
Options []*OOption `json:"options,omitempty"`
Value json.RawMessage `json:"value,omitempty"`
Confirm *OConfirm `json:"confirm,omitempty"`
}

func (s *ESelectMenu) SetValue(val []byte) *ESelectMenu {
s.Value = val
return s
}

func (s *ESelectMenu) SetConfirm(confirm *OConfirm) *ESelectMenu {
s.Confirm = confirm
return s
}

type EOverflow struct {
Tag string `json:"tag"`
Options []*OOption `json:"options"`
Value json.RawMessage `json:"value,omitempty"`
Confirm *OConfirm `json:"confirm,omitempty"`
}

func (o *EOverflow) SetValue(val []byte) *EOverflow {
o.Value = val
return o
}

func (o *EOverflow) SetConfirm(confirm *OConfirm) *EOverflow {
o.Confirm = confirm
return o
}

type EDatePicker struct {
Tag string `json:"tag"`
InitialDate string `json:"initial_date,omitempty"`
InitialTime string `json:"initial_time,omitempty"`
InitialDatetime string `json:"initial_datetime,omitempty"`
Placeholder string `json:"placeholder,omitempty"`
Value json.RawMessage `json:"value,omitempty"`
Confirm *OConfirm `json:"confirm,omitempty"`
}

func (d *EDatePicker) SetValue(val []byte) *EDatePicker {
d.Value = val
return d
}

func (d *EDatePicker) SetConfirm(confirm *OConfirm) *EDatePicker {
d.Confirm = confirm
return d
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hilaily/larkcard

go 1.15
Loading

0 comments on commit c37f809

Please sign in to comment.