forked from xen0n/go-workwx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrx_msg.go
More file actions
165 lines (138 loc) · 4.76 KB
/
rx_msg.go
File metadata and controls
165 lines (138 loc) · 4.76 KB
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
package workwx
import (
"encoding/xml"
"fmt"
"strings"
"time"
)
// RxMessage 一条接收到的消息
type RxMessage struct {
// FromUserID 发送者的 UserID
FromUserID string
// SendTime 消息发送时间
SendTime time.Time
// MsgType 消息类型
MsgType MessageType
// MsgID 消息 ID
MsgID int64
// AgentID 企业应用 ID,可在应用的设置页面查看
AgentID int64
// Event 事件类型 MsgType为event存在
Event EventType
// ChangeType 变更类型 Event为change_external_contact存在
ChangeType ChangeType
extras messageKind
}
func fromEnvelope(body []byte) (*RxMessage, error) {
// extract common part
var common rxMessageCommon
err := xml.Unmarshal(body, &common)
if err != nil {
return nil, err
}
// deal with polymorphic message types
extras, err := extractMessageExtras(common, body)
if err != nil {
return nil, err
}
// assemble message object
var obj RxMessage
{
// let's force people to think about timezones okay?
// -- let's not
sendTime := time.Unix(common.CreateTime, 0) // in time.Local
obj = RxMessage{
FromUserID: common.FromUserName,
SendTime: sendTime,
MsgType: common.MsgType,
MsgID: common.MsgID,
AgentID: common.AgentID,
Event: common.Event,
ChangeType: common.ChangeType,
extras: extras,
}
}
return &obj, nil
}
func (m *RxMessage) String() string {
var sb strings.Builder
_, _ = fmt.Fprintf(
&sb,
"RxMessage { FromUserID: %#v, SendTime: %d, MsgType: %#v, MsgID: %d, AgentID: %d, Event: %#v, ChangeType: %#v, ",
m.FromUserID,
m.SendTime.UnixNano(),
m.MsgType,
m.MsgID,
m.AgentID,
m.Event,
m.ChangeType,
)
m.extras.formatInto(&sb)
sb.WriteString(" }")
return sb.String()
}
// Text 如果消息为文本类型,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) Text() (TextMessageExtras, bool) {
y, ok := m.extras.(TextMessageExtras)
return y, ok
}
// Image 如果消息为图片类型,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) Image() (ImageMessageExtras, bool) {
y, ok := m.extras.(ImageMessageExtras)
return y, ok
}
// Voice 如果消息为语音类型,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) Voice() (VoiceMessageExtras, bool) {
y, ok := m.extras.(VoiceMessageExtras)
return y, ok
}
// Video 如果消息为视频类型,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) Video() (VideoMessageExtras, bool) {
y, ok := m.extras.(VideoMessageExtras)
return y, ok
}
// Location 如果消息为位置类型,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) Location() (LocationMessageExtras, bool) {
y, ok := m.extras.(LocationMessageExtras)
return y, ok
}
// Link 如果消息为链接类型,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) Link() (LinkMessageExtras, bool) {
y, ok := m.extras.(LinkMessageExtras)
return y, ok
}
// EventAddExternalContact 如果消息为添加企业客户事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventAddExternalContact() (EventAddExternalContact, bool) {
y, ok := m.extras.(EventAddExternalContact)
return y, ok
}
// EventEditExternalContact 如果消息为编辑企业客户事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventEditExternalContact() (EventEditExternalContact, bool) {
y, ok := m.extras.(EventEditExternalContact)
return y, ok
}
// EventDelExternalContact 如果消息为删除企业客户事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventDelExternalContact() (EventDelExternalContact, bool) {
y, ok := m.extras.(EventDelExternalContact)
return y, ok
}
// EventDelFollowUser 如果消息为删除跟进成员事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventDelFollowUser() (EventDelFollowUser, bool) {
y, ok := m.extras.(EventDelFollowUser)
return y, ok
}
// EventAddHalfExternalContact 如果消息为外部联系人免验证添加成员事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventAddHalfExternalContact() (EventAddHalfExternalContact, bool) {
y, ok := m.extras.(EventAddHalfExternalContact)
return y, ok
}
// EventTransferFail 如果消息为客户接替失败事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventTransferFail() (EventTransferFail, bool) {
y, ok := m.extras.(EventTransferFail)
return y, ok
}
// EventChangeExternalChat 如果消息为客户群变更事件,则拿出相应的消息参数,否则返回 nil, false
func (m *RxMessage) EventChangeExternalChat() (EventChangeExternalChat, bool) {
y, ok := m.extras.(EventChangeExternalChat)
return y, ok
}