-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackeventstorage.go
198 lines (166 loc) · 4.91 KB
/
slackeventstorage.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"time"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
)
type SlackMessageEvent struct {
DateTime civil.DateTime
Event string
User string
Channel string
MessageID string
ParentID string
Reaction string
}
// Save implements the ValueSaver interface.
func (i *SlackMessageEvent) Save() (map[string]bigquery.Value, string, error) {
return map[string]bigquery.Value{
"datetime": i.DateTime,
"event": i.Event,
"user": i.User,
"channel": i.Channel,
"messageid": i.MessageID,
"parentid": i.ParentID,
"reaction": i.Reaction,
}, bigquery.NoDedupeID, nil
}
type SlackEventStorage struct {
BigqueryClient *bigquery.Client
bigqueryInserter *bigquery.Inserter
slackClient *slack.Client
sentimentClient *SlackSentiment
config *Config
}
func NewSlackEventStorage(
bigqueryProjectID string,
bigqueryDatasetID string,
bigqueryTableID string,
slackClient *slack.Client,
config *Config) (*SlackEventStorage, error) {
ctx := context.Background()
bigqueryClient, err := bigquery.NewClient(ctx, bigqueryProjectID)
if err != nil {
return nil, err
}
//defer bigqueryClient.Close()
bigqueryInserter := bigqueryClient.Dataset(bigqueryDatasetID).Table(bigqueryTableID).Inserter()
sentimentClient, err := NewSlacSentiment()
if err != nil {
return nil, err
}
return &SlackEventStorage{
BigqueryClient: bigqueryClient,
bigqueryInserter: bigqueryInserter,
slackClient: slackClient,
sentimentClient: sentimentClient,
config: config,
}, nil
}
func (s SlackEventStorage) MessageEvent(ev *slackevents.MessageEvent) error {
// Ignore message_changed events, for some reason they are sent when you post
// to a thread and tick the "send to channel" checkbox
if ev.SubType == "message_changed" {
return nil
}
ctx := context.Background()
user, err := s.slackClient.GetUserInfoContext(ctx, ev.User)
if err != nil {
return err
}
channel, err := s.slackClient.GetConversationInfoContext(ctx, ev.Channel, true)
if err != nil {
return err
}
log.WithFields(log.Fields{
"channel": channel.Name,
"sender": user.Name,
}).Debug("MessageEvent recieved")
if err := s.bigqueryInserter.Put(ctx, SlackMessageEvent{
Event: ev.Type,
User: user.Name,
Channel: channel.Name,
// ChanelIsOpen: channel.IsOpen,
// MessageInTread: ev.ThreadTimeStamp != ""
// The TimeStamp value is essentially the ID of the message, guaranteed
// unique within the context of a channel or conversation. They look like
// UNIX/epoch timestamps, hence TimeStamp, with specified milliseconds.
// They'll even sort like the same. But they're message IDs, even if they're
// partially composed in seconds-since-the-epoch. The TimeStamp of a
// message can be used in many operations such as replying to it in a
// thread, or modifying the message. But it can also be used to retrieve the
// message by itself.
MessageID: ev.TimeStamp,
ParentID: ev.ThreadTimeStamp,
DateTime: civil.DateTimeOf(time.Now()),
}); err != nil {
return err
}
return nil
}
func (s SlackEventStorage) ReactionAddedEvent(ev *slackevents.ReactionAddedEvent) error {
ctx := context.Background()
sender, err := s.slackClient.GetUserInfoContext(ctx, ev.User)
if err != nil {
return err
}
reciever, err := s.slackClient.GetUserInfoContext(ctx, ev.ItemUser)
if err != nil {
return err
}
channel, err := s.slackClient.GetConversationInfoContext(ctx, ev.Item.Channel, true)
if err != nil {
return err
}
// Experimental detects sentiment of reaction text
if s.config.EnableSentiment {
sentiment, err := s.sentimentClient.analyze(ev.Reaction)
if err != nil {
log.WithFields(log.Fields{
"text": ev.Reaction,
"error": err.Error(),
}).Error("sentimentClient.analyze failed")
}
log.WithFields(log.Fields{
"text": ev.Reaction,
"score": sentiment.DocumentSentiment.Score,
}).Debug("sentimentClient.analyze")
//if sentiment.DocumentSentiment.Score >= 0 {
// fmt.Println("Sentiment: positive")
//} else {
// fmt.Println("Sentiment: negative")
//}
}
log.WithFields(log.Fields{
"reaction": ev.Reaction,
"sender": sender.Name,
"reciever": reciever.Name,
}).Debug("reaction recieved")
// Add event for reaction given
if err := s.bigqueryInserter.Put(ctx, SlackMessageEvent{
DateTime: civil.DateTimeOf(time.Now()),
Event: ev.Type,
User: sender.Name,
Channel: channel.Name,
MessageID: ev.Item.Timestamp,
Reaction: ev.Reaction,
}); err != nil {
return err
}
// Add event for reaction recieved
if err := s.bigqueryInserter.Put(ctx, SlackMessageEvent{
DateTime: civil.DateTimeOf(time.Now()),
Event: "reaction_recieved",
User: reciever.Name,
Channel: channel.Name,
MessageID: ev.Item.Timestamp,
Reaction: ev.Reaction,
}); err != nil {
return err
}
return nil
}