-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
64 lines (53 loc) · 1.42 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
package notifications
import (
"bytes"
"fmt"
"github.com/JakubOboza/sifaka/config"
"github.com/JakubOboza/sifaka/storage"
"github.com/slack-go/slack"
)
type SlackService struct {
oauthToken string
channelID string
}
func NewSlackProvider() Provider {
oauthToken := config.SlackOAuth()
channelID := config.SlackChannelID()
return &SlackService{oauthToken: oauthToken, channelID: channelID}
}
func (svc *SlackService) Notify(certs []storage.CertificateData) error {
attachment := slack.Attachment{
Text: certWarningMessageText(certs),
}
return svc.sendMessage(attachment)
}
func (svc *SlackService) Ping() error {
attachment := slack.Attachment{
Text: "I'm alive!",
}
return svc.sendMessage(attachment)
}
func (svc *SlackService) sendMessage(attachment slack.Attachment) error {
api := slack.New(svc.oauthToken)
_, _, err := api.PostMessage(
svc.channelID,
slack.MsgOptionText(
"Sifaka spotted:", false),
slack.MsgOptionAttachments(attachment),
slack.MsgOptionAsUser(true),
)
return err
}
func certWarningMessageText(certs []storage.CertificateData) string {
buf := bytes.NewBufferString("Certificates that will expire soon:\n")
for _, cert := range certs {
buf.WriteString(fmt.Sprintf("%s that expires in %s\n", cert.Name, cert.ExpiresInString()))
}
return buf.String()
}
func slackProviderEnabled() bool {
if config.SlackOAuth() != "" && config.SlackChannelID() != "" {
return true
}
return false
}