mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Implement LINE Messaging API (#14867)
* implement LINE Messaging api * apply code clean up * Update Linemessagingapi.php * fix variable type * Match branding * Remove extra proxy --------- Co-authored-by: Neil Lathwood <gh+n@laf.io> Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
80
LibreNMS/Alert/Transport/Linemessagingapi.php
Normal file
80
LibreNMS/Alert/Transport/Linemessagingapi.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Line Messaging API Transport
|
||||
*
|
||||
* @author Johnny Sung <https://github.com/j796160836>
|
||||
* @copyright 2023 Johnny Sung
|
||||
* @license GPL
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Alert\Transport;
|
||||
|
||||
use LibreNMS\Alert\Transport;
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Exceptions\AlertTransportDeliveryException;
|
||||
use LibreNMS\Util\Http;
|
||||
|
||||
class LineMessagingAPI extends Transport
|
||||
{
|
||||
protected string $name = 'LINE Messaging API';
|
||||
|
||||
/**
|
||||
* Deliver Alert
|
||||
*
|
||||
* @param array<string, string> $alert_data Alert data
|
||||
* @return bool True if message sent successfully
|
||||
*/
|
||||
public function deliverAlert($alert_data): bool
|
||||
{
|
||||
$apiURL = 'https://api.line.me/v2/bot/message/push';
|
||||
$data = [
|
||||
'to' => $this->config['line-messaging-to'],
|
||||
'messages' => [
|
||||
[
|
||||
'type' => 'text',
|
||||
'text' => $alert_data['msg'],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$res = Http::client()
|
||||
->withToken($this->config['line-messaging-token'])
|
||||
->asForm()
|
||||
->post($apiURL, $data);
|
||||
|
||||
if ($res->successful()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $alert_data['msg'], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config template
|
||||
*
|
||||
* @return array<string, mixed> config template
|
||||
*/
|
||||
public static function configTemplate(): array
|
||||
{
|
||||
return [
|
||||
'config' => [
|
||||
[
|
||||
'title' => 'Access token',
|
||||
'name' => 'line-messaging-token',
|
||||
'descr' => 'LINE Channel access token',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'title' => 'Recipient (groupID, userID or roomID)',
|
||||
'name' => 'line-messaging-to',
|
||||
'descr' => 'The ID of the target recipient. Use a userId, groupId or roomID.',
|
||||
'type' => 'text',
|
||||
],
|
||||
],
|
||||
'validation' => [
|
||||
'line-messaging-token' => 'required|string',
|
||||
'line-messaging-to' => 'required|string',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user