card

package
v1.14.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 2 Imported by: 3

README

Card Builder

Interactive card is rich in formats. However, it takes much efforts to build one. Thus, we provide a declarative card builder to make it easier.

We map every element of card (div, text, button, etc.) to declarative calls as shown in the demo.

All inner elements (e.g. fields of div blocks) are considered as arguments, while all element properties (e.g. forward property of card blocks) are considered as chained calls.

Getting Started

b := NewCardBuilder()
c := b.Card(
    b.Markdown("some text"),
).
    Title("title").
    NoForward()

fmt.Println(c.String())

which will render as following:

{
  "config": {
    "wide_screen_mode": true,
    "enable_forward": false
  },
  "header": {
    "title": {
      "tag": "plain_text",
      "content": "title"
    }
  },
  "elements": [
    {
      "tag": "markdown",
      "content": "some text"
    }
  ]
}

Example: examples/interactive-message

I18N

Card with I18N support is also supported. We introduce a I18N struct to build localizable parts (e.g. title and contents):

b := lark.NewCardBuilder()
c := b.I18N.Card(
        b.I18N.WithLocale(
            LocaleEnUS,
            b.Div(
                b.Field(b.Text("English Content")),
            ),
        ),
        b.I18N.WithLocale(
            LocaleZhCN,
            b.Div(
                b.Field(b.Text("中文内容")),
            ),
        ),
    ).
    Title(
        b.I18N.LocalizedText(LocaleEnUS, "English Title"),
        b.I18N.LocalizedText(LocaleZhCN, "中文标题"),
    )

Sending Message

b := lark.NewCardBuilder()
card := b.Card(
    b.Div(
        b.Field(b.Text("Content")).Short(),
    ),
).
    Wathet().
    Title("Card Title")
msg := lark.NewMsgBuffer(lark.MsgInteractive)
om := msg.BindEmail("[email protected]").Card(card.String()).Build()
resp, err := bot.PostMessage(om)

Documentation

Overview

Package card provides declarative card builder

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionBlock

type ActionBlock struct {
	// contains filtered or unexported fields
}

ActionBlock 交互元素

func Action

func Action(actions ...Element) *ActionBlock

Action 交互元素,可添加 Button, SelectMenu, Overflow, DatePicker, TimePicker, DatetimePicker

func (*ActionBlock) BisectedLayout

func (a *ActionBlock) BisectedLayout() *ActionBlock

BisectedLayout 二等分布局排列

func (*ActionBlock) FlowLayout

func (a *ActionBlock) FlowLayout() *ActionBlock

FlowLayout 自适应流式布局

func (*ActionBlock) Render

func (a *ActionBlock) Render() Renderer

Render 渲染为 Renderer

func (*ActionBlock) TrisectionLayout

func (a *ActionBlock) TrisectionLayout() *ActionBlock

TrisectionLayout 三等分布局排列

type Block

type Block struct {
	// contains filtered or unexported fields
}

Block 卡片元素

func Card

func Card(el ...Element) *Block

Card 包裹了最外层的卡片结构

func (*Block) Blue

func (b *Block) Blue() *Block

Blue 设置卡片标题栏颜色(蓝色)

func (*Block) Carmine

func (b *Block) Carmine() *Block

Carmine 设置卡片标题栏颜色(洋红色)

func (*Block) Green

func (b *Block) Green() *Block

Green 设置卡片标题栏颜色(绿色)

func (*Block) Grey

func (b *Block) Grey() *Block

Grey 设置卡片标题栏颜色(灰色)

func (*Block) Indigo

func (b *Block) Indigo() *Block

Indigo 设置卡片标题栏颜色(靛青色)

func (b *Block) Link(href *URLBlock) *Block

Link 设置卡片跳转链接

func (*Block) MarshalJSON

func (b *Block) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Block) NoForward

func (b *Block) NoForward() *Block

NoForward 设置后,卡片将不可转发

func (*Block) Orange

func (b *Block) Orange() *Block

Orange 设置卡片标题栏颜色(橙色)

func (*Block) Purple

func (b *Block) Purple() *Block

Purple 设置卡片标题栏颜色(紫色)

func (*Block) Red

func (b *Block) Red() *Block

Red 设置卡片标题栏颜色(红色)

func (*Block) Render

func (b *Block) Render() Renderer

Render 渲染为 Renderer

func (*Block) String

func (b *Block) String() string

String implements fmt.Stringer

func (*Block) Title

func (b *Block) Title(title string) *Block

Title 卡片标题

func (*Block) Turquoise

func (b *Block) Turquoise() *Block

Turquoise 设置卡片标题栏颜色(松石绿)

func (*Block) UpdateMulti added in v1.7.1

func (b *Block) UpdateMulti(updateMulti bool) *Block

UpdateMulti set card can be updated

func (*Block) Violet

func (b *Block) Violet() *Block

Violet 设置卡片标题栏颜色(紫红色)

func (*Block) Wathet

func (b *Block) Wathet() *Block

Wathet 设置卡片标题栏颜色(浅蓝色)

func (*Block) Yellow

func (b *Block) Yellow() *Block

Yellow 设置卡片标题栏颜色(黄色)

type ButtonBlock

type ButtonBlock struct {
	// contains filtered or unexported fields
}

ButtonBlock 按钮元素

func Button

func Button(text *TextBlock) *ButtonBlock

Button 按钮交互元素

func (*ButtonBlock) Confirm

func (b *ButtonBlock) Confirm(title, text string) *ButtonBlock

Confirm 点击后二次确认的弹框

func (*ButtonBlock) Danger

func (b *ButtonBlock) Danger() *ButtonBlock

Danger 设置按钮样式(警示按钮)

func (*ButtonBlock) Default

func (b *ButtonBlock) Default() *ButtonBlock

Default 设置按钮样式(次要按钮)

func (*ButtonBlock) MultiURL

func (b *ButtonBlock) MultiURL(u *URLBlock) *ButtonBlock

MultiURL 按钮的多端差异跳转链接

func (*ButtonBlock) Primary

func (b *ButtonBlock) Primary() *ButtonBlock

Primary 设置按钮样式(主要按钮)

func (*ButtonBlock) Render

func (b *ButtonBlock) Render() Renderer

Render 渲染为 Renderer

func (*ButtonBlock) URL

func (b *ButtonBlock) URL(u string) *ButtonBlock

URL 按钮的跳转链接

func (*ButtonBlock) Value

func (b *ButtonBlock) Value(v map[string]interface{}) *ButtonBlock

Value 点击后发送给业务方的数据

type ColumnBlock added in v1.9.0

type ColumnBlock struct {
	// contains filtered or unexported fields
}

ColumnBlock column element

func Column added in v1.9.0

func Column(els ...Element) *ColumnBlock

Column .

func (*ColumnBlock) Render added in v1.9.0

func (c *ColumnBlock) Render() Renderer

Render .

func (*ColumnBlock) VerticalAlign added in v1.9.0

func (c *ColumnBlock) VerticalAlign(align string) *ColumnBlock

VerticalAlign .

func (*ColumnBlock) Weight added in v1.9.0

func (c *ColumnBlock) Weight(weight int) *ColumnBlock

Weight .

func (*ColumnBlock) Width added in v1.9.0

func (c *ColumnBlock) Width(width string) *ColumnBlock

Width .

type ColumnSetActionBlock added in v1.9.0

type ColumnSetActionBlock struct {
	// contains filtered or unexported fields
}

ColumnSetActionBlock column action element

func ColumnSetAction added in v1.9.0

func ColumnSetAction(url *URLBlock) *ColumnSetActionBlock

ColumnSetAction .

func (*ColumnSetActionBlock) Render added in v1.9.0

func (c *ColumnSetActionBlock) Render() Renderer

Render .

type ColumnSetBlock added in v1.9.0

type ColumnSetBlock struct {
	// contains filtered or unexported fields
}

ColumnSetBlock column set element

func ColumnSet added in v1.9.0

func ColumnSet(columns ...*ColumnBlock) *ColumnSetBlock

ColumnSet .

func (*ColumnSetBlock) Action added in v1.9.0

Action add column set action

func (*ColumnSetBlock) BackgroundStyle added in v1.9.0

func (c *ColumnSetBlock) BackgroundStyle(style string) *ColumnSetBlock

BackgroundStyle set background style

func (*ColumnSetBlock) FlexMode added in v1.9.0

func (c *ColumnSetBlock) FlexMode(mode string) *ColumnSetBlock

FlexMode set flex mode

func (*ColumnSetBlock) HorizontalSpacing added in v1.9.0

func (c *ColumnSetBlock) HorizontalSpacing(hs string) *ColumnSetBlock

HorizontalSpacing set horizontal spacing

func (*ColumnSetBlock) Render added in v1.9.0

func (c *ColumnSetBlock) Render() Renderer

Render .

type ConfirmBlock

type ConfirmBlock struct {
	// contains filtered or unexported fields
}

ConfirmBlock 确认模块

func Confirm

func Confirm(title, text string) *ConfirmBlock

Confirm 用于交互元素的二次确认

func (*ConfirmBlock) Render

func (c *ConfirmBlock) Render() Renderer

Render 渲染为 Renderer

type DatePickerBlock

type DatePickerBlock struct {
	*PickerBlock
}

DatePickerBlock 日期选择器元素

func DatePicker

func DatePicker() *DatePickerBlock

DatePicker 日期选择器

func (*DatePickerBlock) InitialDate

func (d *DatePickerBlock) InitialDate(t time.Time) *DatePickerBlock

InitialDate 设置选择器的默认日期

func (*DatePickerBlock) InitialDateString

func (d *DatePickerBlock) InitialDateString(date string) *DatePickerBlock

InitialDateString 设置选择器的默认日期

type DatetimePickerBlock

type DatetimePickerBlock struct {
	*PickerBlock
}

DatetimePickerBlock 日期时间选择器元素

func DatetimePicker

func DatetimePicker() *DatetimePickerBlock

DatetimePicker 日期时间选择器

func (*DatetimePickerBlock) InitialDatetime

func (d *DatetimePickerBlock) InitialDatetime(t time.Time) *DatetimePickerBlock

InitialDatetime 设置选择器的默认日期时间

func (*DatetimePickerBlock) InitialDatetimeString

func (d *DatetimePickerBlock) InitialDatetimeString(date string) *DatetimePickerBlock

InitialDatetimeString 设置选择器的默认日期时间

type DivBlock

type DivBlock struct {
	// contains filtered or unexported fields
}

DivBlock 内容元素

func Div

func Div(fields ...*FieldBlock) *DivBlock

Div 内容模块

func (*DivBlock) Extra

func (d *DivBlock) Extra(e Element) *DivBlock

Extra 模块的附加元素,展示在内容右侧。可附加 Img, Button, SelectMenu, Overflow, DatePicker, TimePicker, DatetimePicker

func (*DivBlock) Render

func (d *DivBlock) Render() Renderer

Render 渲染为 Renderer

func (*DivBlock) Text

func (d *DivBlock) Text(t *TextBlock) *DivBlock

Text 单个文本的展示,和 fields 至少要有一个

type Element

type Element interface {
	Render() Renderer
}

Element 所有元素均需实现的方法

type ElementTag

type ElementTag struct {
	Tag string `json:"tag"`
}

ElementTag 标记元素的Tag

type FieldBlock

type FieldBlock struct {
	// contains filtered or unexported fields
}

FieldBlock 排版模块元素

func Field

func Field(text *TextBlock) *FieldBlock

Field 内容模块的排版元素

func (*FieldBlock) Render

func (f *FieldBlock) Render() Renderer

Render 渲染为 Renderer

func (*FieldBlock) Short

func (f *FieldBlock) Short() *FieldBlock

Short 设置后,将会使用并排布局

type HrBlock

type HrBlock struct{}

HrBlock 分割线元素

func Hr

func Hr() *HrBlock

Hr 分割线模块

func (*HrBlock) Render

func (h *HrBlock) Render() Renderer

Render 渲染为 Renderer

type ImgBlock

type ImgBlock struct {
	// contains filtered or unexported fields
}

ImgBlock 图片元素

func Img

func Img(key string) *ImgBlock

Img 图片展示模块

func (*ImgBlock) Alt

func (i *ImgBlock) Alt(s string) *ImgBlock

Alt Hover图片时展示的文案,留空则不展示

func (*ImgBlock) Compact

func (i *ImgBlock) Compact() *ImgBlock

Compact 展示紧凑型的图片,设置后最大宽度为 278px

func (*ImgBlock) CropCenter

func (i *ImgBlock) CropCenter() *ImgBlock

CropCenter 居中裁剪模式(默认)

func (*ImgBlock) FitHorizontal

func (i *ImgBlock) FitHorizontal() *ImgBlock

FitHorizontal 平铺模式

func (*ImgBlock) NoPreview

func (i *ImgBlock) NoPreview() *ImgBlock

NoPreview 设置后,点击图片后将不会放大图片,可与 CardLink 同时设置

func (*ImgBlock) Render

func (i *ImgBlock) Render() Renderer

Render 渲染为 Renderer

func (*ImgBlock) Title

func (i *ImgBlock) Title(t *TextBlock) *ImgBlock

Title 图片标题

func (*ImgBlock) TitleString

func (i *ImgBlock) TitleString(t string) *ImgBlock

TitleString 图片标题

func (*ImgBlock) Width

func (i *ImgBlock) Width(w int) *ImgBlock

Width 图片的最大展示宽度,范围 278 ~ 580

type MarkdownBlock

type MarkdownBlock struct {
	// contains filtered or unexported fields
}

MarkdownBlock Markdown文本元素

func Markdown

func Markdown(s string) *MarkdownBlock

Markdown 单独使用的 Markdown 文本模块

func (*MarkdownBlock) AlignCenter added in v1.9.0

func (m *MarkdownBlock) AlignCenter() *MarkdownBlock

AlignCenter .

func (*MarkdownBlock) AlignLeft added in v1.9.0

func (m *MarkdownBlock) AlignLeft() *MarkdownBlock

AlignLeft .

func (*MarkdownBlock) AlignRight added in v1.9.0

func (m *MarkdownBlock) AlignRight() *MarkdownBlock

AlignRight .

func (*MarkdownBlock) Href added in v1.9.0

func (m *MarkdownBlock) Href(name string, url *URLBlock) *MarkdownBlock

Href 设置文本中 []($urlVal) 格式的链接值,仅在 LarkMd 和 Markdown 模块中可用

func (*MarkdownBlock) Render added in v1.9.0

func (m *MarkdownBlock) Render() Renderer

Render 渲染为 Renderer

type NoteBlock

type NoteBlock struct {
	// contains filtered or unexported fields
}

NoteBlock 备注元素

func Note

func Note() *NoteBlock

Note 备注模块

func (*NoteBlock) AddImage

func (n *NoteBlock) AddImage(i *ImgBlock) *NoteBlock

AddImage 添加一个图片模块

func (*NoteBlock) AddText

func (n *NoteBlock) AddText(t *TextBlock) *NoteBlock

AddText 添加一个文本模块

func (*NoteBlock) Render

func (n *NoteBlock) Render() Renderer

Render 渲染为 Renderer

type OptionBlock

type OptionBlock struct {
	// contains filtered or unexported fields
}

OptionBlock 选项元素

func Option

func Option(value string) *OptionBlock

Option 选项模块,可用于 SelectMenu 和 Overflow

func (*OptionBlock) MultiURL

func (o *OptionBlock) MultiURL(u *URLBlock) *OptionBlock

MultiURL 选项多端跳转链接

func (*OptionBlock) Render

func (o *OptionBlock) Render() Renderer

Render 渲染为 Renderer

func (*OptionBlock) Text

func (o *OptionBlock) Text(s string) *OptionBlock

Text 选项显示内容

func (*OptionBlock) URL

func (o *OptionBlock) URL(u string) *OptionBlock

URL 选项跳转链接

type OverflowBlock

type OverflowBlock struct {
	// contains filtered or unexported fields
}

OverflowBlock 折叠菜单元素

func Overflow

func Overflow(opt ...*OptionBlock) *OverflowBlock

Overflow 折叠按钮菜单组件

func (*OverflowBlock) Confirm

func (o *OverflowBlock) Confirm(title, text string) *OverflowBlock

Confirm 选定后二次确认的弹框

func (*OverflowBlock) Render

func (o *OverflowBlock) Render() Renderer

Render 渲染为 Renderer

func (*OverflowBlock) Value

func (o *OverflowBlock) Value(v map[string]interface{}) *OverflowBlock

Value 选定后发送给业务方的数据

type PickerBlock

type PickerBlock struct {
	// contains filtered or unexported fields
}

PickerBlock 选择器基础元素

func (*PickerBlock) Confirm

func (p *PickerBlock) Confirm(title, text string) *PickerBlock

Confirm 选中后二次确认的弹框

func (*PickerBlock) Placeholder

func (p *PickerBlock) Placeholder(s string) *PickerBlock

Placeholder 未选中值时展示的内容,无默认值时必填

func (*PickerBlock) Render

func (p *PickerBlock) Render() Renderer

Render 渲染为 Renderer

func (*PickerBlock) Value

func (p *PickerBlock) Value(m map[string]interface{}) *PickerBlock

Value 选定后发送给业务方的数据

type Renderer

type Renderer interface{}

Renderer 渲染接口标记,用于保存成通用的树形结构

type SelectMenuBlock

type SelectMenuBlock struct {
	// contains filtered or unexported fields
}

SelectMenuBlock 菜单元素

func SelectMenu

func SelectMenu(opt ...*OptionBlock) *SelectMenuBlock

SelectMenu 菜单组件

func (*SelectMenuBlock) Confirm

func (s *SelectMenuBlock) Confirm(title, text string) *SelectMenuBlock

Confirm 选中后二次确认的弹框

func (*SelectMenuBlock) InitialOption

func (s *SelectMenuBlock) InitialOption(o string) *SelectMenuBlock

InitialOption 默认选项的 value 字段值

func (*SelectMenuBlock) Placeholder

func (s *SelectMenuBlock) Placeholder(p string) *SelectMenuBlock

Placeholder 未选中时展示的内容,无默认选项时必须设置

func (*SelectMenuBlock) Render

func (s *SelectMenuBlock) Render() Renderer

Render 渲染为 Renderer

func (*SelectMenuBlock) SelectPerson

func (s *SelectMenuBlock) SelectPerson() *SelectMenuBlock

SelectPerson 选人模式,value应设置为人员的open_id,options 为空则候选人员为当前群组

func (*SelectMenuBlock) Value

func (s *SelectMenuBlock) Value(v map[string]interface{}) *SelectMenuBlock

Value 选中后发送给业务方的数据

type TextBlock

type TextBlock struct {
	// contains filtered or unexported fields
}

TextBlock 文本元素

func Text

func Text(s string) *TextBlock

Text 文本模块

func (*TextBlock) Href

func (t *TextBlock) Href(name string, url *URLBlock) *TextBlock

Href 设置文本中 []($urlVal) 格式的链接值,仅在 LarkMd 和 Markdown 模块中可用

func (*TextBlock) LarkMd

func (t *TextBlock) LarkMd() *TextBlock

LarkMd 嵌入使用的 Markdown 展示模式

func (*TextBlock) Lines

func (t *TextBlock) Lines(l int) *TextBlock

Lines 内容展示的行数

func (*TextBlock) Render

func (t *TextBlock) Render() Renderer

Render 渲染为 Renderer

type TimePickerBlock

type TimePickerBlock struct {
	*PickerBlock
}

TimePickerBlock 时间选择器元素

func TimePicker

func TimePicker() *TimePickerBlock

TimePicker 时间选择器

func (*TimePickerBlock) InitialTime

func (d *TimePickerBlock) InitialTime(t time.Time) *TimePickerBlock

InitialTime 设置选择器的默认时间

func (*TimePickerBlock) InitialTimeString

func (d *TimePickerBlock) InitialTimeString(date string) *TimePickerBlock

InitialTimeString 设置选择器的默认时间

type URLBlock

type URLBlock struct {
	// contains filtered or unexported fields
}

URLBlock 链接模块

func URL

func URL() *URLBlock

URL 链接模块

func (*URLBlock) Href

func (u *URLBlock) Href(s string) *URLBlock

Href 默认跳转链接

func (*URLBlock) MultiHref

func (u *URLBlock) MultiHref(android, ios, pc string) *URLBlock

MultiHref 多端跳转链接,设置后 Href 将被忽略

func (*URLBlock) Render

func (u *URLBlock) Render() Renderer

Render 渲染为 Renderer

Directories

Path Synopsis
Package i18n internationalization support for card
Package i18n internationalization support for card

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL