-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
64 lines (53 loc) · 1.18 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
package main
import (
"github.com/go-joe/joe"
"github.com/go-joe/redis-memory"
"github.com/pkg/errors"
)
type ExampleBot struct {
*joe.Bot
}
func main() {
b := &ExampleBot{
Bot: joe.New("example",
redis.Memory("localhost:6379"),
),
}
b.Respond("remember (.+) is (.+)", b.Remember)
b.Respond("what is (.+)", b.WhatIs)
b.Respond("show keys", b.ShowKeys)
err := b.Run()
if err != nil {
b.Logger.Fatal(err.Error())
}
}
func (b *ExampleBot) Remember(msg joe.Message) error {
key, value := msg.Matches[0], msg.Matches[1]
msg.Respond("OK, I'll remember %s is %s", key, value)
return b.Store.Set(key, value)
}
func (b *ExampleBot) WhatIs(msg joe.Message) error {
key := msg.Matches[0]
var value string
ok, err := b.Store.Get(key, &value)
if err != nil {
return errors.Wrapf(err, "failed to retrieve key %q from brain", key)
}
if ok {
msg.Respond("%s is %s", key, value)
} else {
msg.Respond("I do not remember %q", key)
}
return nil
}
func (b *ExampleBot) ShowKeys(msg joe.Message) error {
keys, err := b.Store.Keys()
if err != nil {
return err
}
msg.Respond("I got %d keys:", len(keys))
for i, k := range keys {
msg.Respond("%d) %q", i+1, k)
}
return nil
}