Skip to content

Commit f687eff

Browse files
allow using templates to generate messages (#26)
1 parent bf5331c commit f687eff

File tree

7 files changed

+173
-62
lines changed

7 files changed

+173
-62
lines changed

teamup-rocketchat-bot/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ LOG_FILE_NAME: teamup-rocket-chat.log
4848
- `--config Point to the configuration (config) file. It overrides the default configuration file located at app directory`
4949
- `--logpath Set the custom logpath. It overrides the log path specified in configuration (config.yml) file`
5050
- `--help Show help and usage screen`
51+
52+
#### templates
53+
Messages can be customized by calendar, for that Go-templates are used.
54+
1. get the ID of your calendar:
55+
1. go to the settings page of teamup
56+
2. go to the calendars list
57+
3. click on the calendar you want to create a template for
58+
4. copy the number at the end of the URL e.g. `123456` of `https://teamup.com/c/xdfesf/jankaritech/settings/calendars/edit/123456`, this is your calendar id
59+
2. copy `example.tmpl` to `<calendar-id>.tmpl`
60+
3. [adjust the template](https://pkg.go.dev/text/template).
61+
- **functions** can be used inside the template to perform some operations.
62+
Available functions:
63+
- all [sprig](https://masterminds.github.io/sprig/) functions
64+
- `NotesInMarkdown` it returns the notes converted from HTML to Markdown
65+
- **values** are used to display the actual data in the template. For that all fields of the `TeamupEvent` struct are available. Here are some specially useful ones:
66+
1. `.Title`
67+
2. `.Who`
68+
3. `.Location`
69+
4. `.Notes`
70+
5. `.StartDt`
71+
6. `.EndDt`
72+
4. repeat for every calendar for which you want to have a custom message for
73+
5. if there is no template found, a built-in default template will be used.
74+
5175
### Run locally
5276

5377
*Note*: Make sure that the bot has already been added to channel or room

teamup-rocketchat-bot/config.yml.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copy this config.yml.example to config.yml
1+
# Copy this config.yml.example to config.yml
22
# and edit the following details
33
#
44
# Donot remove or add fields as the configuration is checked strictly
@@ -40,3 +40,6 @@ LOG_PATH: /var/log
4040
# name for the log file
4141
# default log file name is teamup-rocket-chat.log
4242
LOG_FILE_NAME: teamup-rocket-chat.log
43+
44+
# where are the templates stored
45+
TEMPLATE_PATH: ./

teamup-rocketchat-bot/configuration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Configuration struct {
2222
RepeatIn int `yaml:"REPEAT_IN"`
2323
LogPath string `yaml:"LOG_PATH"`
2424
LogFileName string `yaml:"LOG_FILE_NAME"`
25+
TemplatePath string `yaml:"TEMPLATE_PATH"`
2526
}
2627

2728
// Prints beatiful

teamup-rocketchat-bot/example.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{ .Who }} **{{ .Title }}** will start soon
2+
Start-time: **{{ dateInZone "02 Jan 06 15:04" (toDate "2006-01-02T15:04:05Z07:00" (.StartDt)) "Asia/Kathmandu" }}**
3+
let's meet in {{ .Location }}
4+
5+
{{ (NotesInMarkdown) }}

teamup-rocketchat-bot/go.mod

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
module github.com/jankaritech/hubot/teamup-rocketchat-bot
22

3-
go 1.19
3+
go 1.23.0
4+
5+
toolchain go1.24.1
46

57
require (
6-
github.com/badkaktus/gorocket v0.1.3
7-
github.com/go-resty/resty/v2 v2.7.0
8+
github.com/JohannesKaufmann/html-to-markdown/v2 v2.3.3
9+
github.com/Masterminds/sprig/v3 v3.3.0
10+
github.com/badkaktus/gorocket v0.1.4
11+
github.com/go-resty/resty/v2 v2.16.5
812
gopkg.in/yaml.v3 v3.0.1
913
)
1014

1115
require (
16+
dario.cat/mergo v1.0.1 // indirect
17+
github.com/JohannesKaufmann/dom v0.2.0 // indirect
18+
github.com/Masterminds/goutils v1.1.1 // indirect
19+
github.com/Masterminds/semver/v3 v3.3.0 // indirect
20+
github.com/davecgh/go-spew v1.1.1 // indirect
1221
github.com/google/go-querystring v1.1.0 // indirect
13-
golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
22+
github.com/google/uuid v1.6.0 // indirect
23+
github.com/huandu/xstrings v1.5.0 // indirect
24+
github.com/mitchellh/copystructure v1.2.0 // indirect
25+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
27+
github.com/shopspring/decimal v1.4.0 // indirect
28+
github.com/spf13/cast v1.7.0 // indirect
29+
github.com/stretchr/testify v1.10.0 // indirect
30+
golang.org/x/crypto v0.39.0 // indirect
31+
golang.org/x/net v0.41.0 // indirect
1432
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
1533
)

teamup-rocketchat-bot/go.sum

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,76 @@
1-
github.com/badkaktus/gorocket v0.1.3 h1:ON4YQeuHGYdV6CuD759bGUJ8oxTDdhL9PTVH6QYypFg=
2-
github.com/badkaktus/gorocket v0.1.3/go.mod h1:tR6QXhjPxNQGDGqmiQo+r5hyvw4lkhqx4C6KXiN8KHw=
3-
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
4-
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
5-
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
1+
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
2+
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
3+
github.com/JohannesKaufmann/dom v0.2.0 h1:1bragmEb19K8lHAqgFgqCpiPCFEZMTXzOIEjuxkUfLQ=
4+
github.com/JohannesKaufmann/dom v0.2.0/go.mod h1:57iSUl5RKric4bUkgos4zu6Xt5LMHUnw3TF1l5CbGZo=
5+
github.com/JohannesKaufmann/html-to-markdown/v2 v2.3.3 h1:r3fokGFRDk/8pHmwLwJ8zsX4qiqfS1/1TZm2BH8ueY8=
6+
github.com/JohannesKaufmann/html-to-markdown/v2 v2.3.3/go.mod h1:HtsP+1Fchp4dVvaiIsLHAl/yqL3H1YLwqLC9kNwqQEg=
7+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
8+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
9+
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
10+
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
11+
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
12+
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
13+
github.com/badkaktus/gorocket v0.1.4 h1:fApfgfrlji6l7J8C45V620GtaeWfVbibdiNn/rXvMuI=
14+
github.com/badkaktus/gorocket v0.1.4/go.mod h1:pY+hE1K871gXjzRNij2jO1l0Ab0Na/hq03R4jJlE7wg=
15+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
16+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
17+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
18+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
19+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
20+
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
21+
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
622
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
23+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
24+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
725
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
826
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
9-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
27+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
28+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
29+
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
30+
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
1031
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
32+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
33+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
1134
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
12-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1335
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
14-
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
15-
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
16-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
17-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
19-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
20-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
36+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
37+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
38+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
39+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
40+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
41+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
42+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
43+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
44+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
45+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
46+
github.com/sebdah/goldie/v2 v2.5.5 h1:rx1mwF95RxZ3/83sdS4Yp7t2C5TCokvWP4TBRbAyEWY=
47+
github.com/sebdah/goldie/v2 v2.5.5/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
48+
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
49+
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
50+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
51+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
52+
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
53+
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
54+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
55+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
56+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
57+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
58+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
59+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
60+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
61+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
62+
github.com/yuin/goldmark v1.7.11 h1:ZCxLyDMtz0nT2HFfsYG8WZ47Trip2+JyLysKcMYE5bo=
63+
github.com/yuin/goldmark v1.7.11/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
64+
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
65+
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
66+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
67+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
68+
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
69+
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
2170
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2271
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2372
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
2473
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
74+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2575
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2676
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

teamup-rocketchat-bot/main.go

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package main
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"fmt"
78
"log"
89
"os"
910
"path"
10-
"regexp"
11+
"strconv"
1112
"strings"
1213
"sync"
14+
"text/template"
1315
"time"
1416

1517
"flag"
1618

19+
htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
20+
"github.com/Masterminds/sprig/v3"
1721
"github.com/badkaktus/gorocket"
1822
"github.com/go-resty/resty/v2"
1923
)
@@ -229,7 +233,11 @@ func checkForMeetings(config *Configuration, chatClient *gorocket.Client) {
229233
diff := timeDiffWithNow(event.StartDt)
230234
if diff > 10 && diff < 21 {
231235
toNotifyEventsIds = append(toNotifyEventsIds, EventIDWithStartTime{event.ID, event.StartDt})
232-
toSendMsgs = append(toSendMsgs, prepareMeetingMsg(event))
236+
message, err := prepareMeetingMsg(event, config)
237+
if err != nil {
238+
logger.Printf("failed to create message due to following error:\n%s", err.Error())
239+
}
240+
toSendMsgs = append(toSendMsgs, message)
233241
}
234242
}
235243

@@ -529,50 +537,52 @@ func getFutureEvents(events *TeamupEvents, alreadyNotified []EventIDWithStartTim
529537
return futureEvents
530538
}
531539

532-
// prepareMeetingMsg prepares message to be
533-
// sent for the upcoming event
534-
// with following format
535-
//
536-
// @mentions *Event title* will start soon
537-
// Start-time: 10:00
538-
// let's meet in:
539-
// Some description
540-
// of the meeting
541-
func prepareMeetingMsg(event TeamupEvent) string {
542-
title := event.Title
543-
who := event.Who
544-
locale, _ := time.LoadLocation("Asia/Kathmandu")
545-
startTime, _ := time.Parse(time.RFC3339, event.StartDt) // Parse the time in locale time
546-
location := event.Location
547-
notes := event.Notes
548-
description := strings.Split(notes, "\n")
549-
descSlice := make([]string, 0)
550-
551-
// Catches the html tags present
552-
removeTag := regexp.MustCompile(`<\/?[^>]+(>|$)`) // Create a global func for this
553-
// Catches the whole line containing Who: @mention1 @mention2
554-
removeMention := regexp.MustCompile(`^(Who|who):\s*@.*$`) // Create a global func for this
555-
556-
// Trim the white space, remove html tags, remove "Who: @"
557-
for i := 0; i < len(description); i++ {
558-
str := strings.TrimSpace(description[i])
559-
str = removeTag.ReplaceAllString(str, "")
560-
str = removeMention.ReplaceAllString(str, "")
561-
// Only add the non-empty lines
562-
if len(str) > 0 {
563-
descSlice = append(descSlice, str)
540+
// prepareMeetingMsg reads a template with the name subcalendarID + .tmpl
541+
// renders it and returns a string of the message
542+
func prepareMeetingMsg(event TeamupEvent, config *Configuration) (string, error) {
543+
templateFile := strconv.Itoa(event.SubcalendarID) + ".tmpl"
544+
templateFullPath := path.Join(config.TemplatePath, templateFile)
545+
funcMap := template.FuncMap{
546+
"NotesInMarkdown": func() string {
547+
markdown, _ := htmltomarkdown.ConvertString(event.Notes)
548+
return markdown
549+
},
550+
}
551+
// add all the functions from sprig
552+
for i, f := range sprig.FuncMap() {
553+
funcMap[i] = f
554+
}
555+
556+
var tmpl *template.Template
557+
// Check if the template file exists and is readable
558+
if _, err := os.Stat(templateFullPath); err != nil {
559+
logger.Printf("No template with filename '%s' not found, using default template\n", templateFullPath)
560+
defaultTemplate := "**REMINDER**\n" +
561+
"_{{ .Title }}_\n" +
562+
"Who: {{ .Who }}\n" +
563+
"Start-time: **{{ toDate \"2006-01-02T15:04:05Z07:00\" (.StartDt) | date \"02 Jan 06 15:04\"}}**\n" +
564+
"End-time: **{{ toDate \"2006-01-02T15:04:05Z07:00\" (.EndDt) | date \"02 Jan 06 15:04\"}}**\n" +
565+
"Location: {{ .Location }}\n" +
566+
"Notes: {{ (NotesInMarkdown) }}"
567+
tmpl, err = template.New("default").Funcs(funcMap).Parse(defaultTemplate)
568+
if err != nil {
569+
return "", err
564570
}
571+
} else {
572+
tmpl, err = template.New(templateFile).Funcs(funcMap).ParseFiles(templateFullPath)
573+
if err != nil {
574+
return "", err
575+
}
576+
577+
}
578+
var buff bytes.Buffer
579+
580+
err := tmpl.Execute(&buff, event)
581+
if err != nil {
582+
return "", err
565583
}
566-
finalDesc := strings.Join(descSlice, "\n")
567584

568-
msg := fmt.Sprintf("%s *%s* will start soon\nStart-time: *%s*\nlet's meet in: %s\n%s",
569-
who,
570-
title,
571-
strings.Split(startTime.In(locale).String(), "+")[0],
572-
location,
573-
finalDesc,
574-
)
575-
return msg
585+
return buff.String(), nil
576586
}
577587

578588
// Some struct definitions

0 commit comments

Comments
 (0)