Skip to content

Commit

Permalink
feat: Add low balance notification (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkJD authored Dec 27, 2023
1 parent ce4a049 commit 27c563d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 17 deletions.
10 changes: 7 additions & 3 deletions .munch-o-matic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions cmd/cobra_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ var menuWeeks int
var menuDay string
var autoOrderStrategy string
var formatOutput string
var ntfyTopic string
var message string
var title string
27 changes: 27 additions & 0 deletions cmd/notification.go
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)
}
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ var rootCmd = &cobra.Command{
}

// Update configuration
viper.Set("Core.SessionCredentials.SessionID", cli.SessionID)
viper.Set("Core.SessionCredentials.UserId", cli.UserId)
viper.Set("Core.SessionCredentials.CustomerId", cli.CustomerId)
viper.Set("client.SessionCredentials.SessionID", cli.SessionID)
viper.Set("client.SessionCredentials.UserId", cli.UserId)
viper.Set("client.SessionCredentials.CustomerId", cli.CustomerId)
err = viper.WriteConfig()
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ValidateConfig(Config Config) error {

switch job.Type {
case "CheckBalance":
if email, ok := job.Params["email"].(string); !ok || email == "" {
if topic, ok := job.Params["topic"].(string); !ok || topic == "" {
return fmt.Errorf("CheckBalance job '%v' is missing or has an invalid Email", job.Name)
}
if minBalance, ok := job.Params["minbalance"].(int); !ok || minBalance <= 0 {
Expand Down
25 changes: 15 additions & 10 deletions core/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ func NewDaemon(Cfg Config, Cli *client.RestClient) (*Daemon, error) {
func (d *Daemon) AddJob(StatusChan chan string, Job Job) error {
switch Job.Type {
case "CheckBalance":
email, ok1 := Job.Params["email"].(string)
topic, ok1 := Job.Params["topic"].(string)
minBalance, ok2 := Job.Params["minbalance"].(int)
if !ok1 || !ok2 {
template, ok3 := Job.Params["template"].(string)
if !ok1 || !ok2 || !ok3 {
return fmt.Errorf("invalid parameter types for CheckBalance")
}

_, err := d.chron.AddFunc(Job.Schedule, d.sendLowBalanceEmail(StatusChan, minBalance, email))
_, err := d.chron.AddFunc(Job.Schedule, d.sendLowBalanceNotification(StatusChan, minBalance, topic, template))
if err != nil {
return fmt.Errorf("error adding job: %w", err)
}
Expand Down Expand Up @@ -110,14 +111,18 @@ func (d Daemon) orderFood(ch chan string, Strategy string, WeeksInAdvance int) f
}
}

func (d Daemon) sendLowBalanceEmail(ch chan string, MinBalance int, Email string) func() {
func (d Daemon) sendLowBalanceNotification(ch chan string, MinBalance int, Topic string, Template string) func() {
return func() {
// Simulating checking balance and sending email
balance := 50 // let's say
if balance < 100 {
ch <- fmt.Sprintf("Balance < %v; Email sent to %v", MinBalance, Email)
} else {
ch <- "Balance is okay."
ch <- fmt.Sprint("Checking account balance")
user, err := d.cli.GetUser()
if err != nil {
ch <- fmt.Sprintf("trouble getting user details: %v", err.Error())
}

if user.User.Customer.AccountBalance.Amount <= MinBalance {
ch <- fmt.Sprint("Account balance below minimum")
SendTemplateNotification("thinkjd_munch_o_matic", "Low balance notification", Template, user)
}

}
}
46 changes: 46 additions & 0 deletions core/notification.go
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")

Check failure on line 39 in core/notification.go

View workflow job for this annotation

GitHub Actions / build

fmt.Errorf format %w reads arg #1, but call has 0 args
}
if resp.StatusCode != 200 {
return fmt.Errorf("send notification failed with status %s", resp.Status)
}

return nil
}

0 comments on commit 27c563d

Please sign in to comment.