-
Notifications
You must be signed in to change notification settings - Fork 0
/
historian.go
143 lines (134 loc) · 2.9 KB
/
historian.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
package slackopher
import (
elastic "gopkg.in/olivere/elastic.v5"
"github.com/nlopes/slack"
"context"
"strings"
"github.com/Sirupsen/logrus"
"log"
)
type Historian struct {
ElasticClient *elastic.Client
Ctx context.Context
BotId string
BotName string
Users *Users
Channels *Channels
}
func NewHistorian(elastic_url string) *Historian {
client, err := elastic.NewClient(elastic.SetURL(elastic_url))
if err != nil {
panic(err)
}
historian := &Historian{
ElasticClient: client,
Ctx: context.Background(),
}
historian.Setup()
return historian
}
func (h *Historian) Setup() error {
exists, err := h.ElasticClient.IndexExists("messages").Do(h.Ctx)
if err != nil {
panic(err)
}
if !exists {
h.CreateIndex()
}
h.Users = NewUsers()
h.Channels = NewChannels()
return nil
}
func (h *Historian) CreateIndex() {
mapping := `{
"mappings":{
"message":{
"properties":{
"user":{
"type":"string"
},
"channel":{
"type":"string"
},
"body": {
"type":"text"
},
"ts": {
"type":"date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
}
}`
_, err := h.ElasticClient.CreateIndex("messages").BodyString(mapping).Do(h.Ctx)
if err != nil {
panic(err)
}
}
func (h *Historian) JoinChannel(channel *slack.Channel) {
h.Channels.AddChannel(channel)
}
func (h *Historian) SendCommand(msg *slack.MessageEvent, channel chan *Command) {
logrus.Info("Send New Command")
channel <- &Command{
ChannelId: msg.Channel,
Event: msg,
UserId: msg.User,
}
}
func (h *Historian) SaveMessage(api *slack.Client, msg *slack.MessageEvent) {
logrus.Info("News Message")
u, err := h.Users.GetUser(msg.User)
if err != nil {
user, err := api.GetUserInfo(msg.User)
if err != nil {
log.Fatal("Could not retrieve user")
return
}
u = h.Users.AddUser(user)
}
channel, err := h.Channels.GetChannel(msg.Channel)
if err != nil {
log.Fatal("Could not retreive channel")
return
}
message := Message{
Body: msg.Text,
UserId: msg.User,
User: u.Info.Name,
Avatar: u.Info.Profile.Image48,
Channel: msg.Channel,
ChannelName: channel.Info.Name,
Timestamp: strings.Split(msg.Timestamp, ".")[0],
}
logrus.Info(message)
logrus.Info(message.Body)
_, err = h.ElasticClient.Index().
Index("messages").
Type("message").
BodyJson(message).
Do(h.Ctx)
if err != nil {
log.Fatal(err)
}
}
func (h *Historian) ProcessMessage(api *slack.Client, msg *slack.MessageEvent, channel chan *Command) {
if msg.Type != "message" {
return
}
if strings.HasPrefix(msg.Text, "<@" + h.BotId + ">") {
h.SendCommand(msg, channel)
} else {
if msg.SubType == "channel_join" && msg.User == h.BotId {
logrus.Info("Join new channel")
channelInfo, err := api.GetChannelInfo(msg.Channel)
if err != nil {
log.Fatal(err)
} else {
h.JoinChannel(channelInfo)
}
}
h.SaveMessage(api, msg)
}
}