2024-10-09 22:39:44 +02:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/go-mail/mail"
|
|
|
|
|
|
"html/template"
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func parseTemplate[T any](data T, fileName string) (string, error) {
|
|
|
|
|
|
// Open the file
|
|
|
|
|
|
tmpl, err := template.ParseFiles(filepath.Join(templatePath, fileName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
if err = tmpl.Execute(&buf, data); err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 04:32:03 +02:00
|
|
|
|
func SendEmail(subject, body string) error {
|
|
|
|
|
|
Info("Start sending email notification....")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
config := loadMailConfig()
|
|
|
|
|
|
emails := strings.Split(config.MailTo, ",")
|
|
|
|
|
|
m := mail.NewMessage()
|
|
|
|
|
|
m.SetHeader("From", config.MailFrom)
|
|
|
|
|
|
m.SetHeader("To", emails...)
|
|
|
|
|
|
m.SetHeader("Subject", subject)
|
|
|
|
|
|
m.SetBody("text/html", body)
|
|
|
|
|
|
d := mail.NewDialer(config.MailHost, config.MailPort, config.MailUserName, config.MailPassword)
|
|
|
|
|
|
d.TLSConfig = &tls.Config{InsecureSkipVerify: config.SkipTls}
|
|
|
|
|
|
|
|
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error("Error could not send email : %v", err)
|
|
|
|
|
|
return err
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Info("Email notification has been sent")
|
|
|
|
|
|
return nil
|
2024-10-09 22:39:44 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-10 04:32:03 +02:00
|
|
|
|
func sendMessage(msg string) error {
|
2024-10-09 22:39:44 +02:00
|
|
|
|
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Info("Sending Telegram notification... ")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
chatId := os.Getenv("TG_CHAT_ID")
|
|
|
|
|
|
body, _ := json.Marshal(map[string]string{
|
|
|
|
|
|
"chat_id": chatId,
|
|
|
|
|
|
"text": msg,
|
|
|
|
|
|
})
|
|
|
|
|
|
url := fmt.Sprintf("%s/sendMessage", getTgUrl())
|
|
|
|
|
|
// Create an HTTP post request
|
|
|
|
|
|
request, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
response, err := client.Do(request)
|
|
|
|
|
|
if err != nil {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
return err
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
code := response.StatusCode
|
|
|
|
|
|
if code == 200 {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Info("Telegram notification has been sent")
|
|
|
|
|
|
return nil
|
2024-10-09 22:39:44 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error("Error could not send message, error: %s", string(body))
|
|
|
|
|
|
return fmt.Errorf("error could not send message %s", string(body))
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
func NotifySuccess(notificationData *NotificationData) {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
notificationData.BackupReference = backupReference()
|
2024-10-09 22:39:44 +02:00
|
|
|
|
var vars = []string{
|
|
|
|
|
|
"TG_TOKEN",
|
|
|
|
|
|
"TG_CHAT_ID",
|
|
|
|
|
|
}
|
|
|
|
|
|
var mailVars = []string{
|
|
|
|
|
|
"MAIL_HOST",
|
|
|
|
|
|
"MAIL_PORT",
|
|
|
|
|
|
"MAIL_USERNAME",
|
|
|
|
|
|
"MAIL_PASSWORD",
|
|
|
|
|
|
"MAIL_FROM",
|
|
|
|
|
|
"MAIL_TO",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Email notification
|
|
|
|
|
|
err := CheckEnvVars(mailVars)
|
|
|
|
|
|
if err == nil {
|
2024-10-22 16:55:28 +02:00
|
|
|
|
body, err := parseTemplate(*notificationData, "email.tmpl")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
Error("Could not parse email template: %v", err)
|
|
|
|
|
|
}
|
2024-10-10 04:32:03 +02:00
|
|
|
|
err = SendEmail(fmt.Sprintf("✅ Database Backup Notification – %s", notificationData.Database), body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
Error("Could not send email: %v", err)
|
|
|
|
|
|
}
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
//Telegram notification
|
|
|
|
|
|
err = CheckEnvVars(vars)
|
|
|
|
|
|
if err == nil {
|
2024-10-22 16:55:28 +02:00
|
|
|
|
message, err := parseTemplate(*notificationData, "telegram.tmpl")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
if err != nil {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error("Could not parse telegram template: %v", err)
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 04:32:03 +02:00
|
|
|
|
err = sendMessage(message)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
Error("Could not send Telegram message: %v", err)
|
|
|
|
|
|
}
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
func NotifyError(error string) {
|
|
|
|
|
|
var vars = []string{
|
|
|
|
|
|
"TG_TOKEN",
|
|
|
|
|
|
"TG_CHAT_ID",
|
|
|
|
|
|
}
|
|
|
|
|
|
var mailVars = []string{
|
|
|
|
|
|
"MAIL_HOST",
|
|
|
|
|
|
"MAIL_PORT",
|
|
|
|
|
|
"MAIL_USERNAME",
|
|
|
|
|
|
"MAIL_PASSWORD",
|
|
|
|
|
|
"MAIL_FROM",
|
|
|
|
|
|
"MAIL_TO",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Email notification
|
|
|
|
|
|
err := CheckEnvVars(mailVars)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
body, err := parseTemplate(ErrorMessage{
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error: error,
|
|
|
|
|
|
EndTime: time.Now().Format(TimeFormat()),
|
|
|
|
|
|
BackupReference: os.Getenv("BACKUP_REFERENCE"),
|
2024-10-22 16:55:28 +02:00
|
|
|
|
}, "email-error.tmpl")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
if err != nil {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error("Could not parse error template: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
err = SendEmail(fmt.Sprintf("🔴 Urgent: Database Backup Failure Notification"), body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
Error("Could not send email: %v", err)
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//Telegram notification
|
|
|
|
|
|
err = CheckEnvVars(vars)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
message, err := parseTemplate(ErrorMessage{
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error: error,
|
|
|
|
|
|
EndTime: time.Now().Format(TimeFormat()),
|
|
|
|
|
|
BackupReference: os.Getenv("BACKUP_REFERENCE"),
|
2024-10-22 16:55:28 +02:00
|
|
|
|
}, "telegram-error.tmpl")
|
2024-10-09 22:39:44 +02:00
|
|
|
|
if err != nil {
|
2024-10-10 04:32:03 +02:00
|
|
|
|
Error("Could not parse error template: %v", err)
|
|
|
|
|
|
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 04:32:03 +02:00
|
|
|
|
err = sendMessage(message)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
Error("Could not send telegram message: %v", err)
|
|
|
|
|
|
}
|
2024-10-09 22:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getTgUrl() string {
|
|
|
|
|
|
return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TG_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
}
|