-
Notifications
You must be signed in to change notification settings - Fork 3
/
chat.go
136 lines (118 loc) · 2.8 KB
/
chat.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package bot
import (
"regexp"
"strings"
)
// Chat is the interface for chat integrations
type Chat interface {
Plugin
Username() string
Messages() <-chan Message
Send(Message) error
Reply(Message) error
Topic(Message) error
Direct(Message) error
}
type messageType int
type hook func(r Responder) error
// Matcher determines whether the bot is triggered
type Matcher func(r *Responder) bool
const (
// DefaultMessage assigns our message to robot.Hear()
DefaultMessage messageType = iota
// Response assigns our message to robot.Respond()
Response
// Enter assigns our message to robot.Enter()
Enter
// Leave assigns our message to robot.Leave()
Leave
// Topic assigns our message to robot.Topic()
Topic
)
// Message is our message wrapper
type Message struct {
Type messageType
User string
Room string
Text string
Topic string
// Envelope is the original message that the bot is reacting to
Envelope interface{}
// Params is for adapter-specific parameters
Params interface{}
}
// Responder is the object one receives when listening for an event
type Responder struct {
*Robot
Message
// Only relevant for Regexp
Match []string
}
func newResponder(r *Robot, m Message) *Responder {
return &Responder{
Robot: r,
Message: m,
Match: []string{m.Text},
}
}
// Send sends a message. It defaults to sending in the current room
func (r *Responder) Send(m Message) error {
m.Envelope = r.Envelope
if m.Room == "" {
m.Room = r.Room
}
return r.Chat.Send(m)
}
// Reply responds to a message
func (r *Responder) Reply(text string) error {
return r.Chat.Reply(Message{
Text: text,
Room: r.Room,
Envelope: r.Envelope,
})
}
// Direct responds via direct message to the user
func (r *Responder) Direct(text string) error {
return r.Chat.Direct(Message{
Text: text,
Room: r.Room,
Envelope: r.Envelope,
})
}
// Topic changes the topic
func (r *Responder) Topic(topic string) error {
return r.Chat.Topic(Message{
Room: r.Room,
Topic: topic,
Envelope: r.Envelope,
})
}
// User is a Matcher function that matches the User exactly
func User(u string) Matcher {
return func(r *Responder) bool {
return r.Message.User == u
}
}
// Room is a Matcher function that matches the Room exactly
func Room(c string) Matcher {
return func(r *Responder) bool {
return r.Message.Room == c
}
}
// Contains matches a subset of the message text
func Contains(t string) Matcher {
return func(r *Responder) bool {
return strings.Contains(r.Message.Text, t)
}
}
// Regexp matches the message text with a regular expression
func Regexp(expression string) Matcher {
return func(r *Responder) bool {
reg, err := regexp.Compile(expression)
if err == nil && reg.MatchString(r.Message.Text) {
r.Match = reg.FindStringSubmatch(r.Message.Text)
return true
}
return false
}
}