2020-08-20 12:04:17 +02:00
|
|
|
<?php
|
|
|
|
/* Copyright (C) 2020 Raphael Dannecker <rdannecker@gmail.com>
|
|
|
|
* 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/>. */
|
2020-08-20 12:04:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Matrix Transport
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2020-08-20 12:04:17 +02:00
|
|
|
* @author Raphael Dannecker (github.com/raphael247)
|
|
|
|
* @copyright 2020 , LibreNMS
|
|
|
|
* @license GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Alert\Transport;
|
|
|
|
|
2021-10-29 22:12:20 -05:00
|
|
|
use App\View\SimpleTemplate;
|
2020-08-20 12:04:17 +02:00
|
|
|
use LibreNMS\Alert\Transport;
|
2023-05-23 09:25:17 -05:00
|
|
|
use LibreNMS\Exceptions\AlertTransportDeliveryException;
|
|
|
|
use LibreNMS\Util\Http;
|
2020-08-20 12:04:17 +02:00
|
|
|
|
|
|
|
class Matrix extends Transport
|
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
public function deliverAlert(array $alert_data): bool
|
2020-08-20 12:04:17 +02:00
|
|
|
{
|
|
|
|
$server = $this->config['matrix-server'];
|
|
|
|
$room = $this->config['matrix-room'];
|
|
|
|
$authtoken = $this->config['matrix-authtoken'];
|
|
|
|
$message = $this->config['matrix-message'];
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2020-09-04 09:38:19 -05:00
|
|
|
$txnid = rand(1111, 9999) . time();
|
2020-08-20 12:04:17 +02:00
|
|
|
|
|
|
|
$server = preg_replace('/\/$/', '', $server);
|
2020-09-04 09:38:19 -05:00
|
|
|
$host = $server . '/_matrix/client/r0/rooms/' . urlencode($room) . '/send/m.room.message/' . $txnid;
|
2020-08-20 12:04:17 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
$message = SimpleTemplate::parse($message, $alert_data);
|
2020-08-20 12:04:17 +02:00
|
|
|
|
2024-09-12 16:13:21 +01:00
|
|
|
$body = ['body' => strip_tags($message), 'formatted_body' => "$message", 'msgtype' => 'm.notice', 'format' => 'org.matrix.custom.html'];
|
2020-08-20 12:04:17 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
$res = Http::client()
|
|
|
|
->withToken($authtoken)
|
|
|
|
->acceptJson()
|
|
|
|
->put($host, $body);
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
if ($res->successful()) {
|
|
|
|
return true;
|
2020-08-20 12:04:17 +02:00
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $message, $body);
|
2020-08-20 12:04:17 +02:00
|
|
|
}
|
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
public static function configTemplate(): array
|
2020-08-20 12:04:17 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'config' => [
|
|
|
|
[
|
|
|
|
'title' => 'Matrix-Server URL',
|
|
|
|
'name' => 'matrix-server',
|
|
|
|
'descr' => 'Matrix server URL up to the matrix api-part (for example: https://matrix.example.com/)',
|
|
|
|
'type' => 'text',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Room',
|
|
|
|
'name' => 'matrix-room',
|
|
|
|
'descr' => 'Enter the room',
|
|
|
|
'type' => 'text',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Auth_token',
|
|
|
|
'name' => 'matrix-authtoken',
|
|
|
|
'descr' => 'Enter the auth_token',
|
2023-09-01 20:07:39 +00:00
|
|
|
'type' => 'password',
|
2020-08-20 12:04:17 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Message',
|
|
|
|
'name' => 'matrix-message',
|
|
|
|
'descr' => 'Enter the message',
|
2022-07-21 06:05:38 +02:00
|
|
|
'type' => 'text',
|
2020-08-20 12:04:17 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'validation' => [
|
|
|
|
'matrix-server' => 'required',
|
|
|
|
'matrix-room' => 'required',
|
|
|
|
'matrix-authtoken' => 'required',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|