Add more variables for webhook json (#1016)

* Add more variables for webhook json

To allow status specific icons and a special Text defined in the json, not only provide #message as variable, but also #server_ip, #server_label, #server_last_offline_duration and #status.

* Add #server_error as additional variable for webhooks
This commit is contained in:
Michael 2021-04-15 02:00:06 +02:00 committed by GitHub
parent e5b76ca49e
commit 600f63097e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 15 deletions

View File

@ -1075,23 +1075,22 @@ namespace {
{ {
protected $url; protected $url;
protected $json; protected $json;
protected $message;
/** /**
* Send Webhook * Send Webhook
* *
* @return bool|string * @return bool|string
* @var string $message * @var array $replacements an array of the replacements
* *
*/ */
public function sendWebhook($message) public function sendWebhook($replacements)
{ {
$error = ""; $error = "";
$success = 1; $success = 1;
$this->setMessage($message); $replacements['#message'] = $this->stripTagsFromMessage($replacements['#message']);
$jsonMessage = strtr($this->json, array('#message' => $this->message)); $jsonMessage = strtr($this->json, $replacements);
$curl = curl_init($this->url); $curl = curl_init($this->url);
curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POST, 1);
@ -1167,7 +1166,7 @@ namespace {
* @var string $message * @var string $message
* *
*/ */
public function setMessage($message) public function stripTagsFromMessage($message)
{ {
$message = str_replace("<ul>", "", $message); $message = str_replace("<ul>", "", $message);
$message = str_replace("</ul>", "\n", $message); $message = str_replace("</ul>", "\n", $message);
@ -1178,7 +1177,7 @@ namespace {
$message = str_replace("<b>", "", $message); $message = str_replace("<b>", "", $message);
$message = str_replace("<b/>", "", $message); $message = str_replace("<b/>", "", $message);
$message = strip_tags($message); $message = strip_tags($message);
$this->message = (string)$message; return (string)$message;
} }
} }
} }

View File

@ -134,12 +134,11 @@ $sm_lang = array(
'jabber_label' => 'Jabber', 'jabber_label' => 'Jabber',
'jabber_description' => 'You Jabber account', 'jabber_description' => 'You Jabber account',
'webhook' => 'Webhook', 'webhook' => 'Webhook',
'webhook_description' => 'Send a json webhook to a certain endpoint. <br/> The json can be customized, e.g. { 'webhook_description' => 'Send a json webhook to a certain endpoint.',
"text":"servermon: #message"}',
'webhook_url' => 'Webhook Url', 'webhook_url' => 'Webhook Url',
'webhook_url_description' => 'Webhook public endpoint url, should start with https://.', 'webhook_url_description' => 'Webhook public endpoint url, should start with https://.',
'webhook_json' => 'Webhook JSON', 'webhook_json' => 'Webhook JSON',
'webhook_json_description' => 'Define a custom json, use #message as message variable.', 'webhook_json_description' => 'Define a custom json, e.g. {"username": "servermon: #server_label" "text": "#server_ip is **#status**"}.<br>Available variables: #message, #server_error, #server_ip, #server_label, #server_last_offline_duration and #status',
'delete_title' => 'Delete User', 'delete_title' => 'Delete User',
'delete_message' => 'Are you sure you want to delete user \'%1\'?', 'delete_message' => 'Are you sure you want to delete user \'%1\'?',
'deleted' => 'User deleted.', 'deleted' => 'User deleted.',
@ -320,7 +319,7 @@ $sm_lang = array(
'webhook_url' => 'Webhook Url', 'webhook_url' => 'Webhook Url',
'webhook_url_description' => 'Url to webhook endpoint', 'webhook_url_description' => 'Url to webhook endpoint',
'webhook_json' => 'Webhook Json', 'webhook_json' => 'Webhook Json',
'webhook_json_description' => 'Customized Json, use #message as message variable.', 'webhook_json_description' => 'Define a custom json. Available variables: #message, #server_error, #server_ip, #server_label, #server_last_offline_duration and #status',
'pushover_status' => 'Allow sending Pushover messages', 'pushover_status' => 'Allow sending Pushover messages',
'pushover_description' => 'Pushover is a service that makes it easy to get real-time notifications. See <a href="https://pushover.net/" target="_blank">their website</a> for more info.', 'pushover_description' => 'Pushover is a service that makes it easy to get real-time notifications. See <a href="https://pushover.net/" target="_blank">their website</a> for more info.',
'pushover_clone_app' => 'Click here to create your Pushover app', 'pushover_clone_app' => 'Click here to create your Pushover app',

View File

@ -316,7 +316,7 @@ class StatusNotifier
} }
return; return;
} }
$this->combiNotification['notifications'][$method][$status][$this->server_id] = $this->combiNotification['notifications'][$method][$status][$this->server_id] =
psm_parse_msg($this->status_new, $method . '_message', $this->server, true); psm_parse_msg($this->status_new, $method . '_message', $this->server, true);
} }
@ -632,7 +632,14 @@ class StatusNotifier
} }
$webhook->setUrl($user['webhook_url']); $webhook->setUrl($user['webhook_url']);
$webhook->setJson($user['webhook_json']); $webhook->setJson($user['webhook_json']);
$webhook->sendWebhook($message); $webhook->sendWebhook([
'#message' => $message,
'#server_ip' => $this->server['ip'],
'#server_label' => $this->server['label'],
'#server_error' => $this->server['error'],
'#server_last_offline_duration' => $this->status_new ? $this->server['last_offline_duration'] : '',
'#status' => $this->status_new ? 'online' : 'offline'
]);
} }
} }
/** /**
@ -699,12 +706,12 @@ class StatusNotifier
if ((bool)psm_get_conf('telegram_add_url')) $message .= '<br>'.PSM_BASE_URL; if ((bool)psm_get_conf('telegram_add_url')) $message .= '<br>'.PSM_BASE_URL;
$telegram = psm_build_telegram(); $telegram = psm_build_telegram();
$telegram->setMessage($message); $telegram->setMessage($message);
// Log // Log
if (psm_get_conf('log_telegram')) { if (psm_get_conf('log_telegram')) {
$log_id = psm_add_log($this->server_id, 'telegram', $message); $log_id = psm_add_log($this->server_id, 'telegram', $message);
} }
foreach ($users as $user) { foreach ($users as $user) {
// Log // Log
if (!empty($log_id)) { if (!empty($log_id)) {