Files
sflow-abuse/src/alert_channel.go
2023-10-27 18:05:58 +02:00

64 lines
1.4 KiB
Go

package src
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// WebhookURL is the URL of the Discord webhook.
const WebhookURL = "https://discord.com/api/webhooks/1116824798421594233/ARw2KQvPPIt2wLlw4Ssp98o0VWkjr-FdZ2kpFono8zu5UC-N1Uyysy73wbL_DvYJutya"
// AlertData represents the data to be sent in the alert.
type AlertData struct {
Embeds []Embed `json:"embeds"`
}
// Embed represents an embedded message in the alert.
type Embed struct {
Title string `json:"title"`
Description string `json:"description"`
Color int `json:"color"`
}
// SendAlert sends an alert to the specified Discord webhook.
func SendAlert(alert string) error {
url := WebhookURL
alertData := AlertData{
Embeds: []Embed{
{
Title: "IP Abuse alert",
Description: alert,
Color: 15258703, // Color in decimal (corresponding to a certain color)
},
},
}
jsonData, err := json.Marshal(alertData)
if err != nil {
return err
}
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
fmt.Printf("non-2xx status code: %d\n", resp.StatusCode)
return fmt.Errorf("non-2xx status code")
}
return nil
}