-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (115 loc) · 3.06 KB
/
main.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
ics "github.com/arran4/golang-ical"
"github.com/gtuk/discordwebhook"
"github.com/joho/godotenv"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
func main() {
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
// URL of the .ics file in .env
icsUrl := os.Getenv("ICS_URL")
// webhook URL in .env
webhook := os.Getenv("WEBHOOK_URL")
//set the username and avatar of the bot
username := "📆 Planning Bot"
avatar := os.Getenv("AVATAR_URL")
// set the time location to Europe/Paris
location, err := time.LoadLocation("Europe/Paris")
if err != nil {
log.Fatalf("Error loading time")
}
today := time.Now().In(location)
tomorrow := today.AddDate(0, 0, 1)
var todayCourse []*ics.VEvent
var todayEvent []*ics.VEvent
var tomorrowCourse []*ics.VEvent
var tomorrowEvent []*ics.VEvent
weatherChan := make(chan []discordwebhook.Field)
go getWeather(weatherChan)
// Fetch the calendar
cal := getCal(icsUrl)
// store the events of today and tomorrow
for _, event := range cal.Events() {
// Print the event as JSON
at, err := event.GetStartAt()
if err != nil {
return
}
if DateEqual(at, today) {
category := event.GetProperty("CATEGORIES").Value
if category == "Cours" {
todayCourse = append(todayCourse, event)
}
if category == "Important" {
todayEvent = append(todayEvent, event)
}
}
if DateEqual(at, tomorrow) {
category := event.GetProperty("CATEGORIES").Value
if category == "Cours" {
tomorrowCourse = append(tomorrowCourse, event)
}
if category == "Important" {
tomorrowEvent = append(tomorrowEvent, event)
}
}
}
var embeds []discordwebhook.Embed
weather := <-weatherChan
if len(todayCourse) > 0 || len(todayEvent) > 0 {
embeds = append(embeds, getEmbed(todayCourse, todayEvent, "today", weather[0]))
}
if len(tomorrowCourse) > 0 || len(tomorrowEvent) > 0 {
embeds = append(embeds, getEmbed(tomorrowCourse, tomorrowEvent, "tomorrow", weather[1]))
}
sendMessage(webhook, username, avatar, embeds)
}
func sendMessage(webhook string, username string, avatar string, embed []discordwebhook.Embed) {
message := discordwebhook.Message{
Username: &username,
AvatarUrl: &avatar,
Embeds: &embed,
}
err := discordwebhook.SendMessage(webhook, message)
if err != nil {
log.Fatal(err)
}
}
func getCal(icsUrl string) *ics.Calendar {
resp, err := http.Get(icsUrl)
if err != nil {
log.Fatal("Error fetching .ics file:", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal("Error closing response body:", err)
}
}(resp.Body)
// Read the .ics file contents
icsData, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading .ics file:", err)
}
// Parse the .ics data
cal, err := ics.ParseCalendar(strings.NewReader(string(icsData)))
if err != nil {
log.Fatal("Error parsing .ics data:", err)
}
return cal
}
func DateEqual(date1, date2 time.Time) bool {
y1, m1, d1 := date1.Date()
y2, m2, d2 := date2.Date()
return d1 == d2 && m1 == m2 && y1 == y2
}