forked from shomali11/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
71 lines (58 loc) · 1.69 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
65
66
67
68
69
70
71
package slacker
import (
"fmt"
"github.com/slack-go/slack"
)
const (
errorFormat = "*Error:* _%s_"
)
// A ResponseWriter interface is used to respond to an event
type ResponseWriter interface {
Reply(text string, options ...ReplyOption) error
ReportError(err error, options ...ReportErrorOption)
}
// NewResponse creates a new response structure
func NewResponse(botCtx BotContext) ResponseWriter {
return &response{botCtx: botCtx}
}
type response struct {
botCtx BotContext
}
// ReportError sends back a formatted error message to the channel where we received the event from
func (r *response) ReportError(err error, options ...ReportErrorOption) {
defaults := NewReportErrorDefaults(options...)
client := r.botCtx.Client()
ev := r.botCtx.Event()
opts := []slack.MsgOption{
slack.MsgOptionText(fmt.Sprintf(errorFormat, err.Error()), false),
}
if defaults.ThreadResponse {
opts = append(opts, slack.MsgOptionTS(ev.TimeStamp))
}
_, _, err = client.PostMessage(ev.Channel, opts...)
if err != nil {
fmt.Printf("failed posting message: %v\n", err)
}
}
// Reply send a attachments to the current channel with a message
func (r *response) Reply(message string, options ...ReplyOption) error {
defaults := NewReplyDefaults(options...)
client := r.botCtx.Client()
ev := r.botCtx.Event()
if ev == nil {
return fmt.Errorf("unable to get message event details")
}
opts := []slack.MsgOption{
slack.MsgOptionText(message, false),
slack.MsgOptionAttachments(defaults.Attachments...),
slack.MsgOptionBlocks(defaults.Blocks...),
}
if defaults.ThreadResponse {
opts = append(opts, slack.MsgOptionTS(ev.TimeStamp))
}
_, _, err := client.PostMessage(
ev.Channel,
opts...,
)
return err
}