school-portal-substitution-.../matrix-bot/matrix-bot.go

131 lines
3.2 KiB
Go
Executable File

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 += "<b>✅ Vertretung - Keine Einträge vorhanden</b>"
} else {
if len(todayRepresentation) > 0 {
msg += "<b>⚠️ Vertretung - Heute</b>"
for _, entity := range todayRepresentation {
msg += "<br>Stunde: <b>" + entity.Hour + "</b>"
msg += "<br>Klasse: <b>" + entity.Class + "</b>"
msg += "<br>Vertreter: <b>" + entity.Representative + "</b>"
msg += "<br>Lehrer: <b>" + entity.Teacher + "</b>"
msg += "<br>Fach: <b>" + entity.Subject + "</b>"
msg += "<br>Raum: <b>" + entity.Room + "</b>"
msg += "<br>Hinweis: <b>" + entity.Note + "</b>"
msg += "<br>"
}
}
if len(nextDayRepresentation) > 0 {
msg += "<br><b>⚠️ Vertretung - Nächster Tag</b>"
for _, entity := range nextDayRepresentation {
msg += "<br>Stunde: <b>" + entity.Hour + "</b>"
msg += "<br>Klasse: <b>" + entity.Class + "</b>"
msg += "<br>Vertreter: <b>" + entity.Representative + "</b>"
msg += "<br>Lehrer: <b>" + entity.Teacher + "</b>"
msg += "<br>Fach: <b>" + entity.Subject + "</b>"
msg += "<br>Raum: <b>" + entity.Room + "</b>"
msg += "<br>Hinweis: <b>" + entity.Note + "</b>"
msg += "<br>"
}
}
}
msg += "<br>" + 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
}