-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhello.go
84 lines (72 loc) · 2.4 KB
/
hello.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
package main
import (
"fmt"
"log"
"github.com/kyleterry/quasar"
)
const HelpText = `Use this to convey what the service does during a help query`
const Description = "Says hello and ping back to a user"
func matchHello(msg quasar.Message) quasar.Result {
res := make(quasar.Result)
if msg.Payload != "hello" {
return nil
}
return res
}
var matchName = quasar.NewRegexMatcher("^my name is (?P<name>(.*))$")
func main() {
// Start by creating a configuration object to pass into the service when it's created.
config := &quasar.Config{
Name: "hello",
Version: "1.0",
Service: quasar.ServiceConfig{
SendAddr: "tcp://localhost:61124",
RecvAddr: "tcp://localhost:61123",
},
}
service := quasar.New(config)
service.HelpText = HelpText
service.Description = Description
// Handle tables a MsgHandler that will try to match incoming messages against anything you define.
service.Handle(
quasar.MsgHandler{
MatcherFunc: quasar.MatcherFunc(matchHello),
DirectOnly: true,
MatchHandler: quasar.HandlerFunc(func(match quasar.Result, msg quasar.Message, com quasar.Communication) {
log.Print("Hello handler called")
com.Send(fmt.Sprintf("Hello, %s!", msg.Nick), msg)
}),
// This is a handler level help text. You can use it to document a handler for !help queries.
HelpText: "hello - tenyks will respond with a hello back",
},
)
service.Handle(
quasar.MsgHandler{
MatcherFunc: matchName,
DirectOnly: true,
MatchHandler: quasar.HandlerFunc(func(match quasar.Result, msg quasar.Message, com quasar.Communication) {
log.Print("Name handler called")
if name, ok := match["name"]; ok {
com.Send(fmt.Sprintf("nice to meet you, %s!", name), msg)
}
}),
HelpText: "my name is <name> - tenyks will respond with a personal greeting back",
},
)
// You can set a default handler. This is handler is called if nothing matches an incoming message.
service.DefaultHandle(
quasar.MsgHandler{
DirectOnly: true,
MatchHandler: quasar.PrivmsgMiddlware(quasar.HandlerFunc(func(match quasar.Result, msg quasar.Message, com quasar.Communication) {
fmt.Println(msg)
log.Print("Default handler called")
com.Send(fmt.Sprintf("you rang, %s?", msg.Nick), msg)
})),
HelpText: "if nothing matches, tenyks will respond to any message asking if you rang",
},
)
log.Print("Starting hello service")
if err := service.Run(); err != nil {
log.Fatal(err)
}
}