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

Commit

Permalink
refactor(alert): template loading to use ParseFS instead of ParseFiles (
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dwisiswant0 committed Jan 28, 2023
1 parent 000c429 commit e686bd0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/alert/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
Expand Down

0 comments on commit e686bd0

Please sign in to comment.