2022-10-17 21:25:03 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Monolog package.
|
|
|
|
*
|
|
|
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Monolog\Handler;
|
|
|
|
|
|
|
|
use Monolog\Formatter\FormatterInterface;
|
2023-07-02 23:57:24 +02:00
|
|
|
use Monolog\Logger;
|
2022-10-17 21:25:03 +02:00
|
|
|
use Monolog\Utils;
|
|
|
|
use Monolog\Handler\Slack\SlackRecord;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends notifications through Slack Webhooks
|
|
|
|
*
|
|
|
|
* @author Haralan Dobrev <hkdobrev@gmail.com>
|
|
|
|
* @see https://api.slack.com/incoming-webhooks
|
|
|
|
*/
|
|
|
|
class SlackWebhookHandler extends AbstractProcessingHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Slack Webhook token
|
2023-07-02 23:57:24 +02:00
|
|
|
* @var string
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
private $webhookUrl;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the SlackRecord util class preparing data for Slack API.
|
2023-07-02 23:57:24 +02:00
|
|
|
* @var SlackRecord
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
private $slackRecord;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $webhookUrl Slack Webhook URL
|
|
|
|
* @param string|null $channel Slack channel (encoded ID or name)
|
|
|
|
* @param string|null $username Name of a bot
|
|
|
|
* @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
|
|
|
|
* @param string|null $iconEmoji The emoji name to use (or null)
|
|
|
|
* @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style
|
|
|
|
* @param bool $includeContextAndExtra Whether the attachment should include context and extra data
|
|
|
|
* @param string[] $excludeFields Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
string $webhookUrl,
|
|
|
|
?string $channel = null,
|
|
|
|
?string $username = null,
|
|
|
|
bool $useAttachment = true,
|
|
|
|
?string $iconEmoji = null,
|
|
|
|
bool $useShortAttachment = false,
|
|
|
|
bool $includeContextAndExtra = false,
|
2023-07-02 23:57:24 +02:00
|
|
|
$level = Logger::CRITICAL,
|
2022-10-17 21:25:03 +02:00
|
|
|
bool $bubble = true,
|
2023-07-02 23:57:24 +02:00
|
|
|
array $excludeFields = array()
|
2022-10-17 21:25:03 +02:00
|
|
|
) {
|
|
|
|
if (!extension_loaded('curl')) {
|
|
|
|
throw new MissingExtensionException('The curl extension is needed to use the SlackWebhookHandler');
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($level, $bubble);
|
|
|
|
|
|
|
|
$this->webhookUrl = $webhookUrl;
|
|
|
|
|
|
|
|
$this->slackRecord = new SlackRecord(
|
|
|
|
$channel,
|
|
|
|
$username,
|
|
|
|
$useAttachment,
|
|
|
|
$iconEmoji,
|
|
|
|
$useShortAttachment,
|
|
|
|
$includeContextAndExtra,
|
|
|
|
$excludeFields
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSlackRecord(): SlackRecord
|
|
|
|
{
|
|
|
|
return $this->slackRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWebhookUrl(): string
|
|
|
|
{
|
|
|
|
return $this->webhookUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
protected function write(array $record): void
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
$postData = $this->slackRecord->getSlackData($record);
|
|
|
|
$postString = Utils::jsonEncode($postData);
|
|
|
|
|
|
|
|
$ch = curl_init();
|
2023-07-02 23:57:24 +02:00
|
|
|
$options = array(
|
2022-10-17 21:25:03 +02:00
|
|
|
CURLOPT_URL => $this->webhookUrl,
|
|
|
|
CURLOPT_POST => true,
|
|
|
|
CURLOPT_RETURNTRANSFER => true,
|
2023-07-02 23:57:24 +02:00
|
|
|
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
|
2022-10-17 21:25:03 +02:00
|
|
|
CURLOPT_POSTFIELDS => $postString,
|
2023-07-02 23:57:24 +02:00
|
|
|
);
|
2022-10-17 21:25:03 +02:00
|
|
|
if (defined('CURLOPT_SAFE_UPLOAD')) {
|
|
|
|
$options[CURLOPT_SAFE_UPLOAD] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt_array($ch, $options);
|
|
|
|
|
|
|
|
Curl\Util::execute($ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFormatter(FormatterInterface $formatter): HandlerInterface
|
|
|
|
{
|
|
|
|
parent::setFormatter($formatter);
|
|
|
|
$this->slackRecord->setFormatter($formatter);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormatter(): FormatterInterface
|
|
|
|
{
|
|
|
|
$formatter = parent::getFormatter();
|
|
|
|
$this->slackRecord->setFormatter($formatter);
|
|
|
|
|
|
|
|
return $formatter;
|
|
|
|
}
|
|
|
|
}
|