前言
本文我们基于飞书开放平台提供了服务端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"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"os"
)
func sendTextMsg(client *lark.Client) {
// Build the text message content
//content := "{\"text\":\"hello,world\\n<at user_id=\\\"ou_c245b0a7dff2725cfa2fb104f8b48b9d\\\">加多</at>\"}"
content := larkim.NewTextMsgBuilder().
Text("hello,world").
AtUser("ou_c245b0a7dff2725cfa2fb104f8b48b9d", "加多").
Build()
resp, err := client.Im.Message.Create(context.Background(), larkim.NewCreateMessageReqBuilder().
ReceiveIdType(larkim.ReceiveIdTypeOpenId).
Body(larkim.NewCreateMessageReqBodyBuilder().
MsgType(larkim.MsgTypeText).
ReceiveId("ou_c245b0a7dff2725cfa2fb104f8b48b9d").
Content(content).
Build()).
Build())
if err != nil {
fmt.Println(err)
return
}
if !resp.Success() {
fmt.Println(resp.Code, resp.Msg, resp.RequestId())
return
}
fmt.Println(resp.Data.MessageId)
fmt.Println(larkcore.Prettify(resp))
fmt.Println(resp.RequestId())
}
func sendPostMsgUseBuilder(client *lark.Client) {
// 第一行
// 文本 &超链接
zhCnPostText := &larkim.MessagePostText{Text: "第一行:", UnEscape: false}
enUsPostText := &larkim.MessagePostText{Text: "英文内容", UnEscape: false}
zhCnPostA := &larkim.MessagePostA{Text: "超链接", Href: "http://www.baidu.com", UnEscape: false}
enUsPostA := &larkim.MessagePostA{Text: "link", Href: "http://www.baidu.com", UnEscape: false}
// At人
zhCnPostAt := &larkim.MessagePostAt{UserId: "ou_c245b0a7dff2725cfa2fb104f8b48b9d", UserName: "加多"}
enCnPostAt := &larkim.MessagePostAt{UserId: "ou_c245b0a7dff2725cfa2fb104f8b48b9d", UserName: "jiaduo"}
// 图片
zhCnPostImage := &larkim.MessagePostImage{ImageKey: "img_v2_a66c4f79-c7b5-4899-b5e3-622766c4f82g"}
enCnPostImage := &larkim.MessagePostImage{ImageKey: "img_v2_a66c4f79-c7b5-4899-b5e3-622766c4f82g"}
// 第二行
// 文本 &超链接
zhCnPostText21 := &larkim.MessagePostText{Text: "第二行:", UnEscape: false}
enUsPostText21 := &larkim.MessagePostText{Text: "英文内容", UnEscape: false}
// 图片
zhCnPostImage2 := &larkim.MessagePostImage{ImageKey: "img_v2_a66c4f79-c7b5-4899-b5e3-622766c4f82g"}
enCnPostImage2 := &larkim.MessagePostImage{ImageKey: "img_v2_a66c4f79-c7b5-4899-b5e3-622766c4f82g"}
// 中文
zhCn := larkim.NewMessagePostContent().
ContentTitle("我是一个标题").
AppendContent([]larkim.MessagePostElement{zhCnPostText, zhCnPostA, zhCnPostAt}).
AppendContent([]larkim.MessagePostElement{zhCnPostImage}).
AppendContent([]larkim.MessagePostElement{zhCnPostText21}).
AppendContent([]larkim.MessagePostElement{zhCnPostImage2}).
Build()
// 英文
enUs := larkim.NewMessagePostContent().
ContentTitle("im a title").
AppendContent([]larkim.MessagePostElement{enUsPostA, enUsPostText, enCnPostAt}).
AppendContent([]larkim.MessagePostElement{enCnPostImage}).
AppendContent([]larkim.MessagePostElement{enUsPostText21}).
AppendContent([]larkim.MessagePostElement{enCnPostImage2}).
Build()
// 构建消息体
postText, err := larkim.NewMessagePost().ZhCn(zhCn).EnUs(enUs).Build()
if err != nil {
fmt.Println(err)
return
}
resp, err := client.Im.Message.Create(context.Background(), larkim.NewCreateMessageReqBuilder().
ReceiveIdType(larkim.ReceiveIdTypeOpenId).
Body(larkim.NewCreateMessageReqBodyBuilder().
MsgType(larkim.MsgTypePost).
ReceiveId("ou_c245b0a7dff2725cfa2fb104f8b48b9d").
Content(postText).
Build()).
Build())
if err != nil {
fmt.Println(err)
return
}
if resp.Success() {
fmt.Println(larkcore.Prettify(resp))
fmt.Println(*resp.Data.MessageId)
} else {
fmt.Println(resp.RequestId(), resp.Msg, resp.Code)
}
}
func main() {
// 创建 api client
var appID, appSecret = os.Getenv("APP_ID"), os.Getenv("APP_SECRET")
client := lark.NewClient(appID, appSecret)
// 发送文本消息
sendTextMsg(client)
// 发送富文本消息
sendPostMsgUseBuilder(client)
}
运行后,机器人就会给指定的接受者发送两个消息,消息内容如下: