fresh/src/Hooks/SlackHook.php

56 lines
1.7 KiB
PHP

<?php
namespace NoccyLabs\FreshDocker\Hooks;
use GuzzleHttp\Client;
use RuntimeException;
class SlackHook
{
private array $options = [];
public function __construct(array $options = [])
{
$this->options = $options;
}
public function sendMessage(string $text, array $options)
{
$mergedOptions = array_merge($this->options, $options);
$url = $mergedOptions['url'] ?? throw new RuntimeException("Empty hook url");
$body = $this->makeBody($text, $mergedOptions);
$client = new Client();
$client->post($url, [
'json' => $body
]);
}
private function makeBody(string $text, array $options)
{
/*
{
"channel": "town-square",
"username": "test-automation",
"icon_url": "https://mattermost.org/wp-content/uploads/2016/04/icon.png",
"text": "#### Test results for July 27th, 2017\n<!channel> please review failed tests.\n
| Component | Tests Run | Tests Failed |
|:-----------|:-----------:|:-----------------------------------------------|
| Server | 948 | :white_check_mark: 0 |
| Web Client | 123 | :warning: 2 [(see details)](http://linktologs) |
| iOS Client | 78 | :warning: 3 [(see details)](http://linktologs) |
"
}
*/
$body = [];
if (array_key_exists('channel', $options)) $body['channel'] = $options['channel'];
if (array_key_exists('username', $options)) $body['username'] = $options['username'];
if (array_key_exists('icon_url', $options)) $body['icon_url'] = $options['icon_url'];
$body['text'] = $text;
return $body;
}
}