added timezone managment

This commit is contained in:
Jannover5000r 2026-03-30 05:24:33 +02:00
parent 1b47e7ac04
commit 8d243869ea
3 changed files with 30 additions and 31 deletions

View file

@ -25,7 +25,6 @@
### For multi user support: using the command **!addaccount** in the discord server the bot is used in, it will than dm you with the sign up process prompting you to enter your untis Username and password (no email neededd just untis username). The username is stored as plain text and the password is hashed on the device hosting the bot, so currently it is **not secure** since the host will have access to the hashed password and the hash used! Deleting an account is done by using **!removeaccount** which will delete the account and the password, created with your discord account
## In case your school is using a different timing for the Lessons than mine, you can change the times where you will be notified with the next room and Lesson for the day in **Line 71 in the main.go** file. The bot may not send you a message if there is no lesson after the specified time so that you can leave empty lessons as is for the days
## For your timezone setup an ENV called "LOCATION_ENV" with your timezone as explained in the example env file. In case your school is using a different timing for the Lessons than mine, you can change the times where you will be notified with the next room and Lesson for the day in **Line 71 in the main.go** file. The bot may not send you a message if there is no lesson after the specified time so that you can leave empty lessons as is for the days
# I am neither a representative of Untis, Untis Baden-Württemberg GmbH nor a Developer in their team. This project is based on their API and my code and is not affiliated with them

7
exampleEnv Normal file
View file

@ -0,0 +1,7 @@
DISCORD_WEBHOOK_URL=[Paste Webhook]
UNTIS_PASSWORD=[Default user passwd]
UNTIS_USER=[Default user username]
DISCORD_BOT_TOKEN=[Paste Bot token]
ENC_KEY=[Generate enc key and paste]
URL=[Untis url, create from tutorial in readme]
LOCATION_ENV=Europe/Berlin [change to your timezone, needs to be usable by go's time package via time.LoadLocation()]

51
main.go
View file

@ -33,6 +33,8 @@ type NamedTimetableEntry struct {
ActivityType string `json:"activityType"`
}
var location *time.Location
// init and main//
func init() {
godotenv.Load(".env")
@ -50,6 +52,21 @@ func init() {
} else {
log.Println("No Discord webhook provided, Discord notifications will be disabled")
}
locEnv := os.Getenv("LOCATION_ENV")
var locErr error
location, locErr = time.LoadLocation(locEnv)
if locErr != nil {
log.Fatalf("Failed to load timezone: %v", locErr)
}
log.Printf("Timezone set to: %s", location)
}
func getTime() time.Time {
return time.Now().In(location)
}
func main() {
@ -132,7 +149,7 @@ func scheduleTimetableUpdate() {
updateAllUsers()
// Ticker for periodic updates
hourTicker := time.NewTicker(1 * time.Minute) // For testing, 1 minute. Change to 1 * time.Hour for production.
hourTicker := time.NewTicker(30 * time.Minute)
go func() {
for range hourTicker.C {
updateAllUsers()
@ -141,7 +158,7 @@ func scheduleTimetableUpdate() {
// Ticker for scheduled lesson notifications
startMinuteTicker(func() {
now := time.Now()
now := getTime()
if isScheduledTime(now) {
log.Println("Scheduled time reached, running notifications...")
accounts, _ := BotStart.LoadAndDecryptAccounts()
@ -156,7 +173,7 @@ func scheduleTimetableUpdate() {
}
func startMinuteTicker(f func()) {
now := time.Now()
now := getTime()
next := now.Truncate(time.Minute).Add(time.Minute)
time.Sleep(time.Until(next))
ticker := time.NewTicker(1 * time.Minute)
@ -185,7 +202,7 @@ func Run(userID string) {
roomByStartTime, _ := MapTimeToRoom(timetableFile)
subjectByStartTime, _ := MapTimeToSubject(timetableFile)
now := time.Now().Format("15:04")
now := getTime().Format("15:04")
nextTime, room, foundRoom := NextRoomForTime(roomByStartTime, now)
if !foundRoom {
return // No upcoming lessons
@ -417,31 +434,7 @@ func sendUpdateDiscordWebhook() {
log.Println("Sending Discord webhook notification...")
// Create a rich embed message
message := "A lesson on your timetable has changed"
/*embed := Embed{
Title: "Next Lesson ",
Description: "The next lesson is starting soon: ",
Color: 3066993, // Green color
Timestamp: time.Now().Format(time.RFC3339),
Fields: []Field{
{
Name: "New Lesson",
// Value: fmt.Sprintf("`%s`", ip),
Value: fmt.Sprintf("Room: %s", room),
Inline: true,
},
{
Name: "Start-Time",
Value: fmt.Sprintf("Start time: %s", nextTime),
Inline: true,
},
{
Name: "Status",
Value: fmt.Sprintf("Status: %s", Status),
Inline: true,
},
},
}
*/
payload := DiscordWebhookPayload{
Content: message,
}