/
main.go
368 lines (292 loc) · 8.43 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
arrow "github.com/bmuller/arrow/lib"
gorelic "github.com/brandfolder/gin-gorelic"
"github.com/carlescere/scheduler"
"github.com/gin-gonic/contrib/ginrus"
"github.com/gin-gonic/gin"
"github.com/nlopes/slack"
"gopkg.in/mgo.v2/bson"
"github.com/dwarvesf/working-on/db"
)
type Item struct {
ID bson.ObjectId `json:"id" bson:"_id"`
UserID string `json:"user_id" bson:"user_id"`
Name string `json:"user_name" bson:"user_name"`
Text string `json:"text" bson:"text"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
}
func main() {
// Read configuration from file and env
port := os.Getenv("PORT")
digestTime := os.Getenv("DIGEST_TIME")
dailyScrumTime := os.Getenv("DAILYSCRUM_TIME")
gorelic.InitNewrelicAgent(os.Getenv("NEW_RELIC_LICENSE_KEY"), "working", false)
digestConfig, err := parseConfig("digest.json")
if err != nil {
log.Fatalln(err)
}
// Setup schedule digest jobs
for _, i := range digestConfig.Items {
digestJob := postDigest(i.Channel, os.Getenv(i.Token), i.Tags)
_, err := scheduler.Every().Day().At(digestTime).Run(digestJob)
if err != nil {
log.Infoln(err)
}
}
// Setup schedule for daily scrum reminer
scheduler.Every().Day().At(dailyScrumTime).Run(remindDailyScrum)
settingConfig, err := parseConfig("setting.json")
if err != nil {
log.Fatalln(err)
}
// Prepare router
router := gin.New()
router.Use(gorelic.Handler)
router.Use(ginrus.Ginrus(log.StandardLogger(), time.RFC3339, true))
// router.LoadHTMLGlob("templates/*.tmpl.html")
router.Static("/static", "static")
router.POST("/on", on(*settingConfig))
router.POST("/til", til(*settingConfig))
router.POST("/done", done(*settingConfig))
// Start server
router.Run(":" + port)
}
func done(config Configuration) func(c *gin.Context) {
return func(c *gin.Context) {
text := c.PostForm("text")
text = strings.TrimSpace(text)
if text == "" {
log.Fatalln("Message is nil")
return
}
userID := c.PostForm("user_id")
userName := c.PostForm("user_name")
addItem(text, userID, userName, config, ":cantboiroi: *%s* has *done*: %s")
}
}
func til(config Configuration) func(c *gin.Context) {
return func(c *gin.Context) {
text := c.PostForm("text")
text = strings.TrimSpace(text)
if text == "" {
log.Fatalln("Message is nil")
return
}
userID := c.PostForm("user_id")
userName := c.PostForm("user_name")
addItem(text, userID, userName, config, "*%s* #til - Today I learned: %s :adore:")
}
}
func on(config Configuration) func(c *gin.Context) {
return func(c *gin.Context) {
text := c.PostForm("text")
text = strings.TrimSpace(text)
if text == "" {
log.Fatalln("Message is nil")
return
}
userID := c.PostForm("user_id")
userName := c.PostForm("user_name")
addItem(text, userID, userName, config, "*%s* is *working* on: %s")
}
}
// Message will be passed to server with '-' prefix via various way
// + Direct message with the bots
// + Use slash command `/working <message>`
// + Use cli `working-on <message>`
// + Might use Chrome plugin
// + ...
// Token is secondary param to indicate the user
func addItem(text string, userID string, userName string, configuration Configuration, format string) {
// Parse token and message
var item Item
item.ID = bson.NewObjectId()
item.CreatedAt = time.Now()
item.Name = userName
item.UserID = userID
item.Text = text
ctx, err := db.NewContext()
if err != nil {
log.Fatal(err)
}
defer ctx.Close()
// Add Item to database
err = ctx.C("items").Insert(item)
if err != nil {
log.Fatalln(err)
}
// Repost to the target channel
channel := os.Getenv("WORKING_CHANNEL")
botToken := os.Getenv("BOT_TOKEN")
if botToken == "" {
log.Fatal("No token provided")
}
// <@U024BE7LH|bob>: format text to match Slack format
userName = fmt.Sprintf("<@%s|%s>", userID, userName)
title := fmt.Sprintf(format, userName, text)
postItem(botToken, channel, title)
// Post item to project group
for _, config := range configuration.Items {
for _, tag := range config.Tags {
if strings.Contains(text, tag) {
log.Infof("Hit %s", tag)
log.Infof("Token: %s", config.Token)
// Post to target channel
postItem(config.Token, config.Channel, title)
}
}
}
}
func postItem(token string, channel string, text string) {
s := slack.New(token)
params := slack.PostMessageParameters{}
params.IconURL = "http://i.imgur.com/fLcxkel.png"
params.Username = "oshin"
s.PostMessage(channel, text, params)
}
type Configuration struct {
Items []ConfigurationItem `json:"items"`
}
type ConfigurationItem struct {
Channel string `json:"channel"`
Tags []string `json:"tags"`
Token string `json:"token"`
}
func parseConfig(path string) (*Configuration, error) {
var configuration Configuration
// Parse configuration
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.New("Cannot read setting file")
}
err = json.Unmarshal(bytes, &configuration)
if err != nil {
return nil, errors.New("Cannot parse setting")
}
return &configuration, nil
}
// Remind daily scrum by posting message to Slack
func remindDailyScrum() {
today := arrow.Now().Weekday()
if today == time.Sunday || today == time.Saturday {
log.Infoln("It's weekend and people goes party!")
return
}
botToken := os.Getenv("BOT_TOKEN")
url := os.Getenv("DAILYSCRUM_URL")
if botToken == "" || url == "" {
log.Fatal("No token provided")
os.Exit(1)
}
s := slack.New(botToken)
params := slack.PostMessageParameters{}
params.IconURL = "http://i.imgur.com/fLcxkel.png"
params.Username = "oshin"
text := "Đến giờ daily scrum rồi mấy bé <!here|here> " + url + " :4head:"
channel := "#random"
s.PostMessage(channel, text, params)
}
// Post summary to Slack channel.
// Only post to specific channel when tags are met.
func postDigest(channel, botToken string, tags []string) func() {
return func() {
if botToken == "" {
log.Fatal("No token provided")
os.Exit(1)
}
s := slack.New(botToken)
users, err := s.GetUsers()
if err != nil {
log.Fatal("Cannot get users")
os.Exit(1)
}
ctx, err := db.NewContext()
if err != nil {
log.Fatal(err)
}
defer ctx.Close()
log.Info("Preparing data")
// If count > 0, it means there is data to show
count := 0
params := slack.PostMessageParameters{}
fields := []slack.AttachmentField{}
yesterday := arrow.Yesterday().UTC()
toDate := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.UTC)
title := fmt.Sprintf(" :rocket: >> Team daily digest for *%s* :rocket: <!channel>", arrow.Yesterday().CFormat("%Y-%m-%d"))
// Prepare attachment of done items
for _, user := range users {
if user.IsBot || user.Deleted {
continue
}
// log.Infof("Process user: %s - %s", user.Name, user.Id)
// Query done items from Database
var values []string
var items []Item
err = ctx.C("items").Find(
bson.M{
"$and": []bson.M{
bson.M{"user_name": user.Name},
bson.M{"created_at": bson.M{"$gt": toDate}},
},
}).All(&items)
if err != nil {
log.Fatal("Cannot query done items.")
os.Exit(1)
}
for _, item := range items {
log.Infof("User: %s, item: %s, tags: %+v", user.Name, item.Text, tags)
// delete items which text does not contain tags
if tags != nil {
// check if item contains any tags
var containTag bool
for _, tag := range tags {
if !strings.Contains(item.Text, tag) {
continue
}
containTag = true
break
}
// if item.Text doesn't contains any tags then don't
// add it to the digest message (values)
if !containTag {
continue
}
}
// construct text format
values = append(values, fmt.Sprintf("+ %s", item.Text))
}
// <@U024BE7LH|bob>
if len(values) > 0 {
count++
field := slack.AttachmentField{
// Title: fmt.Sprintf("<@%s|%s>", user.ID, user.Name),
Title: fmt.Sprintf("%s", user.Name),
Value: strings.Join(values, "\n"),
}
fields = append(fields, field)
}
}
params.Attachments = []slack.Attachment{
slack.Attachment{
Color: "#7CD197",
Fields: fields,
Footer: "Oshin Bot",
FooterIcon: "http://i.imgur.com/fLcxkel.png",
},
}
params.IconURL = "http://i.imgur.com/fLcxkel.png"
params.Username = "oshin"
if count > 0 {
s.PostMessage(channel, title, params)
}
}
}