@@ -15,22 +15,84 @@ This repository contains a module for the [Joe Bot library][joe].
15
15
16
16
## Getting Started
17
17
18
- Joe is packaged using the new [ Go modules] [ go-modules ] . Therefore the recommended
19
- installation is by adding joe and all used modules to your ` go.mod ` file like this:
18
+ This library is packaged using the new [ Go modules] [ go-modules ] . You can get it via:
20
19
21
20
```
22
- module github.com/go-joe/example-bot
23
-
24
- require (
25
- github.com/go-joe/joe v0.4.0
26
- github.com/go-joe/redis-memory v0.2.0
27
- …
28
- )
21
+ go get github.com/go-joe/redis-memory
29
22
```
30
23
31
24
### Example usage
32
25
33
- TODO
26
+ In order to connect your bot to Redis you can simply pass it as module when
27
+ creating a new bot:
28
+
29
+ [ embedmd ] :# ( _examples/main.go )
30
+ ``` go
31
+ package main
32
+
33
+ import (
34
+ " github.com/go-joe/joe"
35
+ " github.com/go-joe/redis-memory"
36
+ " github.com/pkg/errors"
37
+ )
38
+
39
+ type ExampleBot struct {
40
+ *joe.Bot
41
+ }
42
+
43
+ func main () {
44
+ b := &ExampleBot{
45
+ Bot: joe.New (" example" ,
46
+ redis.Memory (" localhost:6379" ),
47
+ ),
48
+ }
49
+
50
+ b.Respond (" remember (.+) is (.+)" , b.Remember )
51
+ b.Respond (" what is (.+)" , b.WhatIs )
52
+ b.Respond (" show keys" , b.ShowKeys )
53
+
54
+ err := b.Run ()
55
+ if err != nil {
56
+ b.Logger .Fatal (err.Error ())
57
+ }
58
+ }
59
+
60
+ func (b *ExampleBot ) Remember (msg joe .Message ) error {
61
+ key , value := msg.Matches [0 ], msg.Matches [1 ]
62
+ msg.Respond (" OK, I'll remember %s is %s " , key, value)
63
+ return b.Store .Set (key, value)
64
+ }
65
+
66
+ func (b *ExampleBot ) WhatIs (msg joe .Message ) error {
67
+ key := msg.Matches [0 ]
68
+ var value string
69
+ ok , err := b.Store .Get (key, &value)
70
+ if err != nil {
71
+ return errors.Wrapf (err, " failed to retrieve key %q from brain" , key)
72
+ }
73
+
74
+ if ok {
75
+ msg.Respond (" %s is %s " , key, value)
76
+ } else {
77
+ msg.Respond (" I do not remember %q " , key)
78
+ }
79
+
80
+ return nil
81
+ }
82
+
83
+ func (b *ExampleBot ) ShowKeys (msg joe .Message ) error {
84
+ keys , err := b.Store .Keys ()
85
+ if err != nil {
86
+ return err
87
+ }
88
+
89
+ msg.Respond (" I got %d keys:" , len (keys))
90
+ for i , k := range keys {
91
+ msg.Respond (" %d ) %q " , i+1 , k)
92
+ }
93
+ return nil
94
+ }
95
+ ```
34
96
35
97
## Built With
36
98
@@ -57,6 +119,10 @@ See also the list of [contributors][contributors] who participated in this proje
57
119
58
120
This project is licensed under the BSD-3-Clause License - see the [ LICENSE] ( LICENSE ) file for details.
59
121
122
+ ## Acknowledgments
123
+
124
+ - [ embedmd] [ embedmd ] for a cool tool to embed source code in markdown files
125
+
60
126
[ joe ] : https://github.com/go-joe/joe
61
127
[ go-modules ] : https://github.com/golang/go/wiki/Modules
62
128
[ tags ] : https://github.com/go-joe/redis-memory/tags
0 commit comments