161 lines
4.5 KiB
Go
161 lines
4.5 KiB
Go
package lib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
dingtalkoauth2_1_0 "github.com/alibabacloud-go/dingtalk/oauth2_1_0"
|
|
dingtalkrobot_1_0 "github.com/alibabacloud-go/dingtalk/robot_1_0"
|
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
"github.com/patrickmn/go-cache"
|
|
"github.com/tidwall/gjson"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type Dingding struct {
|
|
AppKey string
|
|
AppSecret string
|
|
accessTokenKey string
|
|
}
|
|
|
|
// dingding token 缓存
|
|
var DingCache = cache.New(2*time.Hour, 5*time.Minute)
|
|
|
|
func NewDing(appKey, appSecret string) (*Dingding, error) {
|
|
ding := &Dingding{
|
|
AppKey: appKey,
|
|
AppSecret: appSecret,
|
|
accessTokenKey: appKey + "_token",
|
|
}
|
|
return ding, nil
|
|
}
|
|
|
|
//获取access_token
|
|
func (this *Dingding) GetAccessToken(forceUpdate bool) (string, error) {
|
|
if !forceUpdate {
|
|
tokenCache, ok := DingCache.Get(this.accessTokenKey)
|
|
if ok {
|
|
token := tokenCache.(string)
|
|
return token, nil
|
|
}
|
|
}
|
|
|
|
getAccessTokenRequest := &dingtalkoauth2_1_0.GetAccessTokenRequest{
|
|
AppKey: tea.String(this.AppKey),
|
|
AppSecret: tea.String(this.AppSecret),
|
|
}
|
|
config := &openapi.Config{}
|
|
config.Protocol = tea.String("https")
|
|
config.RegionId = tea.String("central")
|
|
client, err := dingtalkoauth2_1_0.NewClient(config)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return "", err
|
|
}
|
|
result, err := client.GetAccessToken(getAccessTokenRequest)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return "", err
|
|
}
|
|
access := result.Body.String()
|
|
accessToken := gjson.Get(access, "accessToken").String()
|
|
//缓存2小时
|
|
DingCache.Set(this.accessTokenKey, accessToken, 2*time.Hour)
|
|
return accessToken, nil
|
|
}
|
|
|
|
//批量发送人与机器人会话
|
|
func (this *Dingding) BatchSend(robotCode string, userIds []string, content string) (string, error) {
|
|
// 构建请求参数
|
|
params := map[string]interface{}{
|
|
"content": content,
|
|
}
|
|
jsonParams, err := json.Marshal(params)
|
|
|
|
batchSendOTOHeaders := &dingtalkrobot_1_0.BatchSendOTOHeaders{}
|
|
toUserId := make([]*string, 0)
|
|
for _, uid := range userIds {
|
|
toUserId = append(toUserId, tea.String(uid))
|
|
}
|
|
batchSendOTORequest := &dingtalkrobot_1_0.BatchSendOTORequest{
|
|
MsgParam: tea.String(string(jsonParams)),
|
|
MsgKey: tea.String("sampleText"),
|
|
RobotCode: tea.String(robotCode),
|
|
UserIds: toUserId,
|
|
}
|
|
config := &openapi.Config{}
|
|
config.Protocol = tea.String("https")
|
|
config.RegionId = tea.String("central")
|
|
|
|
client, err := dingtalkrobot_1_0.NewClient(config)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return "", err
|
|
}
|
|
accessToken, _ := this.GetAccessToken(false)
|
|
|
|
for i := 0; i <= 1; i++ {
|
|
batchSendOTOHeaders.XAcsDingtalkAccessToken = tea.String(accessToken)
|
|
result, err := client.BatchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, &util.RuntimeOptions{})
|
|
if err != nil {
|
|
log.Println(err)
|
|
sdkErr, ok := err.(*tea.SDKError)
|
|
if ok && *sdkErr.Code == "InvalidAuthentication" {
|
|
accessToken, _ = this.GetAccessToken(true)
|
|
continue
|
|
}
|
|
return "", err
|
|
}
|
|
res := result.Body.String()
|
|
log.Println("ding:", res)
|
|
return res, nil
|
|
}
|
|
return "", errors.New("发送机器人消息失败!")
|
|
}
|
|
|
|
//发送群聊信息
|
|
func (this *Dingding) SendGroup(openConversationId, robotCode, content string) (string, error) {
|
|
// 构建请求参数
|
|
params := map[string]interface{}{
|
|
"content": content,
|
|
}
|
|
jsonParams, err := json.Marshal(params)
|
|
orgGroupSendHeaders := &dingtalkrobot_1_0.OrgGroupSendHeaders{}
|
|
orgGroupSendRequest := &dingtalkrobot_1_0.OrgGroupSendRequest{
|
|
MsgParam: tea.String(string(jsonParams)),
|
|
MsgKey: tea.String("sampleText"),
|
|
RobotCode: tea.String(robotCode),
|
|
OpenConversationId: tea.String(openConversationId),
|
|
}
|
|
config := &openapi.Config{}
|
|
config.Protocol = tea.String("https")
|
|
config.RegionId = tea.String("central")
|
|
|
|
client, err := dingtalkrobot_1_0.NewClient(config)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return "", err
|
|
}
|
|
accessToken, _ := this.GetAccessToken(false)
|
|
|
|
for i := 0; i <= 1; i++ {
|
|
orgGroupSendHeaders.XAcsDingtalkAccessToken = tea.String(accessToken)
|
|
result, err := client.OrgGroupSendWithOptions(orgGroupSendRequest, orgGroupSendHeaders, &util.RuntimeOptions{})
|
|
if err != nil {
|
|
log.Println(err)
|
|
sdkErr, ok := err.(*tea.SDKError)
|
|
if ok && *sdkErr.Code == "InvalidAuthentication" {
|
|
accessToken, _ = this.GetAccessToken(true)
|
|
continue
|
|
}
|
|
return "", err
|
|
}
|
|
res := result.Body.String()
|
|
log.Println("ding:", res)
|
|
return res, nil
|
|
}
|
|
return "", errors.New("发送机器人群聊消息失败!")
|
|
}
|