summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2023-09-03 12:30:50 -0400
committerCara Salter <cara@devcara.com>2023-09-03 12:30:50 -0400
commit87678ecf435874e7771b0e47911ffb2aa2c4d33e (patch)
tree83ac91d6633f80f738d9d6a3cfccab55b2d5e8f9
parent1a69cd2e8b919ab450b42802ae578c2d23948a07 (diff)
downloadgrocy-reminders-master.tar.gz
grocy-reminders-master.zip
chore remindersHEADmaster
Sends a discord webhook with reminders as to who has what chore!
-rw-r--r--grocy/user.go18
-rw-r--r--main.go77
2 files changed, 85 insertions, 10 deletions
diff --git a/grocy/user.go b/grocy/user.go
index 58d3d08..493c4d5 100644
--- a/grocy/user.go
+++ b/grocy/user.go
@@ -2,6 +2,7 @@ package grocy
import (
"context"
+ "errors"
"fmt"
"net/http"
)
@@ -35,6 +36,23 @@ func (c *Client) GetUsers(ctx context.Context) ([]User, error) {
return res, nil
}
+func (c *Client) GetUser(ctx context.Context, userId int) (User, error) {
+ // This is kinda hacky because grocy doesn't support getting LDAP users by
+ // grocy ID :(
+ res, err := c.GetUsers(ctx)
+ if err != nil {
+ return *new(User), err
+ }
+
+ for _, u := range res {
+ if u.Id == userId {
+ return u, nil
+ }
+ }
+
+ return *new(User), errors.New("User Not Found")
+}
+
func (c *Client) GetUserFields(ctx context.Context, userId int) (map[string]string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/userfields/users/%d", c.BaseUrl, userId), nil)
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
}