-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
169 lines (135 loc) · 3.54 KB
/
types.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
package wechat
import (
"encoding/xml"
"reflect"
)
type MessageHandler func(m *MessageReceive) MessageReply
type Message interface {
Type() string
}
type messageBase struct {
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"` //sender, openID
CreateTime int64 `xml:"CreateTime"`
MsgType string `xml:"MsgType"`
}
func (m messageBase) Type() string {
return m.MsgType
}
func (m messageBase) IsEvent() bool {
return m.MsgType == "event"
}
type MessageReceive struct {
//I do not know what are these for, just leave it untouched
XMLName struct{} `xml:"xml"`
Text string `xml:",chardata"`
//comment struct elements
messageBase
MsgId int64 `xml:"MsgId"`
//MsgType: text
Content string `xml:"Content"`
//below types consist of it
MediaId string `xml:"MediaId"`
//MsgType: image
PicUrl string `xml:"PicUrl"`
//MsgType: voice
Format string `xml:"Format"` //amr, speex
Recognition string `xml:"Recognition"` //available when voice recognition is on
//MsgType: video, shortvideo
ThumbMediaId string `xml:"ThumbMediaId"`
//MsgType: location
LocationX string `xml:"Location_X"`
LocationY string `xml:"Location_Y"`
Scale string `xml:"Scale"`
Label string `xml:"Label"`
//MsgType: link
Title string `xml:"Title"`
Description string `xml:"Description"`
URL string `xml:"Url"`
///////////////////////////
//MsgType: event
Event string `xml:"Event"` //subscribe, unsubscribe
//Event: subscript(first time to subscribe), SCAN(has subscribed)
//Event: CLICK, VIEW
EventKey string `xml:"EventKey"`
Ticket string `xml:"Ticket"`
//Event: LOCATION(upload position event)
Latitude string `xml:"Latitude"`
Longitude string `xml:"Longitude"`
Precision string `xml:"Precision"`
}
type MessageReply interface {
Type() string
}
type xmlMsgReply struct {
XMLName struct{} `xml:"xml"`
messageBase
MsgWrapper messageWrapper
}
type messageWrapper struct {
Msg Message
}
func (m messageWrapper) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if article, ok := m.Msg.(Articles); ok {
err := e.EncodeElement(len(article.Items), xml.StartElement{Name: xml.Name{Local: "ArticleCount"}})
if err != nil {
return err
}
}
return e.EncodeElement(m.Msg, xml.StartElement{
Name: xml.Name{Local: reflect.TypeOf(m.Msg).Name()},
})
}
var _ xml.Marshaler = &messageWrapper{}
type Text struct {
Content string `xml:"Content"`
}
func (t Text) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(t.Content, xml.StartElement{Name: xml.Name{Local: "Content"}})
}
var _ xml.Marshaler = Text{}
func (Text) Type() string {
return "text"
}
type Image struct {
MediaId string `xml:"MediaId"`
}
func (Image) Type() string {
return "image"
}
type Voice struct {
MediaID string `xml:"MediaId"`
}
func (Voice) Type() string {
return "voice"
}
type Video struct {
MediaID string `xml:"MediaId"`
Title string `xml:"Title"`
Description string `xml:"Description"`
}
func (Video) Type() string {
return "video"
}
type Music struct {
Title string `xml:"Title"`
Description string `xml:"Description"`
MusicURL string `xml:"MusicUrl"`
HqURL string `xml:"HQMusicUrl"`
ThumbID string `xml:"ThumbMediaId"`
}
func (Music) Type() string {
return "music"
}
type ArticleItem struct {
Title string `xml:"Title"`
Description string `xml:"Description"`
PicURL string `xml:"PicUrl"`
URL string `xml:"Url"`
}
type Articles struct {
Items []ArticleItem `xml:"item"`
}
func (Articles) Type() string {
return "news"
}