forked from eatmoreapple/openwechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_processor.go
139 lines (120 loc) · 3.46 KB
/
message_processor.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
package openwechat
import (
"encoding/json"
"html"
"strings"
)
type MessageProcessor interface {
ProcessMessage(msg *Message)
}
type MessageProcessorGroup []MessageProcessor
func (g MessageProcessorGroup) ProcessMessage(msg *Message) {
for _, processor := range g {
processor.ProcessMessage(msg)
}
}
// 保存消息原始内容
type messageRowContentProcessor struct{}
func (m *messageRowContentProcessor) ProcessMessage(msg *Message) {
raw, _ := json.Marshal(msg)
msg.Raw = raw
msg.RawContent = msg.Content
}
// 保存发送者在群里的用户名
type senderInGroupMessageProcessor struct{}
func (s *senderInGroupMessageProcessor) ProcessMessage(msg *Message) {
if !msg.IsSendByGroup() || msg.IsSystem() || msg.IsSendBySelf() {
return
}
data := strings.Split(msg.Content, ":<br/>")
if len(data) < 2 {
return
}
msg.Content = strings.Join(data[1:], "")
msg.senderUserNameInGroup = data[0]
}
// 检查消息是否被@了, 不是特别严谨
type atMessageProcessor struct{}
func (g *atMessageProcessor) ProcessMessage(msg *Message) {
if !msg.IsSendByGroup() {
return
}
if msg.IsSystem() {
return
}
if msg.IsSendBySelf() {
// 这块不严谨,但是只能这么干了
msg.isAt = strings.Contains(msg.Content, "@") || strings.Contains(msg.Content, "\u2005")
return
}
if strings.Contains(msg.Content, "@") {
sender, err := msg.Sender()
if err == nil {
receiver := sender.MemberList.SearchByUserName(1, msg.ToUserName)
if receiver != nil {
displayName := receiver.First().DisplayName
if displayName == "" {
displayName = receiver.First().NickName
}
var atFlag string
msgContent := FormatEmoji(msg.Content)
atName := FormatEmoji(displayName)
if strings.Contains(msgContent, "\u2005") {
atFlag = "@" + atName + "\u2005"
} else {
atFlag = "@" + atName
}
msg.isAt = strings.Contains(msgContent, atFlag) || strings.HasSuffix(msgContent, atFlag)
}
}
}
}
// 处理消息中的换行符
type wrapLineMessageProcessor struct{}
func (w *wrapLineMessageProcessor) ProcessMessage(msg *Message) {
msg.Content = strings.Replace(msg.Content, `<br/>`, "\n", -1)
}
// 处理消息中的html转义字符
type unescapeHTMLMessageProcessor struct{}
func (u *unescapeHTMLMessageProcessor) ProcessMessage(msg *Message) {
msg.Content = html.UnescapeString(msg.Content)
}
// 处理消息中的emoji表情
type emojiMessageProcessor struct{}
func (e *emojiMessageProcessor) ProcessMessage(msg *Message) {
msg.Content = FormatEmoji(msg.Content)
}
// 尝试获取群聊中的消息的发送者
type tryToFindGroupMessageProcessor struct{}
func (t *tryToFindGroupMessageProcessor) ProcessMessage(msg *Message) {
if msg.IsSendByGroup() {
if msg.FromUserName == msg.Owner().UserName {
return
}
// 首先尝试从缓存里面查找, 如果没有找到则从服务器获取
members, err := msg.Owner().Members()
if err != nil {
return
}
_, exist := members.GetByUserName(msg.FromUserName)
if !exist {
owner := msg.Owner()
// 找不到, 从服务器获取
user := newUser(owner, msg.FromUserName)
_ = user.Detail()
owner.members = owner.members.Append(user)
owner.groups = owner.members.Groups()
}
}
}
var (
defaultMessageProcessor MessageProcessor = MessageProcessorGroup{
&messageRowContentProcessor{},
&senderInGroupMessageProcessor{},
&atMessageProcessor{},
&wrapLineMessageProcessor{},
&unescapeHTMLMessageProcessor{},
&emojiMessageProcessor{},
&tryToFindGroupMessageProcessor{},
}
)