-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmsg_xml_reply.go
284 lines (251 loc) · 6.9 KB
/
msg_xml_reply.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* 消息/事件的响应消息体构造和序列化为XML,被msg_parse.go调用
*/
package wxmsg
import (
"text/template"
"fmt"
"time"
"bytes"
)
const (
// reply template name
RTN_ENCRYPTED_REPLY = iota
RTN_TEXT
RTN_IMAGE
RTN_VOICE
RTN_VIDEO
RTN_MUSIC
RTN_NEWS
RTN_TOTAL
)
var strTmpls = map[int]string {
RTN_ENCRYPTED_REPLY: `<xml>
<Encrypt><![CDATA[{{.encryptedText}}]]></Encrypt>
<MsgSignature><![CDATA[{{.signature}}]]></MsgSignature>
<TimeStamp>{{.timestamp}}</TimeStamp>
<Nonce><![CDATA[{{.nonce}}]]></Nonce>
</xml>`,
RTN_TEXT: `<xml>
<ToUserName><![CDATA[{{.ToUserName}}]]></ToUserName>
<FromUserName><![CDATA[{{.FromUserName}}]]></FromUserName>
<CreateTime>{{.CreateTime}}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{{.Content}}]]></Content>
</xml>`,
RTN_IMAGE: `<xml>
<ToUserName><![CDATA[{{.ToUserName}}]]></ToUserName>
<FromUserName><![CDATA[{{.FromUserName}}]]></FromUserName>
<CreateTime>{{.CreateTime}}</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[{{.MediaId}}]]></MediaId>
</Image>
</xml>`,
RTN_VOICE: `<xml>
<ToUserName><![CDATA[{{.ToUserName}}]]></ToUserName>
<FromUserName><![CDATA[{{.FromUserName}}]]></FromUserName>
<CreateTime>{{.CreateTime}}</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[{{.MediaId}}]]></MediaId>
</Voice>
</xml>`,
RTN_VIDEO: `<xml>
<ToUserName><![CDATA[{{.ToUserName}}]]></ToUserName>
<FromUserName><![CDATA[{{.FromUserName}}]]></FromUserName>
<CreateTime>{{.CreateTime}}</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[{{.MediaId}}]]></MediaId>
<Title><![CDATA[{{.Title}}]]></Title>
<Description><![CDATA[{{.Desc}}]]></Description>
</Video>
</xml>`,
RTN_MUSIC: `<xml>
<ToUserName><![CDATA[{{.ToUserName}}]]></ToUserName>
<FromUserName><![CDATA[{{.FromUserName}}]]></FromUserName>
<CreateTime>{{.CreateTime}}</CreateTime>
<MsgType><![CDATA[music]]></MsgType><Music>
<Title><![CDATA[{{.Title}}]]></Title>
<Description><![CDATA[{{.Desc}}]]></Description>
<MusicUrl><![CDATA[{{.MusicUrl}}]]></MusicUrl>
<HQMusicUrl><![CDATA[{{.HQMusicUrl}}]]></HQMusicUrl>
<ThumbMediaId><![CDATA[{{.ThumbMediaId}}]]></ThumbMediaId>
</Music>
</xml>`,
RTN_NEWS: `<xml>
<ToUserName><![CDATA[{{.ToUserName}}]]></ToUserName>
<FromUserName><![CDATA[{{.FromUserName}}]]></FromUserName>
<CreateTime>{{.CreateTime}}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>{{ len .Articles }}</ArticleCount>
<Articles>
{{range .Articles}}
<item>
<Title><![CDATA[{{.Title}}]]></Title>
<Description><![CDATA[{{.Desc}}]]></Description>
<PicUrl><![CDATA[{{.PicUrl}}]]></PicUrl>
<Url><![CDATA[{{.Url}}]]></Url>
</item>
{{end}}
</Articles>
</xml>`,
}
var replyTmpls []*template.Template
func init() {
replyTmpls = make([]*template.Template, RTN_TOTAL)
var err error
for idx, tmpl := range strTmpls {
replyTmpls[idx], err = template.New("ROSBIT").Parse(tmpl)
if err != nil {
fmt.Printf("failed to parse template #%d, %s: %v\n", idx, tmpl, err)
}
}
}
func executeTmpl(tmplIdx int, data interface{}) []byte {
bb := bytes.Buffer{}
replyTmpls[tmplIdx].Execute(&bb, data)
return bb.Bytes()
}
// 根据响应消息生成加密消息
func (p *WxAppIdMsgParser) EncryptReply(replyMsg []byte, timestamp string, nonce string) []byte {
cryptedText, signature := encryptMsg(p.wxParams, replyMsg, timestamp, nonce)
return executeTmpl(RTN_ENCRYPTED_REPLY, map[string]string{
"encryptedText": cryptedText,
"signature": signature,
"timestamp": timestamp,
"nonce": nonce,
})
}
// 所有响应消息都必须实现的接口:序列化为XML。由此消息处理过程为 ReceiveMsg -> [处理] -> ReplyMsg
type ReplyMsg interface {
ToXML() []byte
}
type ReplyMsgBase struct {
ToUserName string
FromUserName string
CreateTime int64
}
func newMsgBase(toUserName, fromUserName string) ReplyMsgBase {
return ReplyMsgBase{ToUserName:toUserName, FromUserName:fromUserName, CreateTime:time.Now().Unix()}
}
// ---------- reply Text Message ---------------
type ReplyTextMsg struct {
ReplyMsgBase
Content string
}
func NewReplyTextMsg(toUserName, fromUserName, content string) *ReplyTextMsg {
return &ReplyTextMsg{
ReplyMsgBase:newMsgBase(toUserName, fromUserName),
Content:content,
}
}
func (msg *ReplyTextMsg) ToXML() []byte {
return executeTmpl(RTN_TEXT, msg)
}
// ------------------ reply only "success" ----------
type SuccessTextMsg struct {
}
func NewSuccessMsg() *SuccessTextMsg {
return &SuccessTextMsg{}
}
func (msg *SuccessTextMsg) ToXML() []byte {
return SUCCESS_TEXT
}
// ----------------- reply image message ----------
type ReplyImageMsg struct {
ReplyMsgBase
MediaId string
}
func NewReplyImageMsg(toUserName, fromUserName, mediaId string) *ReplyImageMsg {
return &ReplyImageMsg{
ReplyMsgBase:newMsgBase(toUserName, fromUserName),
MediaId:mediaId,
}
}
func (msg *ReplyImageMsg) ToXML() []byte {
return executeTmpl(RTN_IMAGE, msg)
}
// ----------------- reply void message ----------------
type ReplyVoiceMsg struct {
ReplyMsgBase
MediaId string
}
func NewReplyVoiceMsg(toUserName, fromUserName, mediaId string) *ReplyVoiceMsg{
return &ReplyVoiceMsg{
ReplyMsgBase:newMsgBase(toUserName, fromUserName),
MediaId:mediaId,
}
}
func (msg *ReplyVoiceMsg) ToXML() []byte {
return executeTmpl(RTN_VOICE, msg)
}
// ---------------- reply video mesage -----------------
type ReplyVideoMsg struct {
ReplyMsgBase
MediaId string
Title string
Desc string
}
func NewReplyVideoMsg(toUserName, fromUserName, mediaId, title, desc string) *ReplyVideoMsg {
return &ReplyVideoMsg{
ReplyMsgBase:newMsgBase(toUserName, fromUserName),
MediaId:mediaId,
Title:title,
Desc:desc,
}
}
func (msg *ReplyVideoMsg) ToXML() []byte {
return executeTmpl(RTN_VIDEO, msg)
}
// ----------------- reply music message -----------------
type ReplyMusicMsg struct {
ReplyMsgBase
ThumbMediaId string
MusicUrl string
HQMusicUrl string
Title string
Desc string
}
func NewReplyMusicMsg(toUserName, fromUserName, thumbMediaId, musicUrl, HQMusicUrl, title, desc string) *ReplyMusicMsg {
return &ReplyMusicMsg{
ReplyMsgBase:newMsgBase(toUserName, fromUserName),
ThumbMediaId:thumbMediaId,
MusicUrl:musicUrl,
HQMusicUrl:HQMusicUrl,
Title:title,
Desc:desc,
}
}
func (msg *ReplyMusicMsg) ToXML() []byte {
return executeTmpl(RTN_MUSIC, msg)
}
// ------------ reply news message ---------------
type NewsArticle struct {
Title string
Desc string
PicUrl string
Url string
}
func NewNewsArticle(title, desc, picUrl, url string) *NewsArticle {
return &NewsArticle {
Title:title,
Desc:desc,
PicUrl:picUrl,
Url:url,
}
}
type ReplyNewsMsg struct {
ReplyMsgBase
Articles []*NewsArticle
}
func NewReplyNewsMsg(toUserName, fromUserName string, articles []*NewsArticle) *ReplyNewsMsg {
return &ReplyNewsMsg{
ReplyMsgBase:newMsgBase(toUserName, fromUserName),
Articles:articles,
}
}
func (msg *ReplyNewsMsg) ToXML() []byte {
return executeTmpl(RTN_NEWS, msg)
}