Untis-Logger/Bot/Untis.go
google-labs-jules[bot] 6c33d1a410 feat: Implement multi-user timetable tracking and notifications
Refactors the application to support multiple users for timetable logging.

- User accounts added via a Discord command are now stored in `accounts.json`.
- The main scheduling loop now fetches timetables for all registered users, in addition to the default user defined in the `.env` file.
- Notifications for registered users are now sent as direct messages, providing personalized updates. The default user continues to use the existing webhook.
- User-specific data files (e.g., `timetable_filled_<userID>.json`) are now used to keep each user's data separate.
- Error handling has been improved to prevent the application from crashing if an operation fails for a single user.
2025-10-10 10:10:15 +00:00

93 lines
1.9 KiB
Go

package Untis
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
type Params struct {
Users string `json:"user"`
Password string `json:"password"`
Client string `json:"client"`
}
type Login struct {
Id string `json:"id"`
Method string `json:"method"`
Params Params `json:"params"`
Jsonrpc string `json:"jsonrpc"`
}
type LoginResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID string `json:"id"`
Result Loginresult `json:"result"`
}
var Url = "https://thalia.webuntis.com/WebUntis/jsonrpc.do?school=Mons_Tabor"
//var Password = os.Getenv("UNTIS_PASSWORD")
//var USERS = os.Getenv("UNTIS_USER")
func Main(user, password, userID string) {
godotenv.Load("../.env")
cookies, err := Auth(user, password, userID)
if err != nil {
log.Printf("Authentication failed for user %s: %v", user, err)
return
}
Rooms(cookies)
Classes(cookies)
Subjects(cookies)
Timetable(cookies, userID)
Teachers(cookies)
}
func Auth(user, password, userID string) ([]*http.Cookie, error) {
l := Login{"2023-05-06 15:44:22.215292", "authenticate", Params{user, password, "WebUntis Test"}, "2.0"}
loginJSON, err := json.Marshal(l)
if err != nil {
return nil, err
}
login := bytes.NewReader(loginJSON)
LoginOut, err := http.Post(Url, "application/json", login)
if err != nil {
return nil, err
}
defer LoginOut.Body.Close()
cookies := LoginOut.Cookies()
log.Printf("Login successful for user: %s", user)
response, err := io.ReadAll(LoginOut.Body)
if err != nil {
return nil, err
}
var Response LoginResponse
if err := json.Unmarshal(response, &Response); err != nil {
return nil, err
}
data, err := json.MarshalIndent(Response.Result, "", " ")
if err != nil {
return nil, err
}
loginFile := "login.json"
if userID != "" {
loginFile = "login_" + userID + ".json"
}
if err := os.WriteFile(loginFile, data, 0644); err != nil {
return nil, err
}
return cookies, nil
}