66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package dingtalk_contact
|
|
|
|
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 Client struct {
|
|
config *config.Config
|
|
cli *contact.Client
|
|
}
|
|
|
|
func NewContactClient(config *config.Config) (*Client, 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 &Client{config: config, cli: c}, nil
|
|
}
|
|
|
|
type SearchUserReq struct {
|
|
FullMatchField int32
|
|
QueryWord string
|
|
Offset int32
|
|
Size int32
|
|
}
|
|
|
|
type SearchUserResp struct {
|
|
Body interface{}
|
|
}
|
|
|
|
func (c *Client) 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
|
|
}
|