ai_scheduler/internal/pkg/dingtalk/contact_client.go

66 lines
1.7 KiB
Go

package dingtalk
import (
"ai_scheduler/internal/config"
errorcode "ai_scheduler/internal/data/error"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
contact "github.com/alibabacloud-go/dingtalk/contact_1_0"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
type ContactClient struct {
config *config.Config
cli *contact.Client
}
func NewContactClient(config *config.Config) (*ContactClient, 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 := contact.NewClient(cfg)
if err != nil {
return nil, err
}
return &ContactClient{config: config, cli: c}, nil
}
type SearchUserReq struct {
FullMatchField int32
QueryWord string
Offset int32
Size int32
}
type SearchUserResp struct {
Body interface{}
}
func (c *ContactClient) SearchUserOne(accessToken string, name string) (string, error) {
headers := &contact.SearchUserHeaders{}
headers.XAcsDingtalkAccessToken = tea.String(accessToken)
resp, err := c.cli.SearchUserWithOptions(&contact.SearchUserRequest{
FullMatchField: tea.Int32(1),
QueryWord: tea.String(name),
Offset: tea.Int32(0),
Size: tea.Int32(1),
}, headers, &util.RuntimeOptions{})
if err != nil {
return "", err
}
if resp.Body == nil {
return "", errorcode.ParamErr("empty response body")
}
if len(resp.Body.List) == 0 {
return "", errorcode.ParamErr("empty user list")
}
userId := resp.Body.List[0]
return *userId, nil
}