. * * @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', ], ]; } }