Discord Transprot user configurable embed fields (#15068)

This commit is contained in:
Tony Murray
2023-07-24 18:53:34 -05:00
committed by GitHub
parent f9d16238c9
commit cce3dc0746

View File

@@ -35,12 +35,9 @@ use LibreNMS\Util\Http;
class Discord extends Transport
{
public const ALERT_FIELDS_TO_DISCORD_FIELDS = [
'timestamp' => 'Timestamp',
'severity' => 'Severity',
'hostname' => 'Hostname',
public const DEFAULT_EMBEDS = 'hostname,name,timestamp,severity';
private array $embedFieldTranslations = [
'name' => 'Rule Name',
'rule' => 'Rule',
];
public function deliverAlert(array $alert_data): bool
@@ -105,15 +102,12 @@ class Discord extends Transport
{
$result = [];
foreach (self::ALERT_FIELDS_TO_DISCORD_FIELDS as $objKey => $discordKey) {
// Skip over keys that do not exist so Discord does not give us a 400.
if (! $alert_data[$objKey]) {
continue;
}
$fields = explode(',', $this->config['discord-embed-fields'] ?? self::DEFAULT_EMBEDS);
foreach ($fields as $field) {
$result[] = [
'name' => $discordKey,
'value' => $alert_data[$objKey],
'name' => $this->embedFieldTranslations[$field] ?? ucfirst($field),
'value' => $alert_data[$field] ?? 'Error: Invalid Field',
];
}
@@ -136,6 +130,13 @@ class Discord extends Transport
'descr' => 'Enter the config options (format: option=value separated by new lines)',
'type' => 'textarea',
],
[
'title' => 'Fields to embed in the alert',
'name' => 'discord-embed-fields',
'descr' => 'Comma seperated list of fields from the alert to attach to the Discord message',
'type' => 'text',
'default' => self::DEFAULT_EMBEDS,
],
],
'validation' => [
'url' => 'required|url',