forked from sbstjn/hanu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversation.go
72 lines (58 loc) · 1.7 KB
/
conversation.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
package hanu
import (
"github.com/ChrisMcKee/allot"
)
// Convo is a shorthand for ConversationInterface
type Convo ConversationInterface
// ConversationInterface is the interface for a conversation
type ConversationInterface interface {
Integer(name string) (int, error)
String(name string) (string, error)
Reply(text string, a ...interface{})
Match(position int) (string, error)
Message() MessageInterface
}
// Sayer is an object that can talk in the channel
type Sayer interface {
Say(string, string, ...interface{})
}
// Conversation stores message, command and socket information and is passed
// to the handler function
type Conversation struct {
message Message
match allot.MatchInterface
bot Sayer
}
// Message returns the convos message
func (c *Conversation) Message() MessageInterface {
return c.message
}
// Reply sends message using the socket to Slack
func (c *Conversation) Reply(text string, a ...interface{}) {
prefix := ""
if !c.message.IsDirectMessage() {
prefix = "<@" + c.message.User() + ">: "
}
c.bot.Say(c.Message().Channel(), prefix+text, a...)
}
// String return string parameter
func (c Conversation) String(name string) (string, error) {
return c.match.String(name)
}
// Integer returns integer parameter
func (c Conversation) Integer(name string) (int, error) {
return c.match.Integer(name)
}
// Match returns the parameter at the position
func (c Conversation) Match(position int) (string, error) {
return c.match.Match(position)
}
// NewConversation returns a Conversation struct
func NewConversation(match allot.MatchInterface, msg Message, bot Sayer) ConversationInterface {
conv := &Conversation{
message: msg,
match: match,
bot: bot,
}
return conv
}