summaryrefslogtreecommitdiff
path: root/main.go
blob: 0d495de74080a32739b3611d116c06fc9056e802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"net/http"
	"os"

	"git.devcara.com/grocy-reminders/grocy"
	"github.com/joho/godotenv"
)

func main() {
	err := godotenv.Load(".env")
	if err != nil {
		log.Fatal(err)
	}
	client := grocy.NewClient(os.Getenv("GROCY_BASE_URL"), os.Getenv("GROCY_API_KEY"))

	if len(os.Args) != 2 {
		log.Fatal(errors.New("Invalid number of arguments, expected 1"))
	}

	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"

		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.Fatal(err)
		}

	} 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
}