scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Monolog package. |
||
5 | * |
||
6 | * (c) Jordi Boggiano <j.boggiano@seld.be> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Monolog\Handler; |
||
13 | |||
14 | use Monolog\Logger; |
||
15 | |||
16 | /** |
||
17 | * Sends notifications through Slack's Slackbot |
||
18 | * |
||
19 | * @author Haralan Dobrev <hkdobrev@gmail.com> |
||
20 | * @see https://slack.com/apps/A0F81R8ET-slackbot |
||
21 | */ |
||
22 | class SlackbotHandler extends AbstractProcessingHandler |
||
23 | { |
||
24 | /** |
||
25 | * The slug of the Slack team |
||
26 | * @var string |
||
27 | */ |
||
28 | private $slackTeam; |
||
29 | |||
30 | /** |
||
31 | * Slackbot token |
||
32 | * @var string |
||
33 | */ |
||
34 | private $token; |
||
35 | |||
36 | /** |
||
37 | * Slack channel name |
||
38 | * @var string |
||
39 | */ |
||
40 | private $channel; |
||
41 | |||
42 | /** |
||
43 | * @param string $slackTeam Slack team slug |
||
44 | * @param string $token Slackbot token |
||
45 | * @param string $channel Slack channel (encoded ID or name) |
||
46 | * @param int $level The minimum logging level at which this handler will be triggered |
||
47 | * @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
||
48 | */ |
||
49 | public function __construct($slackTeam, $token, $channel, $level = Logger::CRITICAL, $bubble = true) |
||
50 | { |
||
51 | parent::__construct($level, $bubble); |
||
52 | |||
53 | $this->slackTeam = $slackTeam; |
||
54 | $this->token = $token; |
||
55 | $this->channel = $channel; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | * |
||
61 | * @param array $record |
||
62 | */ |
||
63 | protected function write(array $record) |
||
64 | { |
||
65 | $slackbotUrl = sprintf( |
||
66 | 'https://%s.slack.com/services/hooks/slackbot?token=%s&channel=%s', |
||
67 | $this->slackTeam, |
||
68 | $this->token, |
||
69 | $this->channel |
||
70 | ); |
||
71 | |||
72 | $ch = curl_init(); |
||
73 | curl_setopt($ch, CURLOPT_URL, $slackbotUrl); |
||
74 | curl_setopt($ch, CURLOPT_POST, true); |
||
75 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
76 | curl_setopt($ch, CURLOPT_POSTFIELDS, $record['message']); |
||
77 | |||
78 | Curl\Util::execute($ch); |
||
79 | } |
||
80 | } |