-
Notifications
You must be signed in to change notification settings - Fork 2
/
temp.go
380 lines (339 loc) · 9.89 KB
/
temp.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
369
370
371
372
373
374
375
376
377
378
379
380
package main
import "fmt"
import "log"
import "os"
import "errors"
import "strings"
import "github.com/nlopes/slack"
import "github.com/tadgh/go-toggl"
import "encoding/gob"
import "time"
import "runtime"
const file = "./test.gob"
func Check(err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Println(line, "\t", file, "\n", err)
os.Exit(1)
}
}
func pingTogglApi(apiKey string) error {
ts := toggl.OpenSession(apiKey)
_, err := ts.GetAccount()
if err != nil {
fmt.Println(os.Stderr, "Error: %s\n", err)
return err
}
return nil
}
func Save(path string, object interface{}) error {
file, err := os.Create(path)
if err == nil {
encoder := gob.NewEncoder(file)
encoder.Encode(object)
}
file.Close()
return err
}
func Load(path string, object interface{}) error {
file, err := os.Open(path)
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(object)
}
file.Close()
return err
}
func createTimeEntry(apiKey, description string, start time.Time, duration time.Duration, pid, tid int) *toggl.TimeEntry {
ts := toggl.OpenSession(apiKey)
te, err := ts.CreateTimeEntry(pid, 0, start, duration, description)
if err != nil {
log.Fatal("Error uploading time entry! %v", err)
}
return &te
}
func stopTimer(apiKey string) (*toggl.TimeEntry, error) {
ts := toggl.OpenSession(apiKey)
te, err := ts.GetActiveTimeEntry()
if err != nil {
return nil, err
}
if te.ID <= 0 {
return nil, errors.New("No timer is currently running!")
}
te, err = ts.StopTimeEntry(te)
if err != nil {
return nil, err
}
return &te, nil
}
func startTimer(apiKey, description string, pid int) *toggl.TimeEntry {
ts := toggl.OpenSession(apiKey)
te, err := ts.StartTimeEntryForProject(description, pid)
if err != nil {
fmt.Println(err)
}
sessionMap[apiKey] = te
return &te
}
func getProjectWithName(apiKey, projectName string) int {
ts := toggl.OpenSession(apiKey)
acc, err := ts.GetAccount()
var retVal int
if err != nil {
fmt.Println(err)
}
for _, project := range acc.Data.Projects {
//case insensitive string comparison
if strings.EqualFold(project.Name, projectName) {
retVal = project.ID
}
}
return retVal
}
type BotCommand struct {
Channel string
Event *slack.MessageEvent
UserId string
}
type ReplyChannel struct {
Channel string
Attachment *slack.Attachment
DisplayTitle string
}
var (
api *slack.Client
botCommandChannel chan *BotCommand
botReplyChannel chan ReplyChannel
botId string
userMap map[string]string
sessionMap map[string]toggl.TimeEntry
)
func handleBotCommands(replyChannel chan ReplyChannel) {
commands := map[string]string{
"register": "Register yourself with togglbot. `@togglbot register MY_TOGGL_API_KEY`",
"start": "Start a timer for a given project and description. `@togglbot start <PROJECT_NAME> <EVERYTHING_ELSE_IS_DESCRIPTION>`",
"stop": "Stops any current timer session. `@togglbot stop`",
"track": "adds a toggl entry with an optional description to a project for a given time range and date (optional). `@togglbot track icancope 9am-5pm` OR `@togglbot track icancope 9am-5pm 2017/08/11 description`",
}
for {
incomingCommand := <-botCommandChannel
commandArray := strings.Fields(strings.ToLower(incomingCommand.Event.Text))
if strings.EqualFold(commandArray[0], "<@"+botId+">") {
commandArray = commandArray[1:]
}
var reply ReplyChannel
reply.Channel = incomingCommand.Channel
switch commandArray[0] {
case "help":
reply.DisplayTitle = "Help!"
fields := make([]slack.AttachmentField, 0)
for k, v := range commands {
fields = append(fields, slack.AttachmentField{
Title: "<bot> " + k,
Value: v,
})
}
attachment := &slack.Attachment{
Pretext: "TogglBot Command List",
Color: "#B733FF",
Fields: fields,
MarkdownIn: []string{"fields"},
}
reply.Attachment = attachment
replyChannel <- reply
case "register":
togglApiKey := commandArray[1]
err := pingTogglApi(togglApiKey)
if err != nil {
reply.DisplayTitle = "Failed to register. Bad api key?"
} else {
userMap[incomingCommand.Event.User] = togglApiKey
err := Save(file, userMap)
Check(err)
reply.DisplayTitle = "Successfully registered!"
}
replyChannel <- reply
}
if reply.DisplayTitle != "" {
continue
}
togglApiKey, ok := userMap[incomingCommand.Event.User]
if !ok {
reply.DisplayTitle = "You have not registered with togglbot yet. Try @togglbot register API_KEY_HERE"
replyChannel <- reply
continue
}
switch commandArray[0] {
case "start":
if len(commandArray) <= 2 {
reply.DisplayTitle = "Please provide a project name and description! `@togglbot start PROJECT_NAME DESCRIPTION`"
replyChannel <- reply
break
}
project := commandArray[1]
description := strings.Join(commandArray[2:], " ")
pid := getProjectWithName(togglApiKey, project)
fmt.Printf("%v", pid)
startTimer(togglApiKey, description, pid)
reply.DisplayTitle = "Timer started! *get back to work peon*"
replyChannel <- reply
case "stop":
te, err := stopTimer(togglApiKey)
if err != nil {
reply.DisplayTitle = "couldn't stop timer: " + err.Error()
replyChannel <- reply
break
}
dur, err := time.ParseDuration(fmt.Sprintf("%vs", te.Duration))
if err != nil {
log.Fatal("Unparseable duration! %v", te.Duration)
}
reply.DisplayTitle = fmt.Sprintf("Timer Stopped. Worked for %v.", dur.String())
replyChannel <- reply
case "track":
if len(commandArray) < 3 {
reply.DisplayTitle = "Sorry, I don't have enough information to make an event for you. try `@togglbot track PROJECT_NAME 9:00AM-5:00PM YYYY/MM/DD TASK_DESCRIPTION`"
replyChannel <- reply
break
}
projectName := commandArray[1]
pid := getProjectWithName(togglApiKey, projectName)
timeRange := commandArray[2]
description := "no description provided"
entryDate := time.Now()
var dateError error;
if len(commandArray) > 3 {
entryDate, dateError = parseDate(commandArray[3])
if dateError != nil {
description = strings.Join(commandArray[3:], " ")
} else {
if len(commandArray) > 4 {
description = strings.Join(commandArray[4:], " ")
}
}
}
startTime, duration, err := parseTimeRange(timeRange, entryDate)
if err != nil {
reply.DisplayTitle = err.Error()
replyChannel <- reply
break
}
createTimeEntry(togglApiKey, description, *startTime, *duration, pid, 0)
reply.DisplayTitle = "Time entry created!"
replyChannel <- reply
default:
reply.DisplayTitle = "Sorry, I don't understand that command. Try `@Togglbot help`"
replyChannel <- reply
}
}
}
func parseDate(readingDate string) (time.Time, error) {
parsedDate, err := time.Parse("2006/1/2", readingDate)
if err != nil {
return time.Now(), err
}
/* No error is returned if malformed date because if parsedDate == nil it will
be assumed to be a description thus error will never be shown */
return parsedDate, nil
}
func parseTimeRange(timeRange string, parsedDate time.Time) (*time.Time, *time.Duration, error) {
timeRange = strings.ToUpper(timeRange)
fields := strings.Split(timeRange, "-")
if len(fields) != 2 {
return nil, nil, errors.New("Your date range is incorrectly formatted! Try something like: 9:00AM-5:00PM")
}
startTime, err := time.Parse(time.Kitchen, fields[0])
if err != nil {
return nil, nil, errors.New("Your date range is incorrectly formatted! Try something like: 9:00AM-5:00PM")
}
endTime, err := time.Parse(time.Kitchen, fields[1])
if err != nil {
return nil, nil, errors.New("Your date range is incorrectly formatted! Try something like: 9:00AM-5:00PM")
}
duration := endTime.Sub(startTime)
// Kitchen time has only hours and PM/AM. Drop in today's date.
fmt.Println("DATE:\t", parsedDate)
startTime = time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), startTime.Hour(), startTime.Minute(), startTime.Second(), startTime.Nanosecond(), parsedDate.Location())
fmt.Println("START TIME:\t", startTime)
return &startTime, &duration, nil
}
func handleBotReplies() {
for {
reply := <-botReplyChannel
params := slack.PostMessageParameters{}
params.AsUser = true
if reply.Attachment != nil {
params.Attachments = []slack.Attachment{*reply.Attachment}
}
_, _, err := api.PostMessage(reply.Channel, reply.DisplayTitle, params)
if err != nil {
fmt.Println("FATAL SHIT")
log.Fatal(err)
}
}
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("usage: togglbot slack-bot-token")
os.Exit(1)
}
//First attempt to deserialize a saved registration file, otherwise
//create new
err := Load(file, &userMap)
if err != nil {
fmt.Println("ERROR WAS DETECTED")
log.Fatal(err)
userMap = make(map[string]string)
}
sessionMap = make(map[string]toggl.TimeEntry)
token := os.Args[1]
api = slack.New(token)
rtm := api.NewRTM()
botCommandChannel = make(chan *BotCommand)
botReplyChannel = make(chan ReplyChannel)
go rtm.ManageConnection()
go handleBotCommands(botReplyChannel)
go handleBotReplies()
go fmt.Println("TogglBot ready, ^C exits")
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch event := msg.Data.(type) {
case *slack.ConnectedEvent:
botId = event.Info.User.ID
case *slack.MessageEvent:
botCommand := &BotCommand{
Channel: event.Channel,
Event: event,
UserId: event.User,
}
if isValidMessageEvent(event) {
fmt.Println("Received event: ", event)
botCommandChannel <- botCommand
}
case *slack.RTMError:
fmt.Printf("ERROR: %s\n", event.Error())
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
break Loop
}
}
}
}
func isValidMessageEvent(event *slack.MessageEvent) bool {
if event.Type != "message" {
return false
}
if event.User == botId {
return false
}
if strings.HasPrefix(event.Text, "<@"+botId+">") {
return true
}
if strings.HasPrefix(event.Channel, "D") {
return true
}
return false
}