diff --git a/LibreNMS/Alert/Transport/Linemessagingapi.php b/LibreNMS/Alert/Transport/Linemessagingapi.php new file mode 100644 index 0000000000..117d1078bd --- /dev/null +++ b/LibreNMS/Alert/Transport/Linemessagingapi.php @@ -0,0 +1,80 @@ + + * @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 $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 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', + ], + ]; + } +} diff --git a/doc/Alerting/Transports.md b/doc/Alerting/Transports.md index 8fa27c9a84..a9274b5e8e 100644 --- a/doc/Alerting/Transports.md +++ b/doc/Alerting/Transports.md @@ -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:///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/)