Skip to content

Commit

Permalink
add account balance notification
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkJD committed Dec 27, 2023
1 parent 36ddc43 commit 4fc4950
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .munch-o-matic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Core:
- name: Check Account Balance
params:
topic: thinkjd_munch_o_matic
minbalance: 5
minbalance: 3000
template: |
Your account balance is low: {{.AccountBalance}}€
schedule: "*/10 * * * * *"
Expand Down
13 changes: 12 additions & 1 deletion cmd/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ var notification = &cobra.Command{
Use: "send-notification",
Short: "Send notifications",
Run: func(cmd *cobra.Command, args []string) {
core.SendAccountBalanceNotification(3000)

template := "Hello, your balance: {{.User.Customer.AccountBalance.Amount}}"

user, err := cli.GetUser()
if err != nil {
print("Error: ", err)
}

err = core.SendTemplateNotification("thinkjd_munch_o_matic", template, user)
if err != nil {
print(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
26 changes: 16 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,19 @@ 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.Sprintf("Account balance below minimum")
template := "Hello, your balance: {{.User.Customer.AccountBalance.Amount}}"
SendTemplateNotification("thinkjd_munch_o_matic", template, user)
}

}
}
23 changes: 9 additions & 14 deletions core/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@ import (
"text/template"
)

func SendAccountBalanceNotification(AccountBalance int) error {
tplString := `Account balance low: {{.AccountBalance}}`

tmpl, err := template.New("accountBalance").Parse(tplString)
// SendTemplateNotification takes a template and matching sate to render the template and
// send to the provided topic.
func SendTemplateNotification(Topic 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

data := map[string]int{
"AccountBalance": AccountBalance,
}

err = tmpl.Execute(&buf, data)
err = tmpl.Execute(&buf, Data)
if err != nil {
return fmt.Errorf("execute template: %w", err)
}
resultString := buf.String()

err = SendNotification("thinkjd_munch_o_matic", resultString)
renderedTemplate := buf.String()
err = SendNotification(Topic, renderedTemplate)
if err != nil {
return fmt.Errorf("Sending failed: %w", err)
return fmt.Errorf("sending failed: %w", err)
}
return nil
}

// SendNotification sends the provided message to the topic
func SendNotification(Topic string, Content string) error {
http.Post("https://ntfy.sh/"+Topic, "text/markdown",
strings.NewReader(Content))
Expand Down

0 comments on commit 4fc4950

Please sign in to comment.