-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtop_api_message.go
124 lines (110 loc) · 3.44 KB
/
top_api_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Author Kevin Zhu
*
* Direct questions, comments to <ipandtcp@gmail.com>
*/
package godingtalk
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"strings"
)
const (
topAPIMsgAsyncSendMethod = "dingtalk.corp.message.corpconversation.asyncsend"
topAPIMsgGetResultMethod = "dingtalk.corp.message.corpconversation.getsendresult"
topAPIMsgGetprogressMethod = "dingtalk.corp.message.corpconversation.getsendprogress"
)
type topAPIMsgSendResponse struct {
topAPIErrResponse
OK struct {
ErrCode int `json:"ding_open_errcode"`
ErrMsg string `json:"error_msg"`
Success bool `json:"success"`
TaskID int `json:"task_id"`
} `json:"result"`
}
// mgType 消息类型:text;iamge;voice;file;link;oa;markdown;action_card
// userList 接收推送的UID 列表
// deptList 接收推送的部门ID列表
// toAll 是否发送给所有用户
// msgContent 消息内容
// If success return task_id, or is error is not nil when errored
func (c *DingTalkClient) TopAPIMsgSend(msgType string, userList []string, deptList []int, toAll bool, msgContent interface{}) (int, error) {
var resp topAPIMsgSendResponse
if len(userList) > 20 || len(deptList) > 20 {
return 0, errors.New("Can't more than 20 users or departments at once")
}
mcontent, err := json.Marshal(msgContent)
if err != nil {
return 0, err
}
toAllStr := "false"
if toAll {
toAllStr = "true"
}
form := url.Values{
"method": {topAPIMsgAsyncSendMethod},
"agent_id": {c.AgentID},
"userid_list": {strings.Join(userList, ",")},
"to_all_user": {toAllStr},
"msgtype": {msgType},
"msgcontent": {string(mcontent)},
}
if len(deptList) > 0 {
var deptListStr string
for _, dept := range deptList {
deptListStr = strconv.Itoa(dept) + ","
}
deptListStr = string([]uint8(deptListStr)[0 : len(deptListStr)-1])
form.Set("dept_id_list", deptListStr)
}
return resp.OK.TaskID, c.topAPIRequest(form, &resp)
}
type TopAPIMsgGetSendResult struct {
topAPIErrResponse
OK struct {
ErrCode int `json:"ding_open_errcode"`
ErrMsg string `json:"error_msg"`
Success bool `json:"success"`
SendResult struct {
InvalidUserIDList []string `json:"invalid_user_id_list"`
ForbiddenUserIDList []string `json:"forbidden_user_id_list"`
FaildedUserIDList []string `json:"failed_user_id_list"`
ReadUserIDLIst []string `json:"read_user_id_list"`
UnreadUserIDList []string `json:"unread_user_id_list"`
InvalidDeptIDList []int `json:"invalid_dept_id_list"`
} `json:"send_result"`
} `json:"result"`
}
func (c *DingTalkClient) TopAPIMsgGetSendResult(taskID int) (TopAPIMsgGetSendResult, error) {
var resp TopAPIMsgGetSendResult
form := url.Values{
"method": {topAPIMsgGetResultMethod},
"agent_id": {c.AgentID},
"task_id": {strconv.Itoa(taskID)},
}
return resp, c.topAPIRequest(form, &resp)
}
type TopAPIMsgGetSendProgress struct {
topAPIErrResponse
OK struct {
ErrCode int `json:"ding_open_errcode"`
ErrMsg string `json:"error_msg"`
Success bool `json:"success"`
Progress struct {
Percent int `json:"progress_in_percent"`
Status int `json:"status"`
} `json:"progress"`
} `json:"result"`
}
func (c *DingTalkClient) TopAPIMsgGetSendProgress(taskID int) (TopAPIMsgGetSendProgress, error) {
var resp TopAPIMsgGetSendProgress
form := url.Values{
"method": {topAPIMsgGetprogressMethod},
"agent_id": {c.AgentID},
"task_id": {strconv.Itoa(taskID)},
}
return resp, c.topAPIRequest(form, &resp)
}