-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconversation.go
94 lines (72 loc) · 2.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package hanu
import (
"fmt"
"golang.org/x/net/websocket"
"github.com/sbstjn/allot"
)
// 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
SetConnection(connection Connection)
send(msg MessageInterface)
}
// Connection is the needed interface for a connection
type Connection interface {
Send(ws *websocket.Conn, v interface{}) (err error)
}
// Conversation stores message, command and socket information and is passed
// to the handler function
type Conversation struct {
message Message
match allot.MatchInterface
socket *websocket.Conn
connection Connection
}
func (c *Conversation) Message() MessageInterface {
return c.message
}
func (c *Conversation) send(msg MessageInterface) {
if c.socket != nil {
c.connection.Send(c.socket, msg)
}
}
// SetConnection sets the conversation connection
func (c *Conversation) SetConnection(connection Connection) {
c.connection = connection
}
// 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() + ">: "
}
msg := c.message
msg.SetText(prefix + fmt.Sprintf(text, a...))
c.send(msg)
}
// String return string paramter
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, socket *websocket.Conn) ConversationInterface {
conv := &Conversation{
message: msg,
match: match,
socket: socket,
}
conv.SetConnection(websocket.JSON)
return conv
}