Files

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

116 lines
3.9 KiB
PHP
Raw Permalink Normal View History

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;
use App\View\SimpleTemplate;
2020-08-20 12:04:17 +02:00
use LibreNMS\Alert\Transport;
2021-10-06 07:29:47 -05:00
use LibreNMS\Util\Proxy;
2020-08-20 12:04:17 +02:00
class Matrix extends Transport
{
public function deliverAlert($obj, $opts)
{
$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-08-20 12:04:17 +02:00
return $this->contactMatrix($obj, $server, $room, $authtoken, $message);
}
private function contactMatrix($obj, $server, $room, $authtoken, $message)
{
$request_opts = [];
$request_heads = [];
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
$request_heads['Authorization'] = "Bearer $authtoken";
$request_heads['Content-Type'] = 'application/json';
$request_heads['Accept'] = 'application/json';
$message = SimpleTemplate::parse($message, $obj);
2020-08-20 12:04:17 +02:00
$body = ['body'=>strip_tags($message), 'formatted_body'=>"$message", 'msgtype'=>'m.text', 'format'=>'org.matrix.custom.html'];
2020-08-20 12:04:17 +02:00
$client = new \GuzzleHttp\Client();
2021-10-06 07:29:47 -05:00
$request_opts['proxy'] = Proxy::forGuzzle();
2020-08-20 12:04:17 +02:00
$request_opts['headers'] = $request_heads;
$request_opts['body'] = json_encode($body);
$res = $client->request('PUT', $host, $request_opts);
$code = $res->getStatusCode();
if ($code != 200) {
var_dump("Matrix '$host' returned Error");
var_dump('Params:');
var_dump('Response headers:');
var_dump($res->getHeaders());
var_dump('Return: ' . $res->getReasonPhrase());
2020-09-21 14:54:51 +02:00
2020-08-20 12:04:17 +02:00
return 'HTTP Status code ' . $code;
}
2020-09-21 14:54:51 +02:00
2020-08-20 12:04:17 +02:00
return true;
}
public static function configTemplate()
{
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',
'type' => 'text',
],
[
'title' => 'Message',
'name' => 'matrix-message',
'descr' => 'Enter the message',
'type' => 'text',
2020-08-20 12:04:17 +02:00
],
],
'validation' => [
'matrix-server' => 'required',
'matrix-room' => 'required',
'matrix-authtoken' => 'required',
],
];
}
}