Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit e686bd0

Browse files
committed
refactor(alert): template loading to use ParseFS instead of ParseFiles (fix #296)
This change updates the way alert templates are loaded in the internal/alert package by replacing the use of template.ParseFiles with template.ParseFS. This is done to make the loading of templates more flexible and allow for more options when specifying the location of the template files. Additionally, the change also updates the template file path to be more consistent with the project's file structure. This change will not affect the functionality of the program, but it will improve maintainability and scalability in the long run.
1 parent 000c429 commit e686bd0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

internal/alert/telegram.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@ package alert
22

33
import (
44
"bytes"
5+
"embed"
56
"html/template"
67
"strconv"
78

8-
telegramBot "github.com/go-telegram-bot-api/telegram-bot-api"
9+
tg "github.com/go-telegram-bot-api/telegram-bot-api"
910
"teler.app/pkg/errors"
1011
)
1112

13+
//go:embed template/*.tmpl
14+
var tmpl embed.FS
15+
1216
func toTelegram(token string, chatID string, log map[string]string) {
1317
id, err := strconv.ParseInt(chatID, 10, 64)
1418
if err != nil {
1519
errors.Show(err.Error())
1620
}
1721

18-
api, err := telegramBot.NewBotAPI(token)
22+
api, err := tg.NewBotAPI(token)
1923
if err != nil {
2024
errors.Exit(err.Error())
2125
}
2226

23-
message := telegramBot.NewMessage(id, telegramMessage(log))
27+
message := tg.NewMessage(id, telegramMessage(log))
2428
message.ParseMode = "MarkdownV2"
2529

2630
// TODO: Displays an error if it does not exceed the rate-limit
@@ -31,7 +35,7 @@ func toTelegram(token string, chatID string, log map[string]string) {
3135
func telegramMessage(log map[string]string) string {
3236
var buffer bytes.Buffer
3337

34-
tpl, err := template.ParseFiles("internal/alert/template/telegram.tmpl")
38+
tpl, err := template.ParseFS(tmpl, "template/telegram.tmpl")
3539
if err != nil {
3640
errors.Exit(err.Error())
3741
}

0 commit comments

Comments
 (0)