-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add low balance notification (#28)
- Loading branch information
Showing
7 changed files
with
102 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,17 @@ Core: | |
Jobs: | ||
- name: Check Account Balance | ||
params: | ||
email: [email protected] | ||
minbalance: 5 | ||
topic: my_ntfy_topic | ||
minbalance: 3000 | ||
template: "Your account balanc is low: {{.User.Customer.AccountBalance.Amount}}ct" | ||
schedule: "*/10 * * * * *" | ||
type: CheckBalance | ||
- name: Order Food | ||
params: | ||
strategy: SchooFav | ||
weeks: 1 | ||
weeks: 2 | ||
topic: my_ntfy_topic | ||
template: | | ||
Food order | ||
schedule: "*/20 * * * * *" | ||
type: Order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"munch-o-matic/core" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var notification = &cobra.Command{ | ||
Use: "send-notification", | ||
Short: "Send notifications", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
err := core.SendNotification(ntfyTopic, title, message) | ||
if err != nil { | ||
log.Fatal("could not send notification: ", err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
notification.Flags().StringVarP(&ntfyTopic, "topic", "T", "", "Ntfy topic") | ||
notification.Flags().StringVarP(&title, "title", "t", "CLI", "Notification title") | ||
notification.Flags().StringVarP(&message, "message", "m", "", "Notificatoin message") | ||
rootCmd.AddCommand(notification) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package core | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
// SendTemplateNotification takes a template and matching sate to render the template and | ||
// send to the provided topic. | ||
func SendTemplateNotification(Topic string, Title string, Template string, Data interface{}) error { | ||
tmpl, err := template.New("template").Parse(Template) | ||
if err != nil { | ||
return fmt.Errorf("create template: %w", err) | ||
} | ||
var buf bytes.Buffer | ||
|
||
err = tmpl.Execute(&buf, Data) | ||
if err != nil { | ||
return fmt.Errorf("execute template: %w", err) | ||
} | ||
renderedTemplate := buf.String() | ||
err = SendNotification(Topic, Title, renderedTemplate) | ||
if err != nil { | ||
return fmt.Errorf("sending failed: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// SendNotification sends the provided message to the topic | ||
func SendNotification(Topic string, Title string, Message string) error { | ||
req, _ := http.NewRequest("POST", "https://ntfy.sh/"+Topic, strings.NewReader(Message)) | ||
req.Header.Set("Title", Title) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("could not send notification: %w") | ||
} | ||
if resp.StatusCode != 200 { | ||
return fmt.Errorf("send notification failed with status %s", resp.Status) | ||
} | ||
|
||
return nil | ||
} |