Skip to content

Commit

Permalink
Merge pull request #102 from LucaBernstein/improvement-template
Browse files Browse the repository at this point in the history
PoC for templating strings
  • Loading branch information
LucaBernstein authored Jan 21, 2022
2 parents 0448522 + d8d9cc1 commit f21a859
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
38 changes: 26 additions & 12 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,33 @@ func (bc *BotController) configHelp(m *tb.Message, err error) {
if err != nil {
errorMsg += fmt.Sprintf("Error executing your command: %s\n\n", err.Error())
}

tz, _ := time.Now().Zone()
_, err = bc.Bot.Send(m.Sender, errorMsg+fmt.Sprintf("Usage help for /%s:\n\n"+
"/%s currency <c> - Change default currency"+
"\n/%s about - Display the version this bot is running on"+
"\n\nTags will be added to each new transaction with a '#':\n"+
"\n/%s tag - Get currently set tag"+
"\n/%s tag off - Turn off tag"+
"\n/%s tag <name> - Set tag to apply to new transactions, e.g. when on vacation"+
"\n\nCreate a schedule to be notified of open transactions (i.e. not archived or deleted):\n"+
"\n/%s notify - Get current notification status"+
"\n/%s notify off - Disable reminder notifications"+
"\n/%s notify <delay> <hour> - Notify of open transaction after <delay> days at <hour> of the day (%s)",
CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, CMD_CONFIG, tz))
filledTemplate, err := h.Template(`Usage help for /{{.CONFIG_COMMAND}}:
/{{.CONFIG_COMMAND}} currency <c> - Change default currency
/{{.CONFIG_COMMAND}} about - Display the version this bot is running on
Tags will be added to each new transaction with a '#':
/{{.CONFIG_COMMAND}} tag - Get currently set tag
/{{.CONFIG_COMMAND}} tag off - Turn off tag
/{{.CONFIG_COMMAND}} tag <name> - Set tag to apply to new transactions, e.g. when on vacation
Create a schedule to be notified of open transactions (i.e. not archived or deleted):
/{{.CONFIG_COMMAND}} notify - Get current notification status
/{{.CONFIG_COMMAND}} notify off - Disable reminder notifications
/{{.CONFIG_COMMAND}} notify <delay> <hour> - Notify of open transaction after <delay> days at <hour> of the day ({{.TZ}})",
`, map[string]interface{}{
"CONFIG_COMMAND": CMD_CONFIG,
"TZ": tz,
})
if err != nil {
bc.Logf(ERROR, m, "Parsing configHelp template failed: %s", err.Error())
}

_, err = bc.Bot.Send(m.Sender, errorMsg+filledTemplate)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand Down
20 changes: 20 additions & 0 deletions helpers/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package helpers

import (
"bytes"
"fmt"
"html/template"
)

func Template(tmpl string, values map[string]interface{}) (string, error) {
message, err := template.New("").Parse(tmpl)
if err != nil {
return "", fmt.Errorf("creating template failed: %s", err.Error())
}
buf := new(bytes.Buffer)
err = message.Execute(buf, values)
if err != nil {
return "", fmt.Errorf("filling template failed: %s", err.Error())
}
return buf.String(), nil
}
17 changes: 17 additions & 0 deletions helpers/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package helpers_test

import (
"testing"

"github.com/LucaBernstein/beancount-bot-tg/helpers"
)

func TestTemplateParsing(t *testing.T) {
s, err := helpers.Template("Fill in this: {{.TestValue}}", map[string]interface{}{"TestValue": "Wurzelgemüse"})
if err != nil {
t.Errorf("Error encountered parsing template: %s", err.Error())
}
if s != "Fill in this: Wurzelgemüse" {
t.Errorf("Unexpected string after templating: %s", s)
}
}

0 comments on commit f21a859

Please sign in to comment.