forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslack.go
68 lines (56 loc) · 1.87 KB
/
slack.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
package slack
import (
"context"
"github.com/pkg/errors"
"github.com/slack-go/slack"
)
//go:generate mockery --name=slackClient --output=. --case=underscore --inpackage
type slackClient interface {
PostMessageContext(ctx context.Context, channelID string, options ...slack.MsgOption) (string, string, error)
}
// Compile-time check to ensure that slack.Client implements the slackClient interface.
var _ slackClient = new(slack.Client)
// Slack struct holds necessary data to communicate with the Slack API.
type Slack struct {
client slackClient
channelIDs []string
}
// New returns a new instance of a Slack notification service.
// For more information about slack api token:
//
// -> https://pkg.go.dev/github.com/slack-go/slack#New
func New(apiToken string) *Slack {
client := slack.New(apiToken)
s := &Slack{
client: client,
channelIDs: []string{},
}
return s
}
// AddReceivers takes Slack channel IDs and adds them to the internal channel ID list. The Send method will send
// a given message to all those channels.
func (s *Slack) AddReceivers(channelIDs ...string) {
s.channelIDs = append(s.channelIDs, channelIDs...)
}
// Send takes a message subject and a message body and sends them to all previously set channels.
// you will need a slack app with the chat:write.public and chat:write permissions.
// see https://api.slack.com/
func (s Slack) Send(ctx context.Context, subject, message string) error {
fullMessage := subject + "\n" + message // Treating subject as message title
for _, channelID := range s.channelIDs {
select {
case <-ctx.Done():
return ctx.Err()
default:
id, timestamp, err := s.client.PostMessageContext(
ctx,
channelID,
slack.MsgOptionText(fullMessage, false),
)
if err != nil {
return errors.Wrapf(err, "failed to send message to Slack channel '%s' at time '%s'", id, timestamp)
}
}
}
return nil
}