From e686bd0f60233aad8973773853ac0c3958a9ae2d Mon Sep 17 00:00:00 2001 From: Dwi Siswanto Date: Sat, 28 Jan 2023 19:57:28 +0700 Subject: [PATCH] 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. --- internal/alert/telegram.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/alert/telegram.go b/internal/alert/telegram.go index 63229fdb..f2717e21 100644 --- a/internal/alert/telegram.go +++ b/internal/alert/telegram.go @@ -2,25 +2,29 @@ package alert import ( "bytes" + "embed" "html/template" "strconv" - telegramBot "github.com/go-telegram-bot-api/telegram-bot-api" + tg "github.com/go-telegram-bot-api/telegram-bot-api" "teler.app/pkg/errors" ) +//go:embed template/*.tmpl +var tmpl embed.FS + func toTelegram(token string, chatID string, log map[string]string) { id, err := strconv.ParseInt(chatID, 10, 64) if err != nil { errors.Show(err.Error()) } - api, err := telegramBot.NewBotAPI(token) + api, err := tg.NewBotAPI(token) if err != nil { errors.Exit(err.Error()) } - message := telegramBot.NewMessage(id, telegramMessage(log)) + message := tg.NewMessage(id, telegramMessage(log)) message.ParseMode = "MarkdownV2" // 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) { func telegramMessage(log map[string]string) string { var buffer bytes.Buffer - tpl, err := template.ParseFiles("internal/alert/template/telegram.tmpl") + tpl, err := template.ParseFS(tmpl, "template/telegram.tmpl") if err != nil { errors.Exit(err.Error()) }