-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
68 lines (55 loc) · 1.65 KB
/
example_test.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
package bot_test
import (
"fmt"
"net/http"
"github.com/botopolis/bot"
"github.com/botopolis/bot/mock"
)
type ExamplePlugin struct {
Username string
}
func (p *ExamplePlugin) Load(r *bot.Robot) { p.Username = "beardroid" }
func Example() {
// Ignore this - just example setup
chat := mock.NewChat()
chat.MessageChan = make(chan bot.Message)
go func() { close(chat.MessageChan) }()
// Install adapter and plugins
robot := bot.New(chat, &ExamplePlugin{})
robot.Logger = mock.NewLogger()
// Respond to any message
robot.Hear(bot.Regexp("welcome (\\w*)"), func(r bot.Responder) error {
return r.Send(bot.Message{
Text: "All hail " + r.Match[1] + ", our new overlord",
})
})
// Respond to messages that are either DMed to the bot or start with the bot's name
robot.Respond(bot.Regexp("topic (.*)"), func(r bot.Responder) error {
return r.Topic(r.Match[1])
})
// Track when topics are updated
robot.Topic(func(r bot.Responder) error {
if r.Room == "announcements" {
var announcements []string
r.Brain.Get("announcements", &announcements)
announcements = append(announcements, r.Match[1])
r.Brain.Set("announcements", &announcements)
}
return nil
})
// Track the comings and goings of people
robot.Enter(func(r bot.Responder) error { return nil })
robot.Leave(func(r bot.Responder) error { return nil })
// Create server endpoints
robot.Router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world"))
})
// Start the server and listening
robot.Run()
// Access plugins
var plugin ExamplePlugin
if ok := robot.Plugin(&plugin); ok {
fmt.Println(plugin.Username)
// Output: beardroid
}
}