- Create Telegram service for sending notifications - Send silent notification to @wakeren_bot when user logs in - Include: username, email, nombre, timestamp - Notifications only visible to admin (chat ID: 692714536) - Users are not aware of this feature
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package services
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
telegramBotToken = "8551922819:AAHIXNbavzcI90eEIGVx1NIisYHecfcBYtU"
|
|
telegramChatID = "692714536"
|
|
telegramAPI = "https://api.telegram.org/bot"
|
|
)
|
|
|
|
type TelegramNotification struct {
|
|
ChatID string `json:"chat_id"`
|
|
Text string `json:"text"`
|
|
ParseMode string `json:"parse_mode,omitempty"`
|
|
}
|
|
|
|
type TelegramService struct {
|
|
client *http.Client
|
|
}
|
|
|
|
func NewTelegramService() *TelegramService {
|
|
return &TelegramService{
|
|
client: &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (s *TelegramService) SendLoginNotification(username, email, nombre string, timestamp time.Time) error {
|
|
message := fmt.Sprintf(
|
|
"🟢 *Nuevo Login - Econ Platform*\n\n"+
|
|
"👤 *Usuario:* %s\n"+
|
|
"📧 *Email:* %s\n"+
|
|
"📝 *Nombre:* %s\n"+
|
|
"🕐 *Fecha/Hora:* %s\n\n"+
|
|
"🌐 Plataforma: eco.cbcren.online",
|
|
username,
|
|
email,
|
|
nombre,
|
|
timestamp.Format("02/01/2006 15:04:05"),
|
|
)
|
|
|
|
return s.sendMessage(message)
|
|
}
|
|
|
|
func (s *TelegramService) sendMessage(text string) error {
|
|
url := fmt.Sprintf("%s%s/sendMessage", telegramAPI, telegramBotToken)
|
|
|
|
payload := TelegramNotification{
|
|
ChatID: telegramChatID,
|
|
Text: text,
|
|
ParseMode: "Markdown",
|
|
}
|
|
|
|
jsonData, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("error marshaling telegram payload: %w", err)
|
|
}
|
|
|
|
resp, err := s.client.Post(url, "application/json", bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return fmt.Errorf("error sending telegram message: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("telegram API returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// SendErrorNotification envía notificación de errores críticos (opcional)
|
|
func (s *TelegramService) SendErrorNotification(errorMsg string) error {
|
|
message := fmt.Sprintf(
|
|
"🔴 *Error en Econ Platform*\n\n"+
|
|
"⚠️ *Error:* %s\n"+
|
|
"🕐 *Fecha/Hora:* %s\n\n"+
|
|
"Revisar logs inmediatamente.",
|
|
errorMsg,
|
|
time.Now().Format("02/01/2006 15:04:05"),
|
|
)
|
|
|
|
return s.sendMessage(message)
|
|
}
|