-
Notifications
You must be signed in to change notification settings - Fork 14
/
response_writer.go
96 lines (80 loc) · 2.64 KB
/
response_writer.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
95
96
package slacker
import (
"context"
"fmt"
"github.com/slack-go/slack"
)
// newWriter creates a new poster structure
func newWriter(ctx context.Context, logger Logger, slackClient *slack.Client) *Writer {
return &Writer{ctx: ctx, logger: logger, slackClient: slackClient}
}
// Writer sends messages to Slack
type Writer struct {
ctx context.Context
logger Logger
slackClient *slack.Client
}
// Post send a message to a channel
func (r *Writer) Post(channel string, message string, options ...PostOption) (string, error) {
return r.post(channel, message, []slack.Block{}, options...)
}
// PostError send an error to a channel
func (r *Writer) PostError(channel string, err error, options ...PostOption) (string, error) {
attachments := []slack.Attachment{}
attachments = append(attachments, slack.Attachment{
Color: "danger",
Text: err.Error(),
})
return r.post(channel, "", []slack.Block{}, append(options, SetAttachments(attachments))...)
}
// PostBlocks send blocks to a channel
func (r *Writer) PostBlocks(channel string, blocks []slack.Block, options ...PostOption) (string, error) {
return r.post(channel, "", blocks, options...)
}
// Delete deletes message
func (r *Writer) Delete(channel string, messageTimestamp string) (string, error) {
_, timestamp, err := r.slackClient.DeleteMessage(
channel,
messageTimestamp,
)
if err != nil {
r.logger.Error("failed to delete message", "error", err)
}
return timestamp, err
}
func (r *Writer) post(channel string, message string, blocks []slack.Block, options ...PostOption) (string, error) {
postOptions := newPostOptions(options...)
opts := []slack.MsgOption{
slack.MsgOptionText(message, false),
slack.MsgOptionAttachments(postOptions.Attachments...),
slack.MsgOptionBlocks(blocks...),
}
if len(postOptions.ThreadTS) > 0 {
opts = append(opts, slack.MsgOptionTS(postOptions.ThreadTS))
}
if len(postOptions.ReplaceMessageTS) > 0 {
opts = append(opts, slack.MsgOptionUpdate(postOptions.ReplaceMessageTS))
}
if len(postOptions.EphemeralUserID) > 0 {
opts = append(opts, slack.MsgOptionPostEphemeral(postOptions.EphemeralUserID))
}
if postOptions.ScheduleTime != nil {
postAt := fmt.Sprintf("%d", postOptions.ScheduleTime.Unix())
opts = append(opts, slack.MsgOptionSchedule(postAt))
}
// response_writer.go
if postOptions.UnfurlLinks != nil {
if !*postOptions.UnfurlLinks {
opts = append(opts, slack.MsgOptionDisableLinkUnfurl(), slack.MsgOptionDisableMediaUnfurl())
}
}
_, timestamp, err := r.slackClient.PostMessageContext(
r.ctx,
channel,
opts...,
)
if err != nil {
r.logger.Error("failed to post message", "error", err)
}
return timestamp, err
}