From 9f208360b35b19cd31bfd1486ca073b51de45964 Mon Sep 17 00:00:00 2001 From: pbaldovi Date: Sat, 6 Mar 2021 17:48:20 -0300 Subject: [PATCH] Add Google Chat Transport (#12558) * Add Google Chat Transport * Bug. False response on succesfully sended messages * Style CI patch --- LibreNMS/Alert/Transport/Googlechat.php | 91 +++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 LibreNMS/Alert/Transport/Googlechat.php diff --git a/LibreNMS/Alert/Transport/Googlechat.php b/LibreNMS/Alert/Transport/Googlechat.php new file mode 100755 index 0000000000..2e0201135a --- /dev/null +++ b/LibreNMS/Alert/Transport/Googlechat.php @@ -0,0 +1,91 @@ +. + * + * @link http://librenms.org + * @copyright 2021 Pablo Baldovi + * @author Pablo Baldovi + */ + +namespace LibreNMS\Alert\Transport; + +use LibreNMS\Alert\Transport; +use Log; + +class Googlechat extends Transport +{ + public function deliverAlert($obj, $opts) + { + $googlechat_conf['webhookurl'] = $this->config['googlechat-webhook']; + + return $this->contactGooglechat($obj, $googlechat_conf); + } + + public static function contactGooglechat($obj, $data) + { + $payload = '{"text": "' . $obj['msg'] . '"}'; + + Log::debug($payload); + + // Create a new cURL resource + $ch = curl_init($data['webhookurl']); + + // Attach encoded JSON string to the POST fields + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + + // Set the content type to application/json + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); + + // Return response instead of outputting + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // Execute the POST request + $result = curl_exec($ch); + + // Close cURL resource + + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + Log::debug($code); + + if ($code != 200) { + Log::error('Google Chat Transport Error'); + Log::error($result); + + return 'HTTP Status code ' . $code; + } + + return true; + } + + public static function configTemplate() + { + return [ + 'config' => [ + [ + 'title' => 'Webhook URL', + 'name' => 'googlechat-webhook', + 'descr' => 'Google Chat Room Webhook', + 'type' => 'text', + ], + ], + 'validation' => [ + 'googlechat-webhook' => 'required|string', + ], + ]; + } +}