61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"ai_scheduler/internal/config"
|
|
errorcode "ai_scheduler/internal/data/error"
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
card "github.com/alibabacloud-go/dingtalk/card_1_0"
|
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
)
|
|
|
|
type CardClient struct {
|
|
config *config.Config
|
|
cli *card.Client
|
|
oauth2Client *Oauth2Client
|
|
}
|
|
|
|
func NewCardClient(config *config.Config, oauth2Client *Oauth2Client) (*CardClient, 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 := card.NewClient(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &CardClient{config: config, cli: c, oauth2Client: oauth2Client}, nil
|
|
}
|
|
|
|
// 创建并投放卡片
|
|
func (c *CardClient) CreateAndDeliver(req AppKey, cardData *card.CreateAndDeliverRequest) (bool, error) {
|
|
// 获取token
|
|
accessToken, err := c.oauth2Client.GetAccessToken(req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// 调用API
|
|
resp, err := c.cli.CreateAndDeliverWithOptions(
|
|
cardData,
|
|
&card.CreateAndDeliverHeaders{XAcsDingtalkAccessToken: tea.String(accessToken)},
|
|
&util.RuntimeOptions{},
|
|
)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if resp.Body == nil {
|
|
return false, errorcode.ParamErrf("empty response body")
|
|
}
|
|
if !*resp.Body.Success {
|
|
return false, errorcode.ParamErrf("create and deliver failed")
|
|
}
|
|
|
|
return true, nil
|
|
}
|