72 lines
2.0 KiB
Go
Raw Normal View History

2024-05-08 01:56:48 +02:00
package notification
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
2024-05-10 14:23:54 +02:00
"slices"
2024-05-09 15:27:45 +02:00
"strconv"
2024-05-10 14:23:54 +02:00
"strings"
2024-05-08 01:56:48 +02:00
"wazuh-notify/types"
)
func SendDiscord(params types.Params) {
2024-05-10 14:23:54 +02:00
var embedDescription string
2024-05-27 11:29:47 +02:00
if slices.Contains(strings.Split(params.FullAlert, ","), "discord") {
2024-05-24 13:17:08 +02:00
fullAlert, _ := json.MarshalIndent(params.WazuhMessage, "", " ")
fullAlertString := strings.ReplaceAll(string(fullAlert), `"`, "")
fullAlertString = strings.ReplaceAll(fullAlertString, "{", "")
fullAlertString = strings.ReplaceAll(fullAlertString, "}", "")
fullAlertString = strings.ReplaceAll(fullAlertString, "[", "")
fullAlertString = strings.ReplaceAll(fullAlertString, "]", "")
fullAlertString = strings.ReplaceAll(fullAlertString, " ,", "")
2024-05-10 14:23:54 +02:00
embedDescription = "\n\n ```" +
2024-05-24 13:17:08 +02:00
fullAlertString +
2024-05-10 14:23:54 +02:00
"```\n\n" +
"Priority: " + strconv.Itoa(params.Priority) + "\n" +
"Tags: " + params.Tags + "\n\n" +
params.Click
} else {
embedDescription = "\n\n" +
"**Agent:** " + params.WazuhMessage.Parameters.Alert.Agent.Name + "\n" +
"**Event id:** " + params.WazuhMessage.Parameters.Alert.Rule.ID + "\n" +
2024-05-13 14:44:32 +02:00
"**Rule:** " + params.WazuhMessage.Parameters.Alert.Rule.Description + "\n" +
"**Description: **" + params.WazuhMessage.Parameters.Alert.FullLog + "\n" +
2024-05-10 14:23:54 +02:00
"**Threat level:** " + strconv.Itoa(params.WazuhMessage.Parameters.Alert.Rule.Level) + "\n" +
"**Times fired:** " + strconv.Itoa(params.WazuhMessage.Parameters.Alert.Rule.Firedtimes) +
"\n\n" +
"Priority: " + strconv.Itoa(params.Priority) + "\n" +
"Tags: " + params.Tags + "\n\n" +
params.Click
}
2024-05-08 01:56:48 +02:00
message := types.Message{
Username: params.Sender,
2024-05-13 16:03:00 +02:00
Content: params.Mention,
2024-05-08 01:56:48 +02:00
Embeds: []types.Embed{
{
2024-05-09 15:27:45 +02:00
Title: params.Sender,
2024-05-08 01:56:48 +02:00
Description: embedDescription,
2024-05-13 16:03:00 +02:00
Color: params.Color,
2024-05-08 01:56:48 +02:00
},
},
}
payload := new(bytes.Buffer)
err := json.NewEncoder(payload).Encode(message)
if err != nil {
return
}
2024-05-09 19:03:34 +02:00
_, err = http.Post(os.Getenv("DISCORD_URL"), "application/json", payload)
2024-05-08 01:56:48 +02:00
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
}