package matrixbot
import (
"encoding/base64"
"encoding/json"
"log"
"time"
"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"`
}
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
if today != "-" {
representation, err := Base64DecodeAndUnmarshal(today)
if err != nil {
log.Fatalln(err)
}
todayRepresentation = representation
}
if nextDay != "-" {
representation, err := Base64DecodeAndUnmarshal(nextDay)
if err != nil {
log.Fatalln(err)
}
nextDayRepresentation = representation
}
var msg string
dt := time.Now()
if len(todayRepresentation) == 0 && len(nextDayRepresentation) == 0 { // no entries
msg += "✅ Vertretung - Keine Einträge vorhanden"
} else {
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 += "
"
}
}
}
msg += "
" + dt.Format("02.01.2006 15:04:05")
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
}