-
Notifications
You must be signed in to change notification settings - Fork 14
/
response_replier.go
67 lines (54 loc) · 2.21 KB
/
response_replier.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
package slacker
import (
"github.com/slack-go/slack"
)
// newReplier creates a new replier structure
func newReplier(channelID string, userID string, inThread bool, eventTS string, writer *Writer) *Replier {
return &Replier{channelID: channelID, userID: userID, inThread: inThread, eventTS: eventTS, writer: writer}
}
// Replier sends messages to the same channel the event came from
type Replier struct {
channelID string
userID string
inThread bool
eventTS string
writer *Writer
}
// Reply send a message to the current channel
func (r *Replier) Reply(message string, options ...ReplyOption) (string, error) {
responseOptions := r.convertOptions(options...)
return r.writer.Post(r.channelID, message, responseOptions...)
}
// ReplyError send an error to the current channel
func (r *Replier) ReplyError(err error, options ...ReplyOption) (string, error) {
responseOptions := r.convertOptions(options...)
return r.writer.PostError(r.channelID, err, responseOptions...)
}
// ReplyBlocks send blocks to the current channel
func (r *Replier) ReplyBlocks(blocks []slack.Block, options ...ReplyOption) (string, error) {
responseOptions := r.convertOptions(options...)
return r.writer.PostBlocks(r.channelID, blocks, responseOptions...)
}
func (r *Replier) convertOptions(options ...ReplyOption) []PostOption {
replyOptions := newReplyOptions(options...)
responseOptions := []PostOption{
SetAttachments(replyOptions.Attachments),
}
// If the original message came from a thread, reply in a thread, unless there is an override
if (replyOptions.InThread == nil && r.inThread) || (replyOptions.InThread != nil && *replyOptions.InThread) {
responseOptions = append(responseOptions, SetThreadTS(r.eventTS))
}
if len(replyOptions.ReplaceMessageTS) > 0 {
responseOptions = append(responseOptions, SetReplace(replyOptions.ReplaceMessageTS))
}
if replyOptions.IsEphemeral {
responseOptions = append(responseOptions, SetEphemeral(r.userID))
}
if replyOptions.ScheduleTime != nil {
responseOptions = append(responseOptions, SetSchedule(*replyOptions.ScheduleTime))
}
if replyOptions.UnfurlLinks != nil {
responseOptions = append(responseOptions, SetUnfurlLinks(*replyOptions.UnfurlLinks))
}
return responseOptions
}