From 8c7533efa670f6b165d7333d754f97f13c33201e Mon Sep 17 00:00:00 2001 From: zhaomingqiang Date: Wed, 2 Mar 2022 15:25:34 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9Amessage=20card=20i18n=20code=20samp?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/api/message_model.go | 54 +++++++++++++++++++ sample/api/message_model_card.go | 92 ++++++++++++++++++++++++++------ sample/go.mod | 2 +- sample/go.sum | 2 + 4 files changed, 132 insertions(+), 18 deletions(-) diff --git a/sample/api/message_model.go b/sample/api/message_model.go index 9346c69a..699637f2 100644 --- a/sample/api/message_model.go +++ b/sample/api/message_model.go @@ -9,6 +9,7 @@ func main() { printMessageText() printMessagePost() printMessageCard() + printMessageCardI18n() printMessageImage() printMessageShareChat() printMessageShareUser() @@ -146,6 +147,59 @@ func printMessageCard() { fmt.Println("-----------------------------------------") } +func printMessageCardI18n() { + card := &lark.MessageCard{ + CardLink: &lark.MessageCardURL{ + URL: "https://www.feishu.cn", + AndroidURL: "https://www.feishu.cn", + IOSURL: "https://www.feishu.cn", + PCURL: "https://www.feishu.cn", + }, + Config: &lark.MessageCardConfig{EnableForward: lark.BoolPtr(true)}, + Header: &lark.MessageCardHeader{ + Template: lark.StringPtr("blue"), + Title: &lark.MessageCardPlainText{ + I18n: &lark.MessageCardPlainTextI18n{ + ZhCN: "ZhCN Header title", + EnUS: "", + JaJP: "JaJP Header title", + }, + }, + }, + I18nElements: &lark.MessageCardI18nElements{ + ZhCN: []lark.MessageCardElement{ + &lark.MessageCardMarkdown{ + Content: "**ZhCN**", + }, + &lark.MessageCardMarkdown{ + Content: "**ZhCN-2**", + }, + }, + EnUS: []lark.MessageCardElement{ + &lark.MessageCardMarkdown{ + Content: "**EnUS**", + }, + &lark.MessageCardMarkdown{ + Content: "**EnUS-2**", + }, + }, + JaJP: []lark.MessageCardElement{ + &lark.MessageCardMarkdown{ + Content: "**JaJP**", + }, + &lark.MessageCardMarkdown{ + Content: "**JaJP-2**", + }, + }, + }, + } + s, err := card.JSON() + if err != nil { + panic(err) + } + fmt.Println(s) +} + func printMessageImage() { image := &lark.MessageImage{ImageKey: "img_v2_9221f258-db3e-4a40-b9cb-24decddee2bg"} s, err := image.JSON() diff --git a/sample/api/message_model_card.go b/sample/api/message_model_card.go index 57dc8f9f..c80331c1 100644 --- a/sample/api/message_model_card.go +++ b/sample/api/message_model_card.go @@ -6,30 +6,88 @@ import ( ) func main() { - printMessageCard1() + printMessageCardWithAction() + //printMessageCardWithI18n() } -func printMessageCard1() { - card := lark.MessageCard{ +func printMessageCardWithAction() { + card := &lark.MessageCard{ + CardLink: &lark.MessageCardURL{ + URL: "https://www.feishu.cn", + AndroidURL: "https://www.feishu.cn", + IOSURL: "https://www.feishu.cn", + PCURL: "https://www.feishu.cn", + }, + Config: &lark.MessageCardConfig{EnableForward: lark.BoolPtr(true)}, Header: &lark.MessageCardHeader{ - Title: &lark.MessageCardPlainText{Content: "k8s ticket"}, - Template: lark.StringPtr("indigo"), + Template: lark.StringPtr("blue"), + Title: &lark.MessageCardPlainText{ + Content: "Header title", + }, }, - } - element := &lark.MessageCardAction{} - cluster := &lark.MessageCardEmbedSelectMenuStatic{ - MessageCardEmbedSelectMenuBase: &lark.MessageCardEmbedSelectMenuBase{ - Placeholder: &lark.MessageCardPlainText{ - Content: "choose cluster", + Elements: []lark.MessageCardElement{ + &lark.MessageCardImage{ + Alt: &lark.MessageCardPlainText{ + Content: "img_v2_9221f258-db3e-4a40-b9cb-24decddee2bg", + Lines: nil, + }, + Title: &lark.MessageCardPlainText{ + Content: "img_v2_9221f258-db3e-4a40-b9cb-24decddee2bg", + }, + ImgKey: "img_v2_9221f258-db3e-4a40-b9cb-24decddee2bg", + CustomWidth: lark.IntPtr(300), + CompactWidth: lark.BoolPtr(false), + }, + &lark.MessageCardAction{ + Actions: []lark.MessageCardActionElement{ + &lark.MessageCardEmbedButton{ + Text: &lark.MessageCardPlainText{ + Content: "button", + }, + Type: lark.MessageCardButtonTypeDanger.Ptr(), + Value: map[string]interface{}{"value": "1"}, + Confirm: &lark.MessageCardActionConfirm{ + Title: &lark.MessageCardPlainText{ + Content: "Title", + }, + Text: &lark.MessageCardPlainText{ + Content: "Text", + }, + }, + }, + }, + Layout: lark.MessageCardActionLayoutFlow.Ptr(), + }, + &lark.MessageCardMarkdown{ + Content: "**Markdown**", + }, + &lark.MessageCardDiv{ + Text: &lark.MessageCardPlainText{ + Content: "text", + }, + Extra: &lark.MessageCardEmbedButton{ + Text: &lark.MessageCardPlainText{ + Content: "button", + }, + Type: lark.MessageCardButtonTypeDanger.Ptr(), + Value: map[string]interface{}{"value": "1"}, + Confirm: &lark.MessageCardActionConfirm{ + Title: &lark.MessageCardPlainText{ + Content: "Title", + }, + Text: &lark.MessageCardPlainText{ + Content: "Text", + }, + }, + }, }, - InitialOption: "dev", }, } - element.Actions = append(element.Actions, cluster) - card.Elements = append(card.Elements, element) - content, err := card.JSON() + s, err := card.JSON() if err != nil { - fmt.Println(err) + panic(err) } - fmt.Printf("card:%s\n", content) + fmt.Println("MessageCard: ----------------------------") + fmt.Println(s) + fmt.Println("-----------------------------------------") } diff --git a/sample/go.mod b/sample/go.mod index 26d1dfa9..2397eb18 100644 --- a/sample/go.mod +++ b/sample/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/go-redis/redis/v8 v8.11.4 - github.com/larksuite/oapi-sdk-go/v2 v2.0.7 + github.com/larksuite/oapi-sdk-go/v2 v2.0.10-0.20220302071802-a789bd255ddb github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.6.1 // indirect ) diff --git a/sample/go.sum b/sample/go.sum index e5e88a3f..3812454a 100644 --- a/sample/go.sum +++ b/sample/go.sum @@ -30,6 +30,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/larksuite/oapi-sdk-go/v2 v2.0.7 h1:4MLxG40DKhdDhHdf/Wd5SUIDipX3K3Vj0XV9aGaSh8Y= github.com/larksuite/oapi-sdk-go/v2 v2.0.7/go.mod h1:ruCRr2wLAZJGJRugAgO70rbdpnIiLkz6sTlGQC38jcs= +github.com/larksuite/oapi-sdk-go/v2 v2.0.10-0.20220302071802-a789bd255ddb h1:HM9GIIaD4/b1Lt937k+/X0N3GiPCGR7iR7K9QOfGGuE= +github.com/larksuite/oapi-sdk-go/v2 v2.0.10-0.20220302071802-a789bd255ddb/go.mod h1:ruCRr2wLAZJGJRugAgO70rbdpnIiLkz6sTlGQC38jcs= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=