forked from hugozhu/godingtalk
/
api_message.go
196 lines (179 loc) · 5.23 KB
/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package godingtalk
import (
"net/url"
"strconv"
)
//SendAppMessage is 发送企业会话消息
func (c *DingTalkClient) SendAppMessage(agentID string, touser string, msg string) error {
if agentID == "" {
agentID = c.AgentID
}
var data OAPIResponse
request := map[string]interface{}{
"touser": touser,
"agentid": agentID,
"msgtype": "text",
"text": map[string]interface{}{
"content": msg,
},
}
err := c.httpRPC("message/send", nil, request, &data)
return err
}
//SendAppOAMessage is 发送OA消息
func (c *DingTalkClient) SendAppOAMessage(agentID string, touser string, msg OAMessage) error {
if agentID == "" {
agentID = c.AgentID
}
var data OAPIResponse
request := map[string]interface{}{
"touser": touser,
"agentid": agentID,
"msgtype": "oa",
"oa": msg,
}
err := c.httpRPC("message/send", nil, request, &data)
return err
}
//SendAppLinkMessage is 发送企业会话链接消息
func (c *DingTalkClient) SendAppLinkMessage(agentID, touser string, title, text string, picUrl, url string) error {
if agentID == "" {
agentID = c.AgentID
}
var data OAPIResponse
request := map[string]interface{}{
"touser": touser,
"agentid": agentID,
"msgtype": "link",
"link": map[string]string{
"messageUrl": url,
"picUrl": picUrl,
"title": title,
"text": text,
},
}
err := c.httpRPC("message/send", nil, request, &data)
return err
}
//SendTextMessage is 发送普通文本消息
func (c *DingTalkClient) SendTextMessage(sender string, cid string, msg string) (data MessageResponse, err error) {
request := map[string]interface{}{
"chatid": cid,
"sender": sender,
"msgtype": "text",
"text": map[string]interface{}{
"content": msg,
},
}
err = c.httpRPC("chat/send", nil, request, &data)
return data, err
}
//SendImageMessage is 发送图片消息
func (c *DingTalkClient) SendImageMessage(sender string, cid string, mediaID string) (data MessageResponse, err error) {
request := map[string]interface{}{
"chatid": cid,
"sender": sender,
"msgtype": "image",
"image": map[string]string{
"media_id": mediaID,
},
}
err = c.httpRPC("chat/send", nil, request, &data)
return data, err
}
//SendVoiceMessage is 发送语音消息
func (c *DingTalkClient) SendVoiceMessage(sender string, cid string, mediaID string, duration string) (data MessageResponse, err error) {
request := map[string]interface{}{
"chatid": cid,
"sender": sender,
"msgtype": "voice",
"voice": map[string]string{
"media_id": mediaID,
"duration": duration,
},
}
err = c.httpRPC("chat/send", nil, request, &data)
return data, err
}
//SendFileMessage is 发送文件消息
func (c *DingTalkClient) SendFileMessage(sender string, cid string, mediaID string) (data MessageResponse, err error) {
request := map[string]interface{}{
"chatid": cid,
"sender": sender,
"msgtype": "file",
"file": map[string]string{
"media_id": mediaID,
},
}
err = c.httpRPC("chat/send", nil, request, &data)
return data, err
}
//SendLinkMessage is 发送链接消息
func (c *DingTalkClient) SendLinkMessage(sender string, cid string, mediaID string, url string, title string, text string) (data MessageResponse, err error) {
request := map[string]interface{}{
"chatid": cid,
"sender": sender,
"msgtype": "link",
"link": map[string]string{
"messageUrl": url,
"picUrl": mediaID,
"title": title,
"text": text,
},
}
err = c.httpRPC("chat/send", nil, request, &data)
return data, err
}
//OAMessage is the Message for OA
type OAMessage struct {
URL string `json:"message_url"`
PcURL string `json:"pc_message_url"`
Head struct {
BgColor string `json:"bgcolor,omitempty"`
Text string `json:"text,omitempty"`
} `json:"head,omitempty"`
Body struct {
Title string `json:"title,omitempty"`
Form []OAMessageForm `json:"form,omitempty"`
Rich OAMessageRich `json:"rich,omitempty"`
Content string `json:"content,omitempty"`
Image string `json:"image,omitempty"`
FileCount int `json:"file_count,omitempty"`
Author string `json:"author,omitempty"`
} `json:"body,omitempty"`
}
type OAMessageForm struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type OAMessageRich struct {
Num string `json:"num,omitempty"`
Unit string `json:"body,omitempty"`
}
func (m *OAMessage) AppendFormItem(key string, value string) {
f := OAMessageForm{Key: key, Value: value}
if m.Body.Form == nil {
m.Body.Form = []OAMessageForm{}
}
m.Body.Form = append(m.Body.Form, f)
}
//SendOAMessage is 发送OA消息
func (c *DingTalkClient) SendOAMessage(sender string, cid string, msg OAMessage) (data MessageResponse, err error) {
request := map[string]interface{}{
"chatid": cid,
"sender": sender,
"msgtype": "oa",
"oa": msg,
}
err = c.httpRPC("chat/send", nil, request, &data)
return data, err
}
//GetMessageReadList is 获取已读列表
func (c *DingTalkClient) GetMessageReadList(messageID string, cursor int, size int) (data MessageReadListResponse, err error) {
params := url.Values{}
params.Add("messageId", messageID)
params.Add("cursor", strconv.Itoa(cursor))
params.Add("size", strconv.Itoa(size))
err = c.httpRPC("chat/getReadList", params, nil, &data)
return data, err
}