Files

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

86 lines
2.8 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 ToeiRei <[email protected]>
* @copyright 2017 ToeiRei, LibreNMS work based on the work of f0o. It's his work.
* @license GPL
*/
2020-09-21 14:54:51 +02:00
namespace LibreNMS\Alert\Transport;
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 Rocket extends Transport
{
protected string $name = 'Rocket Chat';
public function deliverAlert(array $alert_data): bool
2018-07-21 13:34:59 -06:00
{
$rocket_opts = $this->parseUserOptions($this->config['rocket-options']);
$rocket_msg = strip_tags($alert_data['msg']);
2018-07-21 13:34:59 -06:00
$data = [
'attachments' => [
0 => [
'fallback' => $rocket_msg,
'color' => self::getColorForState($alert_data['state']),
'title' => $alert_data['title'],
2018-07-21 13:34:59 -06:00
'text' => $rocket_msg,
2020-09-21 14:54:51 +02:00
],
2018-07-21 13:34:59 -06:00
],
'channel' => $rocket_opts['channel'] ?? null,
'username' => $rocket_opts['username'] ?? null,
'icon_url' => $rocket_opts['icon_url'] ?? null,
'icon_emoji' => $rocket_opts['icon_emoji'] ?? null,
2018-07-21 13:34:59 -06:00
];
$res = Http::client()->post($this->config['rocket-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(), $rocket_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' => 'rocket-url',
'descr' => 'Rocket.chat Webhook URL',
'type' => 'text',
],
[
'title' => 'Rocket.chat Options',
'name' => 'rocket-options',
'descr' => 'Rocket.chat Options',
'type' => 'textarea',
],
],
'validation' => [
'rocket-url' => 'required|url',
],
];
}
}