前言
本文我们基于飞书开放平台提供的服务端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"
"net/http"
"os"
)
type TextMsg struct {
Text string `json:"text,omitempty"`
}
func main() {
// 创建 Client
var appID, appSecret = os.Getenv("APP_ID"), os.Getenv("APP_SECRET")
client := lark.NewClient(appID, appSecret)
// 构建body
body := map[string]interface{}{}
body["msg_type"] = larkim.MsgTypeText
body["content"] = TextMsg{Text: "经老板研究决定,我们过年放假一个月"}
body["open_ids"] = []string{"ou_c245b0a7dff2725cfa2fb104f8b48b9d", "ou_c245b0a7dff2725cfa2fb104f8b48b94"}
// 发起请求
resp, err := client.Do(context.Background(),
&larkcore.ApiReq{
HttpMethod: http.MethodPost,
ApiPath: "/open-apis/message/v4/batch_send",
Body: body,
SupportedAccessTokenTypes: []larkcore.AccessTokenType{larkcore.AccessTokenTypeTenant},
},
)
// 错误处理
if err != nil {
fmt.Println(err)
return
}
// 获取请求 ID
fmt.Println(resp.RequestId())
// 处理请求结果
fmt.Println(resp.StatusCode) // http status code
fmt.Println(resp.Header) // http header
fmt.Println(string(resp.RawBody)) // http body,二进制数据
}