Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.5 KiB
PHP
Raw Permalink Normal View History

<?php
/* Copyright (C) 2014 Daniel Preussker <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
/**
* API Transport
2021-09-10 20:09:53 +02:00
*
* @author f0o <[email protected]>
* @copyright 2014 f0o, LibreNMS
* @license GPL
*/
2020-09-21 14:54:51 +02:00
namespace LibreNMS\Alert\Transport;
use Illuminate\Support\Str;
2018-07-21 13:34:59 -06:00
use LibreNMS\Alert\Transport;
use LibreNMS\Exceptions\AlertTransportDeliveryException;
use LibreNMS\Util\Http;
2018-07-21 13:34:59 -06:00
class Slack extends Transport
{
public function deliverAlert(array $alert_data): bool
2018-07-21 13:34:59 -06:00
{
$slack_opts = $this->parseUserOptions($this->config['slack-options'] ?? '');
$icon = $this->config['slack-icon_emoji'] ?? $slack_opts['icon_emoji'] ?? null;
$slack_msg = html_entity_decode(strip_tags($alert_data['msg'] ?? ''), ENT_QUOTES);
2018-07-21 13:34:59 -06:00
$data = [
'attachments' => [
0 => [
'fallback' => $slack_msg,
'color' => self::getColorForState($alert_data['state']),
'title' => $alert_data['title'] ?? null,
2018-07-21 13:34:59 -06:00
'text' => $slack_msg,
'mrkdwn_in' => ['text', 'fallback'],
'author_name' => $this->config['slack-author'] ?? $slack_opts['author'] ?? null,
2018-07-21 13:34:59 -06:00
],
],
'channel' => $this->config['slack-channel'] ?? $slack_opts['channel'] ?? null,
'icon_emoji' => $icon ? Str::finish(Str::start($icon, ':'), ':') : null,
2018-07-21 13:34:59 -06:00
];
$res = Http::client()->post($this->config['slack-url'] ?? '', $data);
2020-09-21 14:54:51 +02:00
if ($res->successful()) {
return true;
}
2020-09-21 14:54:51 +02:00
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $slack_msg, $data);
}
2018-07-21 13:34:59 -06:00
public static function configTemplate(): array
2018-07-21 13:34:59 -06:00
{
return [
'config' => [
[
'title' => 'Webhook URL',
'name' => 'slack-url',
'descr' => 'Slack Webhook URL',
'type' => 'text',
],
[
'title' => 'Channel',
'name' => 'slack-channel',
'descr' => 'Channel to post to',
'type' => 'text',
],
[
'title' => 'Author Name',
'name' => 'slack-author',
'descr' => 'Name of author',
'type' => 'text',
'default' => 'LibreNMS',
],
[
'title' => 'Icon',
2023-07-07 13:36:34 -04:00
'name' => 'slack-icon_emoji',
'descr' => 'Name of emoji for icon',
'type' => 'text',
2018-07-21 13:34:59 -06:00
],
],
'validation' => [
'slack-url' => 'required|url',
'slack-channel' => 'string',
2023-07-07 13:36:34 -04:00
'slack-author' => 'string',
'slack-icon_emoji' => 'string',
2018-07-21 13:34:59 -06:00
],
];
}
}