-
Notifications
You must be signed in to change notification settings - Fork 0
/
infobot.go
96 lines (74 loc) · 1.85 KB
/
infobot.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
package infobot
import (
"context"
"github.com/jirwin/factpacks"
"github.com/jirwin/quadlek/quadlek"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
)
var factStore = factpacks.MakeFactStore()
const FactStoreKey = "facts"
func load(bot *quadlek.Bot, store *quadlek.Store) error {
return store.Get(FactStoreKey, func(rec []byte) error {
return factStore.Load(rec)
})
}
func infobot(ctx context.Context, hookChan <-chan *quadlek.HookMsg) {
for {
select {
case hookMsg := <-hookChan:
if !hookMsg.Bot.MsgToBot(hookMsg.Msg.Text) {
continue
}
line := strings.TrimPrefix(hookMsg.Msg.Text, fmt.Sprintf("<@%s> ", hookMsg.Bot.GetUserId()))
if lookup := factStore.LookupFact(line); lookup != "" {
hookMsg.Bot.Respond(hookMsg.Msg, lookup)
continue
}
if factStore.HumanFactSet(line) {
out, err := factStore.Serialize()
if err != nil {
log.WithField("err", err).Error("Error serializing factstore.")
continue
}
err = hookMsg.Store.Update(FactStoreKey, out)
if err != nil {
log.WithField("err", err).Error("Error while saving factstore.")
continue
}
hookMsg.Bot.Respond(hookMsg.Msg, "Alright. "+line)
continue
}
if factStore.HumanFactForget(line) {
out, err := factStore.Serialize()
if err != nil {
log.WithField("err", err).Error("Error serializing factstore.")
continue
}
err = hookMsg.Store.Update(FactStoreKey, out)
if err != nil {
log.WithField("err", err).Error("Error while saving factstore.")
continue
}
hookMsg.Bot.Respond(hookMsg.Msg, "Alright. I forgot it.")
continue
}
case <-ctx.Done():
log.Info("Shutting down infobot hook.")
return
}
}
}
func Register() quadlek.Plugin {
return quadlek.MakePlugin(
"infobot",
nil,
[]quadlek.Hook{
quadlek.MakeHook(infobot),
},
nil,
nil,
load,
)
}