forked from shomali11/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
64 lines (52 loc) · 1.51 KB
/
response.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
package slacker
import (
"fmt"
"github.com/nlopes/slack"
)
const (
errorFormat = "*Error:* _%s_"
)
// A ResponseWriter interface is used to respond to an event
type ResponseWriter interface {
Reply(text string, options ...ReplyOption)
ReportError(err error)
Typing()
RTM() *slack.RTM
Client() *slack.Client
}
// NewResponse creates a new response structure
func NewResponse(channel string, client *slack.Client, rtm *slack.RTM) ResponseWriter {
return &response{channel: channel, client: client, rtm: rtm}
}
type response struct {
channel string
client *slack.Client
rtm *slack.RTM
}
// ReportError sends back a formatted error message to the channel where we received the event from
func (r *response) ReportError(err error) {
r.rtm.SendMessage(r.rtm.NewOutgoingMessage(fmt.Sprintf(errorFormat, err.Error()), r.channel))
}
// Typing send a typing indicator
func (r *response) Typing() {
r.rtm.SendMessage(r.rtm.NewTypingMessage(r.channel))
}
// Reply send a attachments to the current channel with a message
func (r *response) Reply(message string, options ...ReplyOption) {
defaults := newReplyDefaults(options...)
r.rtm.PostMessage(
r.channel,
slack.MsgOptionText(message, false),
slack.MsgOptionUser(r.rtm.GetInfo().User.ID),
slack.MsgOptionAsUser(true),
slack.MsgOptionAttachments(defaults.Attachments...),
)
}
// RTM returns the RTM client
func (r *response) RTM() *slack.RTM {
return r.rtm
}
// Client returns the slack client
func (r *response) Client() *slack.Client {
return r.client
}