61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"ai_scheduler/internal/config"
|
|
errorcode "ai_scheduler/internal/data/error"
|
|
"encoding/json"
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
robot "github.com/alibabacloud-go/dingtalk/robot_1_0"
|
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
)
|
|
|
|
type RobotClient struct {
|
|
config *config.Config
|
|
cli *robot.Client
|
|
}
|
|
|
|
func NewRobotClient(config *config.Config) (*RobotClient, 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 := robot.NewClient(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &RobotClient{config: config, cli: c}, nil
|
|
}
|
|
|
|
type SendGroupMessagesReq struct {
|
|
MsgKey string
|
|
MsgParam map[string]any
|
|
OpenConversationId string
|
|
RobotCode string
|
|
}
|
|
|
|
func (c *RobotClient) SendGroupMessages(accessToken string, req *SendGroupMessagesReq) (string, error) {
|
|
headers := &robot.OrgGroupSendHeaders{}
|
|
headers.XAcsDingtalkAccessToken = tea.String(accessToken)
|
|
msgParamBytes, _ := json.Marshal(req.MsgParam)
|
|
msgParamJson := string(msgParamBytes)
|
|
resp, err := c.cli.OrgGroupSendWithOptions(&robot.OrgGroupSendRequest{
|
|
MsgKey: tea.String(req.MsgKey),
|
|
MsgParam: tea.String(msgParamJson),
|
|
OpenConversationId: tea.String(req.OpenConversationId),
|
|
RobotCode: tea.String(req.RobotCode),
|
|
}, headers, &util.RuntimeOptions{})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if resp.Body == nil {
|
|
return "", errorcode.ParamErrf("empty response body")
|
|
}
|
|
|
|
return *resp.Body.ProcessQueryKey, nil
|
|
}
|