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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -352,6 +352,65 @@ LibreNMS database.
|
||||
| Jira Username | myjirauser |
|
||||
| Jira Password | myjirapass |
|
||||
|
||||
## LINE Messaging API
|
||||
|
||||
[LINE Messaging API Docs](https://developers.line.biz/en/docs/messaging-api/overview/)
|
||||
|
||||
Here is the step for setup a LINE bot and using it in LibreNMS.
|
||||
|
||||
1. Use your real LINE account register in [developer protal](https://developers.line.biz/).
|
||||
|
||||
1. Add a new channel, choose `Messaging API` and continue fill up the forms, note that `Channel name` cannot edit later.
|
||||
|
||||
1. Go to "Messaging API" tab of your channel, here listing some important value.
|
||||
|
||||
- `Bot basic ID` and `QR code` is your LINE bot's ID and QR code.
|
||||
- `Channel access token (long-lived)`, will use it in LibreNMS, keep it safe.
|
||||
|
||||
1. Use your real Line account add your LINE bot as a friend.
|
||||
|
||||
1. Recipient ID can be `groupID`, `userID` or `roomID`, it will be used in LibreNMS to send message to a group or a user. Use the following NodeJS program and `ngrok` for temporally https webhook to listen it.
|
||||
|
||||
[LINE-bot-RecipientFetcher](https://github.com/j796160836/LINE-bot-RecipientFetcher)
|
||||
|
||||
1. Run the program and using `ngrok` expose port to public
|
||||
|
||||
```
|
||||
$ node index.js
|
||||
$ ngrok http 3000
|
||||
```
|
||||
|
||||
1. Go to "Messaging API" tab of your channel, fill up Webhook URL to `https://<your ngrok domain>/webhook`
|
||||
|
||||
|
||||
1. If you want to let LINE bot send message to a yourself, use your real account to send a message to your LINE bot. Program will print out the `userID` in console.
|
||||
|
||||
sample value:
|
||||
|
||||
```
|
||||
{"type":"user","userId":"U527xxxxxxxxxxxxxxxxxxxxxxxxxc0ee"}
|
||||
```
|
||||
|
||||
1. If you want to let LINE bot send message to a group, do the following steps.
|
||||
|
||||
- Add your LINE bot into group
|
||||
- Use your real account to send a message to group
|
||||
|
||||
Program will print out the `groupID` in console, it will be Recipient ID, keep it safe.
|
||||
|
||||
sample value:
|
||||
|
||||
```
|
||||
{"type":"group","groupId":"Ce51xxxxxxxxxxxxxxxxxxxxxxxxxx6ef","userId":"U527xxxxxxxxxxxxxxxxxxxxxxxxxc0ee"} ```
|
||||
```
|
||||
|
||||
**Example:**
|
||||
|
||||
| Config | Example |
|
||||
| ------ | ------- |
|
||||
| Access token | fhJ9vH2fsxxxxxxxxxxxxxxxxxxxxlFU= |
|
||||
| Recipient (groupID, userID or roomID) | Ce51xxxxxxxxxxxxxxxxxxxxxxxxxx6ef |
|
||||
|
||||
## LINE Notify
|
||||
|
||||
[LINE Notify](https://notify-bot.line.me/)
|
||||
|
Reference in New Issue
Block a user