commit c4fb2d6b5262518b58b7c3a529f6392f5fc314b4 Author: alex Date: Thu Jun 30 19:06:37 2022 +0200 init project diff --git a/config.sample.yaml b/config.sample.yaml new file mode 100755 index 0000000..910def9 --- /dev/null +++ b/config.sample.yaml @@ -0,0 +1,5 @@ +bot: + homeserver_url: "your_homeserver_url" + user: "your_bot_user" + password: "your_bot_password" + roomId: "news_room_id" \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100755 index 0000000..d9c1683 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module roese.dev/kra-jenkins-matrixbot-message + +go 1.17 + +require ( + github.com/russross/blackfriday/v2 v2.1.0 // indirect + golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect + golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + maunium.net/go/mautrix v0.10.12 // indirect +) diff --git a/go.sum b/go.sum new file mode 100755 index 0000000..9cd50f3 --- /dev/null +++ b/go.sum @@ -0,0 +1,35 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM= +golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38= +golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +maunium.net/go/maulogger/v2 v2.3.2/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= +maunium.net/go/mautrix v0.10.12 h1:GqmsksKyKrTqmLb2B6yGOawoFLPTJ3A3NtXrygAvKM8= +maunium.net/go/mautrix v0.10.12/go.mod h1:xTq6+uMCAXtQwfqjUrYd8O10oIyymbzZm02CYOMt4ek= diff --git a/main b/main new file mode 100755 index 0000000..11c9f63 Binary files /dev/null and b/main differ diff --git a/main.go b/main.go new file mode 100755 index 0000000..466c6ac --- /dev/null +++ b/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "log" + "os" + + matrixbot "roese.dev/kra-jenkins-matrixbot-message/matrix-bot" + "roese.dev/kra-jenkins-matrixbot-message/modules/config" +) + +func init() { + config.LoadConfig() +} + +func main() { + log.Println(len(os.Args)) + if len(os.Args) == 3 { + today := os.Args[1] + nextDay := os.Args[2] + + matrixbot.SendMessage(today, nextDay) + } +} diff --git a/matrix-bot/matrix-bot.go b/matrix-bot/matrix-bot.go new file mode 100755 index 0000000..6e5c2f4 --- /dev/null +++ b/matrix-bot/matrix-bot.go @@ -0,0 +1,132 @@ +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 +} diff --git a/modules/config/config.go b/modules/config/config.go new file mode 100755 index 0000000..f4ddc54 --- /dev/null +++ b/modules/config/config.go @@ -0,0 +1,35 @@ +package config + +import ( + "io/ioutil" + "log" + + "gopkg.in/yaml.v2" +) + +var Cfg Config + +type Config struct { + Bot Bot +} + +type Bot struct { + HomeserverUrl string `yaml:"homeserver_url"` + User string + Password string + RoomId string `yaml:"room_id"` +} + +func LoadConfig() { + file, err := ioutil.ReadFile("../go-chat-bot/config.yaml") + + if err != nil { + log.Fatalln("failed to load yaml file", err) + } + + err = yaml.Unmarshal(file, &Cfg) + + if err != nil { + log.Fatalln("failed to unmarshal config", err) + } +}