ai_scheduler/internal/pkg/dingtalk/old_client.go

179 lines
4.8 KiB
Go

package dingtalk
// 旧版sdk客户端 - 在用,勿删!
import (
"ai_scheduler/internal/config"
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"github.com/faabiosr/cachego/file"
"github.com/fastwego/dingding"
)
type OldClient struct {
config *config.Config
cli *dingding.Client
atm *dingding.DefaultAccessTokenManager
}
func NewOldClient(config *config.Config) *OldClient {
atm := &dingding.DefaultAccessTokenManager{
Id: config.Tools.DingTalkBot.APIKey,
Name: "access_token",
GetRefreshRequestFunc: func() *http.Request {
params := url.Values{}
params.Add("appkey", config.Tools.DingTalkBot.APIKey)
params.Add("appsecret", config.Tools.DingTalkBot.APISecret)
req, _ := http.NewRequest(http.MethodGet, dingding.ServerUrl+"/gettoken?"+params.Encode(), nil)
return req
},
Cache: file.New(os.TempDir()),
}
return &OldClient{config: config, cli: dingding.NewClient(atm), atm: atm}
}
type UserDetail struct {
UserID string `json:"userid"`
Name string `json:"name"`
UnionID string `json:"unionid"`
}
func (c *OldClient) do(ctx context.Context, method, path string, body []byte) ([]byte, error) {
var r io.Reader
if body != nil {
r = bytes.NewReader(body)
}
req, err := http.NewRequestWithContext(ctx, method, path, r)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return c.cli.Do(req)
}
func (c *OldClient) QueryUserDetails(ctx context.Context, userId string) (*UserDetail, error) {
body := struct {
UserId string `json:"userid"`
Language string `json:"language,omitempty"`
}{UserId: userId, Language: "zh_CN"}
b, _ := json.Marshal(body)
res, err := c.do(ctx, http.MethodPost, "/topapi/v2/user/get", b)
if err != nil {
return nil, err
}
var resp struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
Result UserDetail `json:"result"`
}
if err := json.Unmarshal(res, &resp); err != nil {
return nil, err
}
if resp.Code != 0 {
return nil, errors.New(resp.Msg)
}
return &resp.Result, nil
}
func (c *OldClient) QueryUserDetailsByMobile(ctx context.Context, mobile string) (*UserDetail, error) {
body := struct {
Mobile string `json:"mobile"`
}{Mobile: mobile}
b, _ := json.Marshal(body)
res, err := c.do(ctx, http.MethodPost, "/topapi/v2/user/getbymobile", b)
if err != nil {
return nil, err
}
var resp struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
Result UserDetail `json:"result"`
}
if err := json.Unmarshal(res, &resp); err != nil {
return nil, err
}
if resp.Code != 0 {
return nil, errors.New(resp.Msg)
}
return &resp.Result, nil
}
// GetAccessToken 通过 fastwego 的 AccessTokenManager 获取当前可用 access_token
func (c *OldClient) GetAccessToken() (string, error) {
return c.atm.GetAccessToken()
}
// CreateInternalGroupConversation 创建企业内部群聊
func (c *OldClient) CreateInternalGroupConversation(ctx context.Context, groupName string, userIds []string) (chatId, openConversationId string, err error) {
body := struct {
Name string `json:"name"`
Owner string `json:"owner"`
UserIds []string `json:"useridlist"`
ShowHistoryType int `json:"showHistoryType"`
Searchable int `json:"searchable"`
ValidationType int `json:"validationType"`
MentionAllAuthority int `json:"mentionAllAuthority"`
ManagementType int `json:"managementType"`
ChatBannedType int `json:"chatBannedType"`
}{
Name: groupName,
Owner: userIds[0],
UserIds: userIds,
}
b, _ := json.Marshal(body)
var res []byte
res, err = c.do(ctx, http.MethodPost, "/chat/create", b)
if err != nil {
return
}
var resp struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
ChatId string `json:"chatid"`
OpenConversationId string `json:"openConversationId"`
ConversationTag int `json:"conversationTag"`
}
if err = json.Unmarshal(res, &resp); err != nil {
return
}
if resp.Code != 0 {
return "", "", errors.New(resp.Msg)
}
return resp.ChatId, resp.OpenConversationId, nil
}
// 获取入群二维码链接
func (c *OldClient) GetJoinGroupQrcode(ctx context.Context, chatId, userId string) (string, error) {
body := struct {
ChatId string `json:"chatid"`
UserId string `json:"userid"`
}{ChatId: chatId, UserId: userId}
b, _ := json.Marshal(body)
res, err := c.do(ctx, http.MethodPost, "/topapi/chat/qrcode/get", b)
if err != nil {
return "", err
}
var resp struct {
Code int `json:"errcode"`
Msg string `json:"errmsg"`
Result string `json:"result"`
}
if err := json.Unmarshal(res, &resp); err != nil {
return "", err
}
if resp.Code != 0 {
return "", errors.New(resp.Msg)
}
return resp.Result, nil
}