2019-09-05 12:30:53 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* LINE Notify Transport
|
|
|
|
*/
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2019-09-05 12:30:53 +08:00
|
|
|
namespace LibreNMS\Alert\Transport;
|
|
|
|
|
|
|
|
use LibreNMS\Alert\Transport;
|
2023-05-23 09:25:17 -05:00
|
|
|
use LibreNMS\Exceptions\AlertTransportDeliveryException;
|
|
|
|
use LibreNMS\Util\Http;
|
2019-09-05 12:30:53 +08:00
|
|
|
|
|
|
|
class Linenotify extends Transport
|
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
protected string $name = 'LINE Notify';
|
2021-10-06 07:29:47 -05:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
public function deliverAlert(array $alert_data): bool
|
2019-09-05 12:30:53 +08:00
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
// TODO possible to attach graph images
|
|
|
|
$lineUrl = 'https://notify-api.line.me/api/notify';
|
|
|
|
$lineFields = ['message' => $alert_data['msg']];
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
$res = Http::client()
|
|
|
|
->withToken($this->config['line-notify-access-token'])
|
|
|
|
->asForm()
|
|
|
|
->post($lineUrl, $lineFields);
|
2019-09-05 12:30:53 +08:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
if ($res->successful()) {
|
|
|
|
return true;
|
2019-09-05 12:30:53 +08: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(), $alert_data['msg'], $lineFields);
|
2019-09-05 12:30:53 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
public static function configTemplate(): array
|
2019-09-05 12:30:53 +08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'config' => [
|
|
|
|
[
|
|
|
|
'title' => 'Token',
|
|
|
|
'name' => 'line-notify-access-token',
|
|
|
|
'descr' => 'LINE Notify Token',
|
2023-09-01 20:07:39 +00:00
|
|
|
'type' => 'password',
|
2019-09-05 12:30:53 +08:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'validation' => [
|
|
|
|
'line-notify-access-token' => 'required|string',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|