mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Discord Transprot user configurable embed fields (#15068)
This commit is contained in:
@@ -35,12 +35,9 @@ use LibreNMS\Util\Http;
|
|||||||
|
|
||||||
class Discord extends Transport
|
class Discord extends Transport
|
||||||
{
|
{
|
||||||
public const ALERT_FIELDS_TO_DISCORD_FIELDS = [
|
public const DEFAULT_EMBEDS = 'hostname,name,timestamp,severity';
|
||||||
'timestamp' => 'Timestamp',
|
private array $embedFieldTranslations = [
|
||||||
'severity' => 'Severity',
|
|
||||||
'hostname' => 'Hostname',
|
|
||||||
'name' => 'Rule Name',
|
'name' => 'Rule Name',
|
||||||
'rule' => 'Rule',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
public function deliverAlert(array $alert_data): bool
|
public function deliverAlert(array $alert_data): bool
|
||||||
@@ -105,15 +102,12 @@ class Discord extends Transport
|
|||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach (self::ALERT_FIELDS_TO_DISCORD_FIELDS as $objKey => $discordKey) {
|
$fields = explode(',', $this->config['discord-embed-fields'] ?? self::DEFAULT_EMBEDS);
|
||||||
// Skip over keys that do not exist so Discord does not give us a 400.
|
|
||||||
if (! $alert_data[$objKey]) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
foreach ($fields as $field) {
|
||||||
$result[] = [
|
$result[] = [
|
||||||
'name' => $discordKey,
|
'name' => $this->embedFieldTranslations[$field] ?? ucfirst($field),
|
||||||
'value' => $alert_data[$objKey],
|
'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)',
|
'descr' => 'Enter the config options (format: option=value separated by new lines)',
|
||||||
'type' => 'textarea',
|
'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' => [
|
'validation' => [
|
||||||
'url' => 'required|url',
|
'url' => 'required|url',
|
||||||
|
Reference in New Issue
Block a user