ai_scheduler/internal/pkg/dingtalk/notable_client.go

120 lines
2.9 KiB
Go

package dingtalk
import (
"ai_scheduler/internal/config"
errorcode "ai_scheduler/internal/data/error"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
notable "github.com/alibabacloud-go/dingtalk/notable_1_0"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
type NotableClient struct {
config *config.Config
cli *notable.Client
}
func NewNotableClient(config *config.Config) (*NotableClient, error) {
cfg := &openapi.Config{
AccessKeyId: tea.String(config.Tools.DingTalkBot.APIKey),
AccessKeySecret: tea.String(config.Tools.DingTalkBot.APISecret),
Protocol: tea.String("https"),
RegionId: tea.String("central"),
}
c, err := notable.NewClient(cfg)
if err != nil {
return nil, err
}
return &NotableClient{config: config, cli: c}, nil
}
type UpdateRecordReq struct {
BaseId string
SheetId string
RecordId string
OperatorId string
CreatorUnionId string
}
type UpdateRecordsserResp struct {
Body interface{}
}
func (c *NotableClient) UpdateRecord(accessToken string, req *UpdateRecordReq) (bool, error) {
headers := &notable.UpdateRecordsHeaders{}
headers.XAcsDingtalkAccessToken = tea.String(accessToken)
resp, err := c.cli.UpdateRecordsWithOptions(
tea.String(req.BaseId),
tea.String(req.SheetId),
&notable.UpdateRecordsRequest{
OperatorId: tea.String(req.OperatorId),
Records: []*notable.UpdateRecordsRequestRecords{
{
Fields: map[string]any{
"提交人": []map[string]any{
{
"unionId": req.CreatorUnionId,
},
},
},
Id: tea.String(req.RecordId),
},
},
}, headers, &util.RuntimeOptions{})
if err != nil {
return false, err
}
if resp.Body == nil {
return false, errorcode.ParamErrf("empty response body")
}
return true, nil
}
type InsertRecordReq struct {
BaseId string
SheetIdOrName string
OperatorId string
CreatorUnionId string
Content string
}
func (c *NotableClient) InsertRecord(accessToken string, req *InsertRecordReq) (string, error) {
// 默认使用“数据表”
if req.SheetIdOrName == "" {
req.SheetIdOrName = "数据表"
}
headers := &notable.InsertRecordsHeaders{}
headers.XAcsDingtalkAccessToken = tea.String(accessToken)
resp, err := c.cli.InsertRecordsWithOptions(
tea.String(req.BaseId),
tea.String(req.SheetIdOrName),
&notable.InsertRecordsRequest{
OperatorId: tea.String(req.OperatorId),
Records: []*notable.InsertRecordsRequestRecords{
{
Fields: map[string]any{
"需求内容": req.Content,
"提交人": []map[string]any{
{
"unionId": req.CreatorUnionId,
},
},
},
},
},
}, headers, &util.RuntimeOptions{})
if err != nil {
return "", err
}
if resp.Body == nil || resp.Body.Value == nil || len(resp.Body.Value) == 0 {
return "", errorcode.ParamErrf("empty response body")
}
return *resp.Body.Value[0].Id, nil
}