From 2f827fc464932991fe2dc6575d57a81c129783ae Mon Sep 17 00:00:00 2001 From: Ryan Currah Date: Thu, 30 Apr 2020 20:22:14 -0400 Subject: [PATCH] allow choosing attachment color --- Dockerfile | 1 + README.md | 19 ++++++++++--------- main.go | 13 +++++++------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 649c68f..27c8e18 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ FROM alpine:latest as synology-notifications EXPOSE 8080 ENV API_KEY= ENV SLACK_WEBHOOK= +ENV SLACK_ATTACHMENT_COLOR= ENV LISTEN_PORT=8080 RUN apk --no-cache add ca-certificates COPY --from=builder synology-notifications/synology-notifications / diff --git a/README.md b/README.md index 2e1f22e..1fbc318 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,22 @@ # synology-notifications -Receive notifcations from Synology and forward them to the notifcation service of your choice +Receive notifications from Synology and forward them to the notification service of your choice -## Supported notifcation services +## Supported notification services - Slack ## Service settings -Settings are supplied by setting envrionment variables +Settings are supplied by setting environment variables -- `API_KEY`: A minum of 32 character api key that Synology server needs to use to auth to this services api +- `API_KEY`: A minimum of 32 character api key that Synology server needs to use to auth to this services api - `LISTEN_PORT`: Default `8080`. The port the service will listen on ## Slack settings - `SLACK_WEBHOOK`: URL for the Slack web hook +- `SLACK_ATTACHMENT_COLOR`: Color to use for the attachments can use hex `#36a64f` ## Cmd @@ -29,7 +30,7 @@ listening on port 8080 ## Docker ```bash -docker run -e API_KEY='LO45UXS%amLAWJn6CwJ1koaXW&7pY9#Z' -e SLACK_WEBHOOK='https://slack.com/myWebHookUrl' ryancurrah/synology-notifications:latest +docker run -e API_KEY='LO45UXS%amLAWJn6CwJ1koaXW&7pY9#Z' -e SLACK_WEBHOOK='https://slack.com/myWebHookUrl' -p 8080:8080 ryancurrah/synology-notifications:latest listening on port 8080 ``` @@ -37,20 +38,20 @@ listening on port 8080 1. Login to Diskstation 2. Go to `Control Pannel` > `Notification` > `SMS` -3. Check `Enable SMS Notifcations` +3. Check `Enable SMS Notifications` 4. Click `Add SMS Provider` to create a new SMS provider which will be the `synology-notifications` service. (**NOTE**: We will not actually be using `SMS`) 1. **Provider Name**: `synology-notifications` 2. **SMS URL**: `http://:8080` 3. **HTTP Method**: `POST` 5. Click `Add` and set the `Parameter` to `api_key` and leave Value empty then click `Next` -6. Click `Add` and set the `Paramater` to `phone_number` and leave Value empty (**NOTE**: Synology requires this field to exist even though were not going to use it) -7. Click `Add` and set the `Paramater` to `message` set the Value to `Hello world` (Synology requires a sample value) and click `Next` +6. Click `Add` and set the `Parameter` to `phone_number` and leave Value empty (**NOTE**: Synology requires this field to exist even though were not going to use it) +7. Click `Add` and set the `Parameter` to `message` set the Value to `Hello world` (Synology requires a sample value) and click `Next` 8. Set the type of the `api_key` Parameter to `API Key` 9. Set the type of the `phone_number` Parameter to `Phone Number` 10. Set the type of the `message` Parameter to `Message content` and click `Apply` 11. Select `synology-notifications` from the `SMS service provider` dropdown 12. In the `API Key` field paste the API Key you choose for the service and click `Apply` -13. Test the notifcation service by clicking `Send a test SMS message` +13. Test the notification service by clicking `Send a test SMS message` ## Setting up Synology with screenshots diff --git a/main.go b/main.go index df1ae0c..d8e09e3 100644 --- a/main.go +++ b/main.go @@ -20,16 +20,17 @@ type SynologyMessage struct { type AppConfig struct { ListenPort string `env:"LISTEN_PORT" envDefault:"8080"` - ApiKey string `env:"API_KEY,required"` + APIKey string `env:"API_KEY,required"` } type SlackConfig struct { Webhook string `env:"SLACK_WEBHOOK,required"` + Color string `env:"SLACK_ATTACHMENT_COLOR" envDefault:"warning"` } -// PostHandler send notifcations from synology to slack +// PostHandler send notifications from synology to slack func PostHandler(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("api_key") != appConfig.ApiKey { + if r.Header.Get("api_key") != appConfig.APIKey { http.Error(w, "invalid api key", http.StatusUnauthorized) log.Printf("invalid api key") return @@ -51,7 +52,7 @@ func PostHandler(w http.ResponseWriter, r *http.Request) { return } - msg := slack.WebhookMessage{Attachments: []slack.Attachment{{Color: "warning", Text: fmt.Sprintf("%s", synologyMessage.Message)}}} + msg := slack.WebhookMessage{Attachments: []slack.Attachment{{Color: slackConfig.Color, Text: fmt.Sprintf("%s", synologyMessage.Message)}}} err = slack.PostWebhook(slackConfig.Webhook, &msg) if err != nil { @@ -76,8 +77,8 @@ func main() { panic(err) } - if len(appConfig.ApiKey) < 32 { - panic(fmt.Errorf("api key not long enough it should be 32 characters long not %d", len(appConfig.ApiKey))) + if len(appConfig.APIKey) < 32 { + panic(fmt.Errorf("api key not long enough it should be 32 characters long not %d", len(appConfig.APIKey))) } mux := http.NewServeMux()