支持飞书卡片消息

支持飞书卡片消息
This commit is contained in:
zhang fugui 2026-02-28 19:49:17 +08:00 committed by GitHub
parent 27e988c484
commit d1e8136836
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -32,6 +32,15 @@ type FeishuChannel struct {
cancel context.CancelFunc
}
type InteractiveMarkdownContent struct {
Elements []CardElement `json:"elements"`
}
type CardElement struct {
Tag string `json:"tag"`
Content string `json:"content"`
}
func NewFeishuChannel(cfg config.FeishuConfig, bus *bus.MessageBus) (*FeishuChannel, error) {
base := channels.NewBaseChannel("feishu", cfg, bus, cfg.AllowFrom,
channels.WithGroupTrigger(cfg.GroupTrigger),
@ -102,17 +111,23 @@ func (c *FeishuChannel) Send(ctx context.Context, msg bus.OutboundMessage) error
return fmt.Errorf("chat ID is empty")
}
payload, err := json.Marshal(map[string]string{"text": msg.Content})
var element CardElement
element.Tag = "markdown"
element.Content = msg.Content
var cardContent InteractiveMarkdownContent
cardContent.Elements = []CardElement{element}
// 将cardContent 序列化为JSON字符串
cardContentJSON, err := json.Marshal(cardContent)
if err != nil {
return fmt.Errorf("failed to marshal feishu content: %w", err)
return fmt.Errorf("failed to marshal feishu card content: %w", err)
}
req := larkim.NewCreateMessageReqBuilder().
ReceiveIdType(larkim.ReceiveIdTypeChatId).
Body(larkim.NewCreateMessageReqBodyBuilder().
ReceiveId(msg.ChatID).
MsgType(larkim.MsgTypeText).
Content(string(payload)).
MsgType(larkim.MsgTypeInteractive).
Content(string(cardContentJSON)).
Uuid(fmt.Sprintf("picoclaw-%d", time.Now().UnixNano())).
Build()).
Build()