前言
本文我们基于飞书开放平台提供的服务端SDK,展示下如何发送发送卡片消息
代码示例
本文我们基于飞书开平提供的go-sdk进行展示,go-sdk的github地址为:
https://github.com/larksuite/oapi-sdk-go
代码示例如下:
package main
import (
"context"
"fmt"
lark "github.com/larksuite/oapi-sdk-go/v3"
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"os"
)
func sendInteractiveMonitorMsg(client *lark.Client) {
// header
header := larkcard.NewMessageCardHeader().
Template(larkcard.TemplateRed).
Title(larkcard.NewMessageCardPlainText().
Content("1 级报警 - 数据平台").
Build()).
Build()
// Elements
divElement1 := larkcard.NewMessageCardDiv().
Fields([]*larkcard.MessageCardField{
larkcard.NewMessageCardField().
Text(larkcard.NewMessageCardLarkMd().
Content("**🔴 报警服务:**\nQA 7").
Build()).
IsShort(true).
Build(),
larkcard.NewMessageCardField().
Text(larkcard.NewMessageCardLarkMd().
Content("**🕐 时间:**\n2023-02-23 20:17:51").
Build()).
IsShort(true).
Build(),
larkcard.NewMessageCardField().
Text(larkcard.NewMessageCardLarkMd().
Content("**👤 一级值班:**\n[@王冰](https://open.feishu.cn/document/ugTN1YjL4UTN24CO1UjN/uUzN1YjL1cTN24SN3UjN?from=mcb)").
Build()).
IsShort(true).
Build(),
larkcard.NewMessageCardField().
Text(larkcard.NewMessageCardLarkMd().
Content("**👤 二级值班:**\n[@李天天](https://open.feishu.cn/document/ugTN1YjL4UTN24CO1UjN/uUzN1YjL1cTN24SN3UjN?from=mcb)").
Build()).
IsShort(true).
Build()}).
Build()
divElementPlainText := larkcard.NewMessageCardDiv().
Text(larkcard.NewMessageCardPlainText().Content("支付方式 支付成功率低于50%").Build())
imageElement := larkcard.NewMessageCardImage().
ImgKey("img_v2_8b2ebeaf-c97c-411d-a4dc-4323e8cba10g").
Alt(larkcard.NewMessageCardPlainText().Content("").Build()).
Build()
divElement3 := larkcard.NewMessageCardNote().
Elements([]larkcard.MessageCardNoteElement{larkcard.NewMessageCardPlainText().
Content("🔴 支付失败数 🔵 支付成功数").
Build()}).
Build()
divElement4 := larkcard.NewMessageCardAction().
Actions([]larkcard.MessageCardActionElement{larkcard.NewMessageCardEmbedButton().
Type(larkcard.MessageCardButtonTypePrimary).
Value(map[string]interface{}{"key1": "value1"}).
Text(larkcard.NewMessageCardPlainText().
Content("跟进处理").
Build()),
larkcard.NewMessageCardEmbedSelectMenuStatic().
MessageCardEmbedSelectMenuStatic(larkcard.NewMessageCardEmbedSelectMenuBase().
Options([]*larkcard.MessageCardEmbedSelectOption{larkcard.NewMessageCardEmbedSelectOption().
Value("1").
Text(larkcard.NewMessageCardPlainText().
Content("屏蔽10分钟").
Build()),
larkcard.NewMessageCardEmbedSelectOption().
Value("2").
Text(larkcard.NewMessageCardPlainText().
Content("屏蔽30分钟").
Build()),
larkcard.NewMessageCardEmbedSelectOption().
Value("3").
Text(larkcard.NewMessageCardPlainText().
Content("屏蔽1小时").
Build()),
larkcard.NewMessageCardEmbedSelectOption().
Value("4").
Text(larkcard.NewMessageCardPlainText().
Content("屏蔽24小时").
Build()),
}).
Placeholder(larkcard.NewMessageCardPlainText().
Content("暂时屏蔽报警").
Build()).
Value(map[string]interface{}{"key": "value"}).
Build()).
Build()}).
Build()
divElement5 := larkcard.NewMessageCardHr().Build()
divElement6 := larkcard.NewMessageCardDiv().
Text(larkcard.NewMessageCardLarkMd().
Content("🙋🏼 [我要反馈误报](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message-development-tutorial/introduction?from=mcb) | 📝 [录入报警处理过程](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message-development-tutorial/introduction?from=mcb)").
Build()).
Build()
// 卡片消息体
cardContent, err := larkcard.NewMessageCard().
Header(header).
Elements([]larkcard.MessageCardElement{divElement1, divElementPlainText, imageElement, divElement3, divElement4, divElement5, divElement6}).
String()
if err != nil {
fmt.Println(err)
return
}
req := larkim.NewCreateMessageReqBuilder().
ReceiveIdType(larkim.ReceiveIdTypeChatId).
Body(larkim.NewCreateMessageReqBodyBuilder().
MsgType(larkim.MsgTypeInteractive).
ReceiveId("oc_3c91943a1276c5d9fe44717deb1682b7").
Content(cardContent).
Build()).
Build()
resp, err := client.Im.Message.Create(context.Background(), req)
if err != nil {
fmt.Println(err)
return
}
if !resp.Success() {
fmt.Println(resp.Code, resp.Msg, resp.RequestId())
return
}
fmt.Println(larkcore.Prettify(resp))
fmt.Println(resp.RequestId())
}
func main() {
var appID, appSecret = os.Getenv("APP_ID"), os.Getenv("APP_SECRET")
client := lark.NewClient(appID, appSecret)
// 发送卡片消息
sendInteractiveMonitorMsg(client)
}
运行后,机器人就会给指定的接受者发送一个卡片消息,消息内容如下: