forked from abourget/slick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
159 lines (130 loc) · 3.38 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package slick
import (
"fmt"
"regexp"
"strings"
"github.com/nlopes/slack"
)
type botReply struct {
To string
Text string
}
type Message struct {
*slack.Msg
SubMessage *slack.Msg
bot *Bot
MentionsMe bool
IsEdit bool
FromMe bool
FromUser *slack.User
FromChannel *Channel
// Match contains the result of
// Listener.Matches.FindStringSubmatch(msg.Text), when `Matches`
// is set on the `Listener`.
Match []string
}
func (msg *Message) IsPrivate() bool {
return strings.HasPrefix(msg.Channel, "D")
}
func (msg *Message) ContainsAnyCased(strs []string) bool {
for _, s := range strs {
if strings.Contains(msg.Text, s) {
return true
}
}
return false
}
func (msg *Message) ContainsAny(strs []string) bool {
lowerStr := strings.ToLower(msg.Text)
for _, s := range strs {
lowerInput := strings.ToLower(s)
if strings.Contains(lowerStr, lowerInput) {
return true
}
}
return false
}
func (msg *Message) ContainsAll(strs []string) bool {
lowerStr := strings.ToLower(msg.Text)
for _, s := range strs {
lowerInput := strings.ToLower(s)
if !strings.Contains(lowerStr, lowerInput) {
return false
}
}
return true
}
func (msg *Message) Contains(s string) bool {
lowerStr := strings.ToLower(msg.Text)
lowerInput := strings.ToLower(s)
if strings.Contains(lowerStr, lowerInput) {
return true
}
return false
}
func (msg *Message) HasPrefix(prefix string) bool {
return strings.HasPrefix(msg.Text, prefix)
}
func (msg *Message) AddReaction(emoticon string) *Message {
msg.bot.Slack.AddReaction(emoticon, slack.NewRefToMessage(msg.Channel, msg.Timestamp))
return msg
}
func (msg *Message) RemoveReaction(emoticon string) *Message {
msg.bot.Slack.RemoveReaction(emoticon, slack.NewRefToMessage(msg.Channel, msg.Timestamp))
return msg
}
func (msg *Message) ListenReaction(reactListen *ReactionListener) {
msg.bot.ListenReaction(msg.Timestamp, reactListen)
}
func (msg *Message) Reply(text string, v ...interface{}) *Reply {
to := msg.User
if msg.Channel != "" {
to = msg.Channel
}
text = Format(text, v...)
return msg.bot.SendOutgoingMessage(text, to)
}
func (msg *Message) ReplyPrivately(text string, v ...interface{}) *Reply {
text = Format(text, v...)
return msg.bot.SendPrivateMessage(msg.User, text)
}
// ReplyMention replies with a @mention named prefixed, when replying
// in public. When replying in private, nothing is added.
func (msg *Message) ReplyMention(text string, v ...interface{}) *Reply {
if msg.IsPrivate() {
return msg.Reply(text, v...)
}
prefix := ""
if msg.FromUser != nil {
prefix = fmt.Sprintf("<@%s> ", msg.FromUser.Name)
}
return msg.Reply(fmt.Sprintf("%s%s", prefix, text), v...)
}
func (msg *Message) String() string {
return fmt.Sprintf("%#v", msg)
}
func (msg *Message) applyMentionsMe(bot *Bot) {
if msg.IsPrivate() {
msg.MentionsMe = true
}
m := reAtMention.FindStringSubmatch(msg.Text)
if m != nil && m[1] == bot.Myself.ID {
msg.MentionsMe = true
}
}
func (msg *Message) applyFromMe(bot *Bot) {
if msg.User != "" && msg.User == bot.Myself.ID {
msg.FromMe = true
}
}
var reAtMention = regexp.MustCompile(`<@([A-Z0-9]+)(|([^>]+))>`)
// Format conditionally formats using fmt.Sprintf if there is more
// than one argument, otherwise returns the first parameter
// uninterpreted.
func Format(s string, v ...interface{}) string {
count := len(v)
if count == 0 {
return s
}
return fmt.Sprintf(s, v...)
}