-
Notifications
You must be signed in to change notification settings - Fork 17
/
messenger.go
171 lines (135 loc) · 4.54 KB
/
messenger.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
170
171
package handler
import (
"github.com/gobridge/gopherbot/mparser"
"github.com/slack-go/slack/slackevents"
)
// ChannelType represents where a message was sent.
type ChannelType uint8
const (
// ChannelUnknown is for when the type wasn't provided.
ChannelUnknown ChannelType = iota
// ChannelPublic is for public channels.
ChannelPublic ChannelType = 1<<iota - 1
// ChannelPrivate is for private channels, called groups by Slack API
ChannelPrivate
// ChannelDM is for direct messages, or called IMs by Slack API
ChannelDM
// ChannelGroupDM is for group direct messages, or called MPIMs by Slack API
ChannelGroupDM
// ChannelAppHome is for messages sent to the AppHome.
ChannelAppHome
)
func (c ChannelType) String() string {
switch c {
case ChannelPublic:
return "public"
case ChannelPrivate:
return "private"
case ChannelDM:
return "dm"
case ChannelGroupDM:
return "group_dm"
case ChannelAppHome:
return "app_home"
default:
return "unknown"
}
}
func strToChan(s string) ChannelType {
switch s {
case "channel":
return ChannelPublic
case "group":
return ChannelPrivate
case "im":
return ChannelDM
case "mpim":
return ChannelGroupDM
case "app_home":
return ChannelAppHome
default:
return ChannelUnknown
}
}
// Messenger is the interface to represent an incoming message.
type Messenger interface {
// ChannelID is the ID of the channel where the message was sent. This could
// be a DM and not a channel, so be sure to check ChannelType()
ChannelID() string
// ChannelType is the kind of channel this is, also includes DMs.
ChannelType() ChannelType
// UserID is the ID of the user who sent the message.
UserID() string
// ThreadTS is the parent message ID, indicating we are in a thread
ThreadTS() string
// MessageTS is the message ID. If ThreadTS is empty string, we're not in a
// thread
MessageTS() string
// AllMentions contains all parsed mentions in a message, including the bot
// user, channels, etc.
AllMentions() []mparser.Mention
// UserMentions() contains only non-bot user mentions in the message.
UserMentions() []mparser.Mention
// Text is the text with any mentions removed, and leading/trailing
// whitespace removed.
Text() string
// RawText is the raw Slack messages with no mentions removed.
RawText() string
// BotMentioned indicates if the bot was mentioned in the message.
BotMentioned() bool
// Files are any files attached to the message
Files() []slackevents.File
}
// Message is a singular message to be processed. Satisfies Messenger interface.
type Message struct {
channelID string
channelType ChannelType
userID string
threadTS string
messageTS string
subType string
allMentions []mparser.Mention
userMentions []mparser.Mention
text string
botMentioned bool
rawText string
files []slackevents.File
}
var _ Messenger = Message{}
// NewMessage generates a new message from the various inputs.
func NewMessage(channelID, channelType, userID, threadTS, messageTS, subType, text string, files []slackevents.File) Message {
return Message{
channelID: channelID,
channelType: strToChan(channelType),
userID: userID,
threadTS: threadTS,
messageTS: messageTS,
subType: subType,
rawText: text,
files: files,
}
}
// ChannelID satisfies the Messenger interface.
func (m Message) ChannelID() string { return m.channelID }
// ChannelType satisfies the Messenger interface.
func (m Message) ChannelType() ChannelType { return m.channelType }
// UserID satisfies the Messenger interface.
func (m Message) UserID() string { return m.userID }
// ThreadTS satisfies the Messenger interface.
func (m Message) ThreadTS() string { return m.threadTS }
// MessageTS satisfies the Messenger interface.
func (m Message) MessageTS() string { return m.messageTS }
// SubType satisfies the Messenger interface.
func (m Message) SubType() string { return m.subType }
// AllMentions satisfies the Messenger interface.
func (m Message) AllMentions() []mparser.Mention { return m.allMentions }
// UserMentions satisfies the Messenger interface.
func (m Message) UserMentions() []mparser.Mention { return m.userMentions }
// Text satisfies the Messenger interface.
func (m Message) Text() string { return m.text }
// RawText satisfies the Messenger interface.
func (m Message) RawText() string { return m.rawText }
// BotMentioned satisfies the Messenger interface.
func (m Message) BotMentioned() bool { return m.botMentioned }
// Files satisfies the Messenger interface.
func (m Message) Files() []slackevents.File { return m.files }