admin_backend/middleware/feishu/client/client.go
2026-04-15 14:48:13 +08:00

85 lines
1.9 KiB
Go

package client
import (
"backend/middleware/feishu/data"
"context"
"fmt"
"github.com/google/uuid"
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"
)
type client struct {
*lark.Client
}
var C *client
func init() {
C = &client{lark.NewClient(data.APP_ID, data.APP_SECRET)}
}
func GetClient() *client {
return C
}
func (c *client) SendMsg(open_id, msg_type, content string) error {
// 创建 Client
uuidVal := uuid.New().String()
req := larkim.NewCreateMessageReqBuilder().
ReceiveIdType(`open_id`).
Body(larkim.NewCreateMessageReqBodyBuilder().
ReceiveId(open_id).
MsgType(msg_type).
Content(content).
Uuid(uuidVal).
Build()).
Build()
// 发起请求
resp, err := c.Im.V1.Message.Create(context.Background(), req)
// 处理错误
if err != nil {
fmt.Println(err)
return err
}
// 服务端错误处理
if !resp.Success() {
fmt.Printf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
return fmt.Errorf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
}
return nil
}
func (c *client) SendGroupMsg(chat_id, msg_type, content string) error {
// 创建 Client
uuidVal := uuid.New().String()
req := larkim.NewCreateMessageReqBuilder().
ReceiveIdType(`chat_id`).
Body(larkim.NewCreateMessageReqBodyBuilder().
ReceiveId(chat_id).
MsgType(msg_type).
Content(content).
Uuid(uuidVal).
Build()).
Build()
// 发起请求
resp, err := c.Im.V1.Message.Create(context.Background(), req)
// 处理错误
if err != nil {
fmt.Println(err)
return err
}
// 服务端错误处理
if !resp.Success() {
fmt.Printf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
return fmt.Errorf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
}
return nil
}