-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathservice.go
71 lines (59 loc) · 1.7 KB
/
service.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 strillone
import (
"fmt"
"log"
"github.com/bluele/slack"
"github.com/dnsimple/dnsimple-go/dnsimple/webhook"
)
// MessagingService represents a service where the event is published.
// Some examples are Slack, HipChat, and Campfire.
type MessagingService interface {
FormatLink(name, url string) string
PostEvent(event *webhook.Event) (string, error)
}
// SlackService represents the Slack message service.
type SlackService struct {
Token string
}
// FormatLink implements MessagingService
func (s *SlackService) FormatLink(name, url string) string {
return fmt.Sprintf("<%s|%s>", url, name)
}
// FormatMessage implements MessagingService
func (s *SlackService) FormatMessage(message string) string {
return message
}
// PostEvent implements MessagingService
func (s *SlackService) PostEvent(event *webhook.Event) (string, error) {
eventID := eventRequestID(event)
text := Message(s, event)
// Send the webhook to Logs
log.Printf("[event:%v] %s", eventID, text)
// Don't send to Slack
if s.Token[0] == '-' {
return "", nil
}
slackWebhookURL := fmt.Sprintf("https://hooks.slack.com/services/%s", s.Token)
log.Printf("[event:%v] Sending event to slack %v\n", eventID, slackWebhookURL)
webhook := slack.NewWebHook(slackWebhookURL)
webhookErr := webhook.PostMessage(&slack.WebHookPostPayload{
Username: "DNSimple",
IconUrl: "http://cl.ly/2t0u2Q380N3y/trusty.png",
Attachments: []*slack.Attachment{
{
Fallback: text,
Color: "good",
Fields: []*slack.AttachmentField{
{
Title: event.Name,
Value: text,
},
},
},
},
})
if webhookErr != nil {
log.Printf("[event:%v] Error sending to slack: %v\n", eventID, webhookErr)
}
return text, webhookErr
}