diff --git a/classes/webhook.php b/classes/webhook.php index 711ce04..7b783fb 100644 --- a/classes/webhook.php +++ b/classes/webhook.php @@ -2,18 +2,19 @@ namespace classes; use classes\filter\gitea; +use classes\webhook_comp\discord; class webhook extends core { private $json; - private $filter; + private $comp; - public function __construct(string $json,string $filter) + public function __construct(string $json,string $comp) { parent::__construct(); if($json) { - $this->filter = $filter; + $this->comp = $comp; $this->json = json_decode($json, false); } else @@ -24,13 +25,33 @@ class webhook extends core public function getMessage() { - if($this->filter === "gitea") + if($this->comp === "gitea") { - $filter = new gitea(); - return($filter->formatMessageToReadable($this->json->text)); + $comp = new discord($this->json); + return($comp->readableMessage()); } - return $this->json->text; + if($this->comp === "grafana") + { + $comp = new grafana($this->json); + return($comp->readableMessage()); + } + + if(isset($this->json->text)) + { + return $this->json->text; + } + + if(isset($this->json->message) && isset($this->json->imageUrl)) + { + return $this->json->message . PHP_EOL . $this->json->imageUrl; + } + + if(isset($this->json->message)) + { + return $this->json->message; + } + throw new \Exception("Error not compatible query!"); } public function getEventFilter() @@ -48,4 +69,4 @@ class webhook extends core } } -?> \ No newline at end of file +?> diff --git a/classes/webhook_comp/discord.php b/classes/webhook_comp/discord.php new file mode 100644 index 0000000..6f04c07 --- /dev/null +++ b/classes/webhook_comp/discord.php @@ -0,0 +1,37 @@ +json = $json; + } + + public function readableMessage() + { + if(!empty($this->json->content)) + { + return $this->json->content; + } + + if(!empty($this->json->embeds->title)) + { + $this->readable .= $this->json->embeds->title . PHP_EOL; + } + + if(!empty($this->json->embeds->description)) + { + $this->readable .= preg_replace("/(?:__|[*#])|\[(.*?)\]\(.*?\)/"," ",$this->json->embeds->description) . PHP_EOL; + $this->readable .= $this->json->embeds->url; + } + + return $this->readable; + } +} \ No newline at end of file diff --git a/classes/webhook_comp/grafana.php b/classes/webhook_comp/grafana.php new file mode 100644 index 0000000..867b26e --- /dev/null +++ b/classes/webhook_comp/grafana.php @@ -0,0 +1,38 @@ +json = $json; + } + + public function readableMessage() + { + if(!empty($this->json->title)) + { + $this->readable .= $this->json->title; + } + + + if(!empty($this->json->message)) + { + $this->readable .= $this->json->message; + } + + if(!empty($this->json->state)) + { + $this->readable .= $this->json->state; + } + + + return $this->readable; + } +} \ No newline at end of file diff --git a/includes/app.php b/includes/app.php index 0a62482..987e5fd 100644 --- a/includes/app.php +++ b/includes/app.php @@ -77,8 +77,8 @@ class app private function handleIncomingData(): void { $post = file_get_contents("php://input"); - $filter = $this->checkIfFilterNeeded(); - $wh = new Webhook($post,$filter); + $comp = $this->checkIfCompatibilityClassNeeded(); + $wh = new Webhook($post,$comp); $xmpp = new Xmpp(); $xmpp->start(); @@ -87,13 +87,19 @@ class app $xmpp->disconnect(); } - private function checkIfFilterNeeded() + private function checkIfCompatibilityClassNeeded() { if(array_key_exists("HTTP_X_GITEA_DELIVERY",$_SERVER)) { return 'gitea'; } + + if(isset($_SERVER['User-Agent']) && $_SERVER['User-Agent'] === "Grafana") + { + return 'grafana'; + } + return false; } @@ -115,4 +121,4 @@ class app return $ip; } -} \ No newline at end of file +}