-
Notifications
You must be signed in to change notification settings - Fork 119
/
message_event.go
138 lines (117 loc) · 4.03 KB
/
message_event.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
package slacker
import (
"fmt"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
)
// MessageEvent contains details common to message based events, including the
// raw event as returned from Slack along with the corresponding event type.
// The struct should be kept minimal and only include data that is commonly
// used to prevent frequent type assertions when evaluating the event.
type MessageEvent struct {
// Channel ID where the message was sent
ChannelID string
// Channel contains information about the channel
Channel *slack.Channel
// User ID of the sender
UserID string
// UserProfile contains all the information details of a given user
UserProfile *slack.UserProfile
// Text is the unalterted text of the message, as returned by Slack
Text string
// TimeStamp is the message timestamp. For events that do not support
// threading (eg. slash commands) this will be unset.
// will be left unset.
TimeStamp string
// ThreadTimeStamp is the message thread timestamp. For events that do not
// support threading (eg. slash commands) this will be unset.
ThreadTimeStamp string
// Data is the raw event data returned from slack. Using Type, you can assert
// this into a slackevents *Event struct.
Data any
// Type is the type of the event, as returned by Slack. For instance,
// `app_mention` or `message`
Type string
// BotID of the bot that sent this message. If a bot did not send this
// message, this will be an empty string.
BotID string
}
// InThread indicates if a message event took place in a thread.
func (e *MessageEvent) InThread() bool {
return isMessageInThread(e.ThreadTimeStamp, e.TimeStamp)
}
// IsBot indicates if the message was sent by a bot
func (e *MessageEvent) IsBot() bool {
return e.BotID != ""
}
// newMessageEvent creates a new message event structure
func newMessageEvent(logger Logger, slackClient *slack.Client, event any) *MessageEvent {
var messageEvent *MessageEvent
switch ev := event.(type) {
case *slackevents.MessageEvent:
messageEvent = &MessageEvent{
ChannelID: ev.Channel,
Channel: getChannel(logger, slackClient, ev.Channel),
UserID: ev.User,
UserProfile: getUserProfile(logger, slackClient, ev.User),
Text: ev.Text,
Data: event,
Type: ev.Type,
TimeStamp: ev.TimeStamp,
ThreadTimeStamp: ev.ThreadTimeStamp,
BotID: ev.BotID,
}
case *slackevents.AppMentionEvent:
messageEvent = &MessageEvent{
ChannelID: ev.Channel,
Channel: getChannel(logger, slackClient, ev.Channel),
UserID: ev.User,
UserProfile: getUserProfile(logger, slackClient, ev.User),
Text: ev.Text,
Data: event,
Type: ev.Type,
TimeStamp: ev.TimeStamp,
ThreadTimeStamp: ev.ThreadTimeStamp,
BotID: ev.BotID,
}
case *slack.SlashCommand:
messageEvent = &MessageEvent{
ChannelID: ev.ChannelID,
Channel: getChannel(logger, slackClient, ev.ChannelID),
UserID: ev.UserID,
UserProfile: getUserProfile(logger, slackClient, ev.UserID),
Text: fmt.Sprintf("%s %s", ev.Command[1:], ev.Text),
Data: event,
Type: socketmode.RequestTypeSlashCommands,
}
default:
return nil
}
return messageEvent
}
func getChannel(logger Logger, slackClient *slack.Client, channelID string) *slack.Channel {
if len(channelID) == 0 {
return nil
}
channel, err := slackClient.GetConversationInfo(&slack.GetConversationInfoInput{
ChannelID: channelID,
IncludeLocale: false,
IncludeNumMembers: false})
if err != nil {
logger.Errorf("unable to get channel info for %s: %v\n", channelID, err)
return nil
}
return channel
}
func getUserProfile(logger Logger, slackClient *slack.Client, userID string) *slack.UserProfile {
if len(userID) == 0 {
return nil
}
user, err := slackClient.GetUserInfo(userID)
if err != nil {
logger.Errorf("unable to get user info for %s: %v\n", userID, err)
return nil
}
return &user.Profile
}