package matrixbot import ( "encoding/base64" "encoding/json" "log" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" "roese.dev/kra-jenkins-matrixbot-message/modules/config" ) type Representation struct { Hour string `json:"hour"` Class string `json:"class"` Representative string `json:"representative"` Teacher string `json:"teacher"` Subject string `json:"subject"` Room string `json:"room"` Note string `json:"note"` } // day string, hour string, class string, representative string, teacher string, subject string, room string, note string func SendMessage(today string, nextDay string) { cfg := config.Cfg.Bot client, err := mautrix.NewClient(cfg.HomeserverUrl, "", "") if err != nil { log.Fatalln(err) } _, err = client.Login(&mautrix.ReqLogin{ Type: "m.login.password", Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: cfg.User}, Password: cfg.Password, StoreCredentials: true, }) if err != nil { log.Fatalln(err) } var todayRepresentation []Representation var nextDayRepresentation []Representation log.Println(today) if today != "-" { representation, err := Base64DecodeAndUnmarshal(today) if err != nil { log.Fatalln(err) } todayRepresentation = representation log.Println(representation) } if nextDay != "-" { representation, err := Base64DecodeAndUnmarshal(nextDay) if err != nil { log.Fatalln(err) } nextDayRepresentation = representation } log.Println(nextDayRepresentation, todayRepresentation) if len(todayRepresentation) == 0 && len(nextDayRepresentation) == 0 { return } var msg string if len(todayRepresentation) > 0 { msg += "Vertretung - Heute" for _, entity := range todayRepresentation { msg += "
Stunde: " + entity.Hour + "" msg += "
Klasse: " + entity.Class + "" msg += "
Vertreter: " + entity.Representative + "" msg += "
Lehrer: " + entity.Teacher + "" msg += "
Fach: " + entity.Subject + "" msg += "
Raum: " + entity.Room + "" msg += "
Hinweis: " + entity.Note + "" msg += "
" } } if len(nextDayRepresentation) > 0 { msg += "
Vertretung - Nächster Tag" for _, entity := range nextDayRepresentation { msg += "
Stunde: " + entity.Hour + "" msg += "
Klasse: " + entity.Class + "" msg += "
Vertreter: " + entity.Representative + "" msg += "
Lehrer: " + entity.Teacher + "" msg += "
Fach: " + entity.Subject + "" msg += "
Raum: " + entity.Room + "" msg += "
Hinweis: " + entity.Note + "" msg += "
" } } botmsg := format.RenderMarkdown(msg, false, true) client.SendMessageEvent(id.RoomID(cfg.RoomId), event.EventMessage, &botmsg) } func Base64DecodeAndUnmarshal(base64Encoded string) (representation []Representation, err error) { decode, err := base64.StdEncoding.DecodeString(base64Encoded) var rep []Representation if err != nil { return rep, err } err = json.Unmarshal(decode, &rep) if err != nil { return rep, err } return rep, nil }