-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
61 lines (44 loc) · 1.2 KB
/
bot.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
package main
import (
"strings"
"time"
"github.com/jspc-bots/bottom"
"github.com/lrstanley/girc"
"github.com/olekukonko/tablewriter"
)
type Bot struct {
bottom bottom.Bottom
google Google
}
func New(user, password, server string, verify bool, g Google) (b Bot, err error) {
b.google = g
b.bottom, err = bottom.New(user, password, server, verify)
if err != nil {
return
}
b.bottom.Client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
c.Cmd.Join(Chan)
})
router := bottom.NewRouter()
router.AddRoute(`(?i)show\s+my\s+diary`, b.diary)
b.bottom.Middlewares.Push(router)
return
}
func (b Bot) diary(_, channel string, _ []string) (err error) {
events, err := b.google.Today()
if err != nil {
return
}
sb := strings.Builder{}
table := tablewriter.NewWriter(&sb)
table.SetHeader([]string{"", "Event", "Location"})
for _, line := range events {
table.Append([]string{line.StartEnd(), line.Title, line.LocationString()})
}
table.Render()
b.bottom.Client.Cmd.Messagef(channel, "Diary for %s", time.Now().In(b.google.timezone).Format("2006-01-02"))
for _, line := range strings.Split(sb.String(), "\n") {
b.bottom.Client.Cmd.Message(channel, line)
}
return
}