forked from sbstjn/hanu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
141 lines (116 loc) · 3.33 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
package hanu
import (
"github.com/slack-go/slack/slackevents"
"regexp"
"strings"
)
// MessageInterface defines the message interface
type MessageInterface interface {
IsMessage() bool
IsFrom(user string) bool
IsHelpRequest() bool
IsDirectMessage() bool
IsMentionFor(user string) bool
IsRelevantFor(user string) bool
Text() string
User() string
Channel() string
}
func NewMessage(ev *slackevents.MessageEvent) Message {
msg := Message{}
msg.ChannelID = ev.Channel
msg.Message = ev.Text
msg.OriginalMessage = ev.Text
msg.UserID = ev.User
msg.Type = ev.Type
return msg
}
func NewMentionMessage(ev *slackevents.AppMentionEvent) Message {
msg := Message{}
msg.ChannelID = ev.Channel
msg.Message = ev.Text
msg.OriginalMessage = ev.Text
msg.UserID = ev.User
msg.Type = ev.Type
return msg
}
// Message is the Message structure for received and sent messages using Slack
type Message struct {
ID uint64
Type string
ChannelID string
UserID string
Message string
OriginalMessage string
}
// Text returns the message text
func (m Message) Text() string {
return m.Message
}
// Channel returns the channel ID
func (m Message) Channel() string {
return m.ChannelID
}
// User returns the name of the user who sent the message
func (m Message) User() string {
return m.UserID
}
// IsMessage checks if it is a Message or some other kind of processing information
func (m Message) IsMessage() bool {
return true
}
// IsFrom checks the sender of the message
func (m Message) IsFrom(user string) bool {
return m.UserID == user
}
// SetText updates the text of a message
func (m *Message) SetText(text string) {
m.Message = text
}
// StripMention removes the mention from the message beginning
func (m *Message) StripMention(user string) string {
prefix := "<@" + user + "> "
text := m.Text()
if strings.HasPrefix(text, prefix) {
return text[len(prefix):]
}
return text
}
// StripLinkMarkup converts <http://google.com|google.com> into google.com etc.
// https://api.slack.com/docs/message-formatting#how_to_display_formatted_messages
func (m *Message) StripLinkMarkup() string {
re := regexp.MustCompile("<(.*?)>")
result := re.FindAllStringSubmatch(m.Text(), -1)
text := m.Text()
var link string
for _, c := range result {
link = c[len(c)-1]
// Done change Channel, User or Specials tags
if link[:2] == "#C" || link[:2] == "@U" || link[:1] == "!" {
continue
}
url := link
if strings.Contains(link, "|") {
splits := strings.Split(link, "|")
url = splits[1]
}
text = strings.Replace(text, "<"+link+">", url, -1)
}
return text
}
// IsHelpRequest checks if the user requests the help command
func (m Message) IsHelpRequest() bool {
return strings.HasSuffix(m.Message, "help") || strings.HasPrefix(m.Message, "help")
}
// IsDirectMessage checks if the message is received using a direct messaging channel
func (m Message) IsDirectMessage() bool {
return strings.HasPrefix(m.ChannelID, "D")
}
// IsMentionFor checks if the given user was mentioned with the message
func (m Message) IsMentionFor(user string) bool {
return strings.HasPrefix(m.OriginalMessage, "<@"+user+">")
}
// IsRelevantFor checks if the message is relevant for a user
func (m Message) IsRelevantFor(user string) bool {
return m.IsMessage() && !m.IsFrom(user) && (m.IsDirectMessage() || m.IsMentionFor(user))
}