chore: build
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/omnex/control-plane/api/internal/alerts"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
)
|
||||
|
||||
@@ -173,3 +174,55 @@ func (h *Handler) SetAlerts(c *gin.Context) {
|
||||
}
|
||||
c.JSON(http.StatusOK, toAlertSettingsResponse(user))
|
||||
}
|
||||
|
||||
type testChannelResult struct {
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type testAlertsResponse struct {
|
||||
Discord *testChannelResult `json:"discord,omitempty"`
|
||||
Telegram *testChannelResult `json:"telegram,omitempty"`
|
||||
}
|
||||
|
||||
const testAlertMessage = "🔔 Test Omnex — si vous recevez ce message, ce canal d'alerte est bien configuré."
|
||||
|
||||
// TestAlerts : envoie un message de test aux canaux fournis dans la requête,
|
||||
// sans les persister — permet de vérifier la config (webhook Discord valide,
|
||||
// bot Telegram démarré, chat_id correct...) avant d'enregistrer, ou de
|
||||
// vérifier des réglages déjà enregistrés sans les ressaisir.
|
||||
func (h *Handler) TestAlerts(c *gin.Context) {
|
||||
var req alertSettingsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
p := auth.PrincipalFrom(c)
|
||||
if p == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
if req.DiscordWebhookURL == "" && (req.TelegramBotToken == "" || req.TelegramChatID == "") {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "aucun canal renseigné"})
|
||||
return
|
||||
}
|
||||
|
||||
var res testAlertsResponse
|
||||
if req.DiscordWebhookURL != "" {
|
||||
n := &alerts.DiscordNotifier{WebhookURL: req.DiscordWebhookURL}
|
||||
if err := n.Notify(c.Request.Context(), testAlertMessage); err != nil {
|
||||
res.Discord = &testChannelResult{Error: err.Error()}
|
||||
} else {
|
||||
res.Discord = &testChannelResult{OK: true}
|
||||
}
|
||||
}
|
||||
if req.TelegramBotToken != "" && req.TelegramChatID != "" {
|
||||
n := &alerts.TelegramNotifier{BotToken: req.TelegramBotToken, ChatID: req.TelegramChatID}
|
||||
if err := n.Notify(c.Request.Context(), testAlertMessage); err != nil {
|
||||
res.Telegram = &testChannelResult{Error: err.Error()}
|
||||
} else {
|
||||
res.Telegram = &testChannelResult{OK: true}
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user