Skip to content

Commit

Permalink
Send notification via webhook instead
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Jun 29, 2022
1 parent 8296585 commit 2dd729c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
7 changes: 3 additions & 4 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ type Config struct {
Uploader struct {
Token string `yaml:"token"`
} `yaml:"uploader"`
Pushover struct {
User string `yaml:"user"`
Token string `yaml:"token"`
} `yaml:"pushover"`
Webhook struct {
Endpoint string `yaml:"endpoint"`
} `yaml:"webhook"`
}

// ValidateAndInit validates config and sets the Google credentials in
Expand Down
30 changes: 16 additions & 14 deletions internal/pkg/monitoring/latest_probe.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package monitoring

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/Jeffail/gabs/v2"
Expand Down Expand Up @@ -41,25 +42,26 @@ func LatestProbe(cfg config.Config) error {
}

if time.Since(t).Hours() > 24 {
URL := "https://api.pushover.net/1/messages.json"

values := url.Values{}
values.Add("token", cfg.Pushover.Token)
values.Add("user", cfg.Pushover.User)
values.Add("title", "Play Data Warning")
values.Add("message", fmt.Sprintf("%.0f hours since last play", time.Since(t).Hours()))
datab := map[string]string{
"Title": "Play Data Warning",
"Body": fmt.Sprintf("%.0f hours since last play", time.Since(t).Hours()),
"URL": "",
}

res, err := http.PostForm(URL, values)
b, err := json.Marshal(datab)
if err != nil {
return err
return fmt.Errorf("failed to marshal webhook body: %w", err)
}

data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
client := &http.Client{}
req, err := http.NewRequest("POST", cfg.Webhook.Endpoint, bytes.NewBuffer(b))

req.Header.Add("Content-Type", "application/json; charset=utf-8")

_, err = client.Do(req)
if err != nil {
return err
return fmt.Errorf("failed to send webhook: %w", err)
}
fmt.Println(string(data))
}

return nil
Expand Down

0 comments on commit 2dd729c

Please sign in to comment.