summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go77
1 files changed, 67 insertions, 10 deletions
diff --git a/main.go b/main.go
index e0777ab..0d495de 100644
--- a/main.go
+++ b/main.go
@@ -1,9 +1,13 @@
package main
import (
+ "bytes"
"context"
+ "encoding/json"
+ "errors"
"fmt"
"log"
+ "net/http"
"os"
"git.devcara.com/grocy-reminders/grocy"
@@ -13,25 +17,78 @@ import (
func main() {
err := godotenv.Load(".env")
if err != nil {
- log.Panic(err)
+ log.Fatal(err)
}
- apiClient := grocy.NewClient(os.Getenv("GROCY_BASE_URL"), os.Getenv("GROCY_API_KEY"))
+ client := grocy.NewClient(os.Getenv("GROCY_BASE_URL"), os.Getenv("GROCY_API_KEY"))
- res, err := apiClient.GetUsers(context.Background())
-
- if err != nil {
- log.Panic(err)
+ if len(os.Args) != 2 {
+ log.Fatal(errors.New("Invalid number of arguments, expected 1"))
}
- for _, u := range res {
+ arg := os.Args[1]
+
+ if arg == "chore_reminder" {
+ chores, err := client.GetChores(context.Background())
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // let's build our message
+ msg := "Good $timezone friends! Just a friendly reminder as to which chores are assigned to which people! Here's a list:\n\n"
- userfield, err := apiClient.GetUserFields(context.Background(), u.Id)
+ for _, c := range chores {
+ name := c.ChoreName
+ assigneeId := c.NextExecutionAssignedToUserID
+ ufield, err := client.GetUserFields(context.Background(), assigneeId)
+ if err != nil {
+ log.Fatal(err)
+ }
+ discordid := ufield["discordid"]
+
+ msg = msg + fmt.Sprintf("**%s**: <@%s>\n\n", name, discordid)
+ }
+
+ msg = msg + "Please try to make sure that these chores get done by Monday evening! If you know you won't be able to (or you're on trash and trash is delayed), please send a message as a reply to this reminder!"
+
+ err = sendDiscordWebhook(msg)
if err != nil {
- log.Panic(err)
+ log.Fatal(err)
}
- fmt.Printf("%s (%s): %s\n", u.Username, u.DisplayName, userfield["discordid"])
+ } else { // default case
+ log.Fatal(errors.New("Invalid command"))
+ }
+}
+
+func sendDiscordWebhook(msg string) error {
+
+ webhook_msg := map[string]interface{}{
+ "content": msg,
+ "allowed_mentions": map[string]interface{}{
+ "parse": []string{},
+ },
+ }
+
+ if os.Getenv("MENTION_USERS") == "true" {
+ fmt.Println(os.Getenv("MENTION_USERS"))
+ webhook_msg["allowed_mentions"] = map[string]interface{}{
+ "parse": []string{"users"},
+ }
+ }
+
+ json_msg, err := json.Marshal(webhook_msg)
+ if err != nil {
+ return err
+ }
+
+ req, _ := http.NewRequest("POST", os.Getenv("DISCORD_WEBHOOK_URL"), bytes.NewBuffer(json_msg))
+ req.Header.Set("Content-Type", "application/json")
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return err
}
+ log.Printf(res.Status)
+ return nil
}