Skip to content

Commit

Permalink
Migrate to using jcal
Browse files Browse the repository at this point in the history
* Updates dockerfile for timezone
* Updates templates
* Update github workflow
  • Loading branch information
gueldenstone committed Apr 27, 2024
1 parent 58d9898 commit b320388
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 2,780 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19.6
go-version: 1.23
cache: true
- name: Test
run: go test -v ./...
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##
## Build
##
FROM golang:1.18-alpine AS build
FROM golang:1.23-alpine AS build

WORKDIR /app

Expand All @@ -22,6 +22,7 @@ FROM alpine

WORKDIR /
RUN apk add --no-cache tzdata
RUN ln -s /usr/share/zoneinfo/Europe/Berlin /etc/localtime
RUN adduser -D calendar-bot
USER calendar-bot
COPY --from=build /calendar-bot /calendar-bot
Expand Down
26 changes: 18 additions & 8 deletions cmd/calendar-bot/calendar-bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import (

const flags = log.Ldate | log.Ltime | log.Lmsgprefix

var errLog = log.New(os.Stderr, "[ERROR] ", flags|log.Lshortfile)
var infoLog = log.New(os.Stdout, "[INFO] ", flags)
var (
errLog = log.New(os.Stderr, "[ERROR] ", flags|log.Lshortfile)
infoLog = log.New(os.Stdout, "[INFO] ", flags)
)

var configFile = flag.String("config", "", "Path to config file")
var htmlTmplPath = flag.String("html", "", "Path to html template file")
var txtTmplPath = flag.String("txt", "", "Path to txt template file")
var (
configFile = flag.String("config", "", "Path to config file")
htmlTmplPath = flag.String("html", "", "Path to html template file")
txtTmplPath = flag.String("txt", "", "Path to txt template file")
)

func main() {
flag.Parse()
Expand Down Expand Up @@ -94,15 +98,17 @@ func main() {
timezone := time.Local
s := gocron.NewScheduler(timezone)
infoLog.Printf("Scheduling notifications for %s", notifyTime.Format("15:04"))
s.Every(1).Day().At(notifyTime).Do(func() {
_, err = s.Every(1).Day().At(time.Now().Add(2 * time.Second)).Do(func() {
infoLog.Println("Start Notification")
cal, err := calendar.ImportCalendar(conf.Calendar, infoLog)
cal, err := calendar.NewNextcloudCalendar(conf.Calendar)
if err != nil {
errLog.Printf("Could not read calendar info from %s\n", conf.Calendar)
errLog.Println(err)
return
}
todayEvents, err := cal.GetEventsOn(time.Now())
if err != nil {
errLog.Println(err)
return
}
if len(todayEvents) == 0 {
infoLog.Println("No events today!")
Expand All @@ -127,5 +133,9 @@ func main() {
}
}
})
if err != nil {
errLog.Println(err)
return
}
s.StartBlocking()
}
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
module github.com/gueldenstone/calendar-bot

go 1.18
go 1.23.0

require (
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f
github.com/go-co-op/gocron v1.13.0
github.com/go-co-op/gocron v1.37.0
github.com/xHain-hackspace/go-jcal v0.0.3
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.11.0
)

require (
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/teambition/rrule-go v1.7.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 // indirect
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
45 changes: 32 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f h1:feGUUxxvOtWVOhTko8Cbmp33a+tU0IMZxMEmnkoAISQ=
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f/go.mod h1:2MKFUgfNMULRxqZkadG1Vh44we3y5gJAtTBlVsx1BKQ=
github.com/go-co-op/gocron v1.13.0 h1:BjkuNImPy5NuIPEifhWItFG7pYyr27cyjS6BN9w/D4c=
github.com/go-co-op/gocron v1.13.0/go.mod h1:GD5EIEly1YNW+LovFVx5dzbYVcIc8544K99D8UVRpGo=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0=
github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/teambition/rrule-go v1.7.2 h1:goEajFWYydfCgavn2m/3w5U+1b3PGqPUHx/fFSVfTy0=
github.com/teambition/rrule-go v1.7.2/go.mod h1:mBJ1Ht5uboJ6jexKdNUJg2NcwP8uUMNvStWXlJD3MvU=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xHain-hackspace/go-jcal v0.0.3 h1:2FcfP0FimmhMOUCHQ6uXCWgNJnk3OQImbZSFKt8zBbI=
github.com/xHain-hackspace/go-jcal v0.0.3/go.mod h1:TtrDntq5LuHqIYYnPTBmM5fAD0EMWRf+ZS7H+3XpwHM=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20220513224357-95641704303c h1:nF9mHSvoKBLkQNQhJZNsc66z2UzAMUbLGjC95CF3pU0=
golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/mautrix v0.11.0 h1:B1FBHcvE4Mud+AC+zgNQQOw0JxSVrt40watCejhVA7w=
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

type Config struct {
Homeserver string `yaml:"homeserver"`
Rooms []string `yaml:"rooms"`
Calendar string `yaml:"calendarURL"`
NotifyTime string `yaml:"nofifyTime"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Rooms []string `yaml:"rooms"`
}

func Parse(path string) (Config, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/gueldenstone/calendar-bot/pkg/calendar"

"maunium.net/go/mautrix/event"
)

Expand All @@ -24,9 +23,9 @@ type Event struct {
}

type TemplatedMessage struct {
Events []Event
htmlTemplate *template.Template
txtTemplate *template.Template
Events []Event
}

func NewTemplatedMessage(htmlTemplate, txtTemplate string, events []calendar.Event, tz *time.Location) (TemplatedMessage, error) {
Expand All @@ -36,7 +35,7 @@ func NewTemplatedMessage(htmlTemplate, txtTemplate string, events []calendar.Eve
Summary: evt.Summary,
StartTime: evt.Start.In(tz).Format(timeLayout),
EndTime: evt.End.In(tz).Format(timeLayout),
HtmlDescription: strings.ReplaceAll(strings.ReplaceAll(evt.Description, "\\n", "<br>"), "\\", ""),
HtmlDescription: strings.ReplaceAll(strings.ReplaceAll(evt.Description, "\n", "<br>"), "\\", ""),
TxtDescription: evt.Description,
}

Expand Down Expand Up @@ -72,6 +71,7 @@ func (t TemplatedMessage) RenderTxt() (string, error) {
err := t.txtTemplate.Execute(&buf, t)
return buf.String(), err
}

func (t TemplatedMessage) Render() (html string, txt string, err error) {
html, err = t.RenderHtml()
if err != nil {
Expand Down
Loading

0 comments on commit b320388

Please sign in to comment.