summaryrefslogblamecommitdiff
path: root/main.go
blob: 0d495de74080a32739b3611d116c06fc9056e802 (plain) (tree)
1
2
3
4
5
6
7
8
9


            
               
                 

                       

             
                  








                                               
                              
         
                                                                                          
 

                                                                                

         









                                                                                                                                             
 


                                                                     
 











                                                                                                                                                                                                                                  
                               
                                      

                 






























                                                                                                      

         

                              
 
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
}