diff --git a/Bot/Untis.go b/Bot/Untis.go index e46e7d0..8e0fc1d 100644 --- a/Bot/Untis.go +++ b/Bot/Untis.go @@ -3,6 +3,7 @@ package Untis import ( "bytes" "encoding/json" + "io" "log" "net/http" "os" @@ -13,6 +14,12 @@ type Params struct { Password string `json:"key"` Client string `json:"client"` } +type getRooms struct { + Id string `json:"id"` + Method string `json:"method"` + Params Params `json:"params"` + Jsonrpc string `json:"jsonrpc:"` +} type Login struct { Id string `json:"id"` Method string `json:"method"` @@ -34,25 +41,73 @@ 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 Auth() { +func Main() { + cookies, err := Auth() + if err != nil { + log.Fatal(err) + return + } + Rooms(cookies) +} + +func Auth() ([]*http.Cookie, error) { l := Login{"2023-05-06 15:44:22.215292", "authenticate", Params{os.Getenv("UNTIS_USER"), os.Getenv("UNTIS_PASSWORD"), "WebUntis Test"}, "2.0"} loginJSON, err := json.Marshal(l) + if err != nil { + log.Fatalf("Error marshaling login data: %v", err) + return nil, err + } login := bytes.NewReader(loginJSON) + + log.Println("Run") + LoginOut, err := http.Post(Url, "application/json", login) + if err != nil { + log.Printf("Error during authentication: %v", err) + return nil, err + } + defer LoginOut.Body.Close() + + // Parse cookies from response + cookies := LoginOut.Cookies() + log.Println("Received cookies:", cookies) + return cookies, nil + +} + +func Rooms(cookies []*http.Cookie) { + log.Println("Abrufen der Stunden") + g := getRooms{"2023-05-06 15:44:22.215292", "getRooms", Params{}, "2.0"} + roomsJson, err := json.Marshal(g) if err != nil { log.Fatalf("Error marshaling login data: %v", err) return } + rooms := bytes.NewReader(roomsJson) - log.Println("Run") - LoginOut, err := http.Post(Url, "application/json", login) - + prompt, err := http.NewRequest("POST", Url, rooms) if err != nil { - log.Printf("Error during authentication: %v", err) + log.Fatalf("Error creatingrequest: %v", err) return } - defer LoginOut.Body.Close() - out := LoginOut.Header.Values("Set-Cookie") - - log.Println(out) + log.Println(prompt) + log.Println("Cookie: ", cookies) + for _, cookie := range cookies { + prompt.AddCookie(cookie) + log.Println("prompt ", prompt) + } + out, err := http.DefaultClient.Do(prompt) + if err != nil { + log.Printf("Error during request: %v", err) + return + } + defer out.Body.Close() + log.Println(out.Status) + response, err := io.ReadAll(out.Body) + if err != nil { + log.Printf("Error reading response body: %v", err) + return + } + responseString := string(response) + log.Println("Repsonse ", responseString) } diff --git a/README.md b/README.md index 27283e1..29b9b46 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# Discord bot for logging Untis timetable \ No newline at end of file +# Discord bot for logging Untis timetable to a Discord Webhook +create .env with Credentials +UNTIS_PASSWORD +UNTIS_USER +DISCORD_WEBHOOK_URL \ No newline at end of file diff --git a/main.go b/main.go index cf43001..6f23b52 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func init() { } func main() { - go Untis.Auth() //starting API calls function + go Untis.Main() //starting API calls function // Create a channel to receive OS signals sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)