forked from xen0n/go-workwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
260 lines (243 loc) · 6.35 KB
/
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package workwx
import (
"errors"
)
// SendTextMessage 发送文本消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendTextMessage(
recipient *Recipient,
content string,
isSafe bool,
) error {
return c.sendMessage(recipient, "text", map[string]interface{}{"content": content}, isSafe)
}
// SendImageMessage 发送图片消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendImageMessage(
recipient *Recipient,
mediaID string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"image",
map[string]interface{}{
"media_id": mediaID,
}, isSafe,
)
}
// SendVoiceMessage 发送语音消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendVoiceMessage(
recipient *Recipient,
mediaID string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"voice",
map[string]interface{}{
"media_id": mediaID,
}, isSafe,
)
}
// SendVideoMessage 发送视频消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendVideoMessage(
recipient *Recipient,
mediaID string,
description string,
title string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"video",
map[string]interface{}{
"media_id": mediaID,
"description": description, // TODO: 零值
"title": title, // TODO: 零值
}, isSafe,
)
}
// SendFileMessage 发送文件消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendFileMessage(
recipient *Recipient,
mediaID string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"file",
map[string]interface{}{
"media_id": mediaID,
}, isSafe,
)
}
// SendTextCardMessage 发送文本卡片消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendTextCardMessage(
recipient *Recipient,
title string,
description string,
url string,
buttonText string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"textcard",
map[string]interface{}{
"title": title,
"description": description,
"url": url,
"btntxt": buttonText, // TODO: 零值
}, isSafe,
)
}
// SendNewsMessage 发送图文消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendNewsMessage(
recipient *Recipient,
title string,
description string,
url string,
picURL string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"news",
map[string]interface{}{
"title": title,
"description": description, // TODO: 零值
"url": url,
"picurl": picURL, // TODO: 零值
}, isSafe,
)
}
// SendMPNewsMessage 发送 mpnews 类型的图文消息
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendMPNewsMessage(
recipient *Recipient,
title string,
thumbMediaID string,
author string,
sourceContentURL string,
content string,
digest string,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"mpnews",
map[string]interface{}{
// TODO: 支持发送多条图文
"articles": []interface{}{
map[string]interface{}{
"title": title,
"thumb_media_id": thumbMediaID,
"author": author, // TODO: 零值
"content_source_url": sourceContentURL, // TODO: 零值
"content": content,
"digest": digest,
},
},
}, isSafe,
)
}
// SendMarkdownMessage 发送 Markdown 消息
//
// 仅支持 Markdown 的子集,详见[官方文档](https://work.weixin.qq.com/api/doc#90002/90151/90854/%E6%94%AF%E6%8C%81%E7%9A%84markdown%E8%AF%AD%E6%B3%95)。
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) SendMarkdownMessage(
recipient *Recipient,
content string,
isSafe bool,
) error {
return c.sendMessage(recipient, "markdown", map[string]interface{}{"content": content}, isSafe)
}
// SendTaskCardMessage 发送 任务卡片 消息
func (c *WorkwxApp) SendTaskCardMessage(
recipient *Recipient,
title string,
description string,
url string,
taskid string,
btn []TaskCardBtn,
isSafe bool,
) error {
return c.sendMessage(
recipient,
"taskcard",
map[string]interface{}{
"title": title,
"description": description,
"url": url,
"task_id": taskid,
"btn": btn,
}, isSafe,
)
}
// sendMessage 发送消息底层接口
//
// 收件人参数如果仅设置了 `ChatID` 字段,则为【发送消息到群聊会话】接口调用;
// 否则为单纯的【发送应用消息】接口调用。
func (c *WorkwxApp) sendMessage(
recipient *Recipient,
msgtype string,
content map[string]interface{},
isSafe bool,
) error {
isApichatSendRequest := false
if !recipient.isValidForMessageSend() {
if !recipient.isValidForAppchatSend() {
// TODO: better error
return errors.New("recipient invalid for message sending")
}
// 发送给群聊
isApichatSendRequest = true
}
req := reqMessage{
ToUser: recipient.UserIDs,
ToParty: recipient.PartyIDs,
ToTag: recipient.TagIDs,
ChatID: recipient.ChatID,
AgentID: c.AgentID,
MsgType: msgtype,
Content: content,
IsSafe: isSafe,
}
var resp respMessageSend
var err error
if isApichatSendRequest {
resp, err = c.execAppchatSend(req)
} else {
resp, err = c.execMessageSend(req)
}
if err != nil {
return err
}
// TODO: what to do with resp?
_ = resp
return nil
}