Skip to content

Commit 87a6482

Browse files
committed
Add example usage to README.md
1 parent e774fd7 commit 87a6482

File tree

2 files changed

+140
-10
lines changed

2 files changed

+140
-10
lines changed

README.md

+76-10
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,84 @@ This repository contains a module for the [Joe Bot library][joe].
1515

1616
## Getting Started
1717

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:
2019

2120
```
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
2922
```
3023

3124
### Example usage
3225

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+
```
3496

3597
## Built With
3698

@@ -57,6 +119,10 @@ See also the list of [contributors][contributors] who participated in this proje
57119

58120
This project is licensed under the BSD-3-Clause License - see the [LICENSE](LICENSE) file for details.
59121

122+
## Acknowledgments
123+
124+
- [embedmd][embedmd] for a cool tool to embed source code in markdown files
125+
60126
[joe]: https://github.com/go-joe/joe
61127
[go-modules]: https://github.com/golang/go/wiki/Modules
62128
[tags]: https://github.com/go-joe/redis-memory/tags

_examples/main.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"github.com/go-joe/joe"
5+
"github.com/go-joe/redis-memory"
6+
"github.com/pkg/errors"
7+
)
8+
9+
type ExampleBot struct {
10+
*joe.Bot
11+
}
12+
13+
func main() {
14+
b := &ExampleBot{
15+
Bot: joe.New("example",
16+
redis.Memory("localhost:6379"),
17+
),
18+
}
19+
20+
b.Respond("remember (.+) is (.+)", b.Remember)
21+
b.Respond("what is (.+)", b.WhatIs)
22+
b.Respond("show keys", b.ShowKeys)
23+
24+
err := b.Run()
25+
if err != nil {
26+
b.Logger.Fatal(err.Error())
27+
}
28+
}
29+
30+
func (b *ExampleBot) Remember(msg joe.Message) error {
31+
key, value := msg.Matches[0], msg.Matches[1]
32+
msg.Respond("OK, I'll remember %s is %s", key, value)
33+
return b.Store.Set(key, value)
34+
}
35+
36+
func (b *ExampleBot) WhatIs(msg joe.Message) error {
37+
key := msg.Matches[0]
38+
var value string
39+
ok, err := b.Store.Get(key, &value)
40+
if err != nil {
41+
return errors.Wrapf(err, "failed to retrieve key %q from brain", key)
42+
}
43+
44+
if ok {
45+
msg.Respond("%s is %s", key, value)
46+
} else {
47+
msg.Respond("I do not remember %q", key)
48+
}
49+
50+
return nil
51+
}
52+
53+
func (b *ExampleBot) ShowKeys(msg joe.Message) error {
54+
keys, err := b.Store.Keys()
55+
if err != nil {
56+
return err
57+
}
58+
59+
msg.Respond("I got %d keys:", len(keys))
60+
for i, k := range keys {
61+
msg.Respond("%d) %q", i+1, k)
62+
}
63+
return nil
64+
}

0 commit comments

Comments
 (0)