<?php
/*
* 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\Logger;
/**
* Sends notifications through Slack's Slackbot
*
* @author Haralan Dobrev <hkdobrev@gmail.com>
* @see https://slack.com/apps/A0F81R8ET-slackbot
*/
class SlackbotHandler extends AbstractProcessingHandler
{
private $slackTeam;
private $token;
private $channel;
public function __construct($slackTeam, $token, $channel, $level = Logger::CRITICAL, $bubble = true)
{
parent::__construct($level, $bubble);
$this->slackTeam = $slackTeam;
$this->token = $token;
$this->channel = $channel;
}
protected function write(array $record)
{
$slackbotUrl = sprintf(
'https://%s.slack.com/services/hooks/slackbot?token=%s&channel=%s',
$this->slackTeam,
$this->token,
$this->channel
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $slackbotUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $record['message']);
Curl\Util::execute($ch);
}
}