Skip to content

Commit b874ffd

Browse files
authored
Merge pull request #31 from circa10a/slack-provider
Slack provider
2 parents 1be68a8 + acbe59b commit b874ffd

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Currently supported providers:
4444

4545
- [Discord](providers/discord/README.md)
4646
- [Rocket.Chat](providers/rocketchat/README.md)
47+
- [Slack](providers/slack/README.md)
4748
- [SMTP (email)](providers/smtp/README.md)
4849
- [Yammer](providers/yammer/README.md)
4950

config.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ providers:
22
discord:
33
enabled: false
44
webhookURL: "https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}"
5-
yammer:
6-
enabled: false
7-
APIURL: https://www.yammer.com/api/v1/messages.json
8-
groupID: 123456
9-
token: somesecrettext
105
rocketchat:
116
enabled: false
127
webhookURL: "https://<rocket-chat-server-here>/hooks/{token}"
13-
iconURL: "https://a0.awsstatic.com/libra-css/images/logos/aws_logo_smile_1200x630.png"
8+
iconURL: "https://cdn.iconscout.com/icon/free/png-256/aws-1869025-1583149.png"
9+
slack:
10+
enabled: false
11+
webhookURL: "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
12+
iconURL: "https://cdn.iconscout.com/icon/free/png-256/aws-1869025-1583149.png"
1413
smtp:
1514
enabled: false
1615
server: "smtp.gmail.com"
@@ -24,4 +23,9 @@ providers:
2423
- "some.email@mail.com"
2524
- "some.other@mail.com"
2625
# customTemplate: path/to/template.html
26+
yammer:
27+
enabled: false
28+
APIURL: https://www.yammer.com/api/v1/messages.json
29+
groupID: 123456
30+
token: somesecrettext
2731

providers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
_ "github.com/circa10a/go-aws-news/providers/discord"
55
_ "github.com/circa10a/go-aws-news/providers/rocketchat"
6+
_ "github.com/circa10a/go-aws-news/providers/slack"
67
_ "github.com/circa10a/go-aws-news/providers/smtp"
78
_ "github.com/circa10a/go-aws-news/providers/yammer"
89
)

providers/rocketchat/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ providers:
2727
iconURL: "{ override url to aws logo }"
2828
```
2929
30-
[webhook]:https://rocket.chat/docs/administrator-guides/integrations/
31-
3230
__Example post:__
3331
3432
![Rocketchat post example](https://i.imgur.com/4bKSTcf.png)
33+
34+
[webhook]:https://rocket.chat/docs/administrator-guides/integrations/

providers/slack/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Slack Setup
2+
3+
1. In Slack, click "Add more apps" on the left sidebar.
4+
1. In the search field, type "webhook".
5+
1. Select __Incoming WebHooks__.
6+
1. Click __Add to Slack__.
7+
1. Select the channel to post to.
8+
1. Click __Add Incoming WebHooks integration__.
9+
1. Copy the generated webhook URL and paste it in to the [config.yaml](/config.yaml) under
10+
`providers.slack.webhookURL`. The webhook format will look similar to this:
11+
12+
## Example Config
13+
14+
```yaml
15+
providers:
16+
slack:
17+
enabled: true
18+
iconURL: "https://cdn.iconscout.com/icon/free/png-256/aws-1869025-1583149.png"
19+
webhookURL: "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
20+
```
21+
22+
__Example post:__
23+
24+
![Slack post example](https://i.imgur.com/iw2SCJZ.png)
25+
26+
27+
[webhooks]:https://api.slack.com/messaging/webhooks

providers/slack/slack.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package slack
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
"strings"
9+
10+
"github.com/circa10a/go-aws-news/providers"
11+
12+
"github.com/circa10a/go-aws-news/news"
13+
log "github.com/sirupsen/logrus"
14+
"gopkg.in/yaml.v2"
15+
)
16+
17+
type config struct {
18+
Providers struct {
19+
Provider Provider `yaml:"slack"`
20+
} `yaml:"providers"`
21+
}
22+
23+
// Provider is an implementation of the `go-aws-news/providers` Provider interface.
24+
type Provider struct {
25+
IsEnabled bool `yaml:"enabled"`
26+
WebhookURL string `yaml:"webhookURL"`
27+
IconURL string `yaml:"iconURL"`
28+
}
29+
30+
// Content is sent as json to the Rocket.Chat webhook endpoint.
31+
type Content struct {
32+
Username string `json:"username"`
33+
IconURL string `json:"icon_url"`
34+
Text string `json:"text"`
35+
}
36+
37+
// init initializes the provider from the provided config.
38+
func init() {
39+
var c config
40+
if err := yaml.Unmarshal(providers.Config, &c); err != nil {
41+
log.Fatal(err)
42+
}
43+
44+
providers.RegisterProvider(&c.Providers.Provider)
45+
}
46+
47+
// Enabled returns true if the provider is enabled in the config.
48+
func (p *Provider) Enabled() bool {
49+
return p.IsEnabled
50+
}
51+
52+
// GetName returns the Provider's name.
53+
func (*Provider) GetName() string {
54+
return "slack"
55+
}
56+
57+
// Notify is the function executed to POST to a provider's webhook url.
58+
func (p *Provider) Notify(news news.Announcements) {
59+
60+
var b strings.Builder
61+
for _, v := range news {
62+
b.WriteString(fmt.Sprintf("<%s|%s> - %s\n", v.Link, v.Title, v.PostDate))
63+
}
64+
65+
content := &Content{
66+
Username: "AWS News",
67+
IconURL: p.IconURL,
68+
Text: b.String(),
69+
}
70+
71+
json, err := json.Marshal(content)
72+
if err != nil {
73+
log.Error(fmt.Sprintf("[%s] %v", p.GetName(), err))
74+
}
75+
76+
log.Info(fmt.Sprintf("[%v] Firing notification", p.GetName()))
77+
res, err := http.Post(p.WebhookURL, "application/json", bytes.NewBuffer(json))
78+
if err != nil {
79+
log.Error(fmt.Sprintf("[%s] %v", p.GetName(), err))
80+
}
81+
defer res.Body.Close()
82+
}

0 commit comments

Comments
 (0)