SlackHandler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. /**
  13. * Sends notifications through Slack API
  14. *
  15. * @author Greg Kedzierski <greg@gregkedzierski.com>
  16. * @see https://api.slack.com/
  17. */
  18. class SlackHandler extends SocketHandler
  19. {
  20. /**
  21. * Slack API token
  22. * @var string
  23. */
  24. private $token;
  25. /**
  26. * Slack channel (encoded ID or name)
  27. * @var string
  28. */
  29. private $channel;
  30. /**
  31. * Name of a bot
  32. * @var string
  33. */
  34. private $username;
  35. /**
  36. * Emoji icon name
  37. * @var string
  38. */
  39. private $iconEmoji;
  40. /**
  41. * Whether the message should be added to Slack as attachment (plain text otherwise)
  42. * @var bool
  43. */
  44. private $useAttachment;
  45. /**
  46. * @param string $token Slack API token
  47. * @param string $channel Slack channel (encoded ID or name)
  48. * @param string $username Name of a bot
  49. * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
  50. * @param int $level The minimum logging level at which this handler will be triggered
  51. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  52. */
  53. public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true, $iconEmoji = null)
  54. {
  55. if (!extension_loaded('openssl')) {
  56. throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
  57. }
  58. parent::__construct('ssl://slack.com:443', $level, $bubble);
  59. $this->token = $token;
  60. $this->channel = $channel;
  61. $this->username = $username;
  62. $this->iconEmoji = $iconEmoji;
  63. $this->useAttachment = $useAttachment;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @param array $record
  69. * @return string
  70. */
  71. protected function generateDataStream($record)
  72. {
  73. $content = $this->buildContent($record);
  74. return $this->buildHeader($content) . $content;
  75. }
  76. /**
  77. * Builds the body of API call
  78. *
  79. * @param array $record
  80. * @return string
  81. */
  82. private function buildContent($record)
  83. {
  84. $dataArray = array(
  85. 'token' => $this->token,
  86. 'channel' => $this->channel,
  87. 'username' => $this->username,
  88. 'text' => '',
  89. 'attachments' => array()
  90. );
  91. if ($this->useAttachment) {
  92. $dataArray['attachments'] = json_encode(
  93. array(
  94. array(
  95. 'fallback' => $record['message'],
  96. 'color' => $this->getAttachmentColor($record['level']),
  97. 'fields' => array(
  98. array(
  99. 'title' => 'Message',
  100. 'value' => $record['message'],
  101. 'short' => false
  102. ),
  103. array(
  104. 'title' => 'Level',
  105. 'value' => $record['level_name'],
  106. 'short' => true
  107. )
  108. )
  109. )
  110. )
  111. );
  112. } else {
  113. $dataArray['text'] = $record['message'];
  114. }
  115. if ($this->iconEmoji !== null) {
  116. $dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
  117. }
  118. return http_build_query($dataArray);
  119. }
  120. /**
  121. * Builds the header of the API Call
  122. *
  123. * @param string $content
  124. * @return string
  125. */
  126. private function buildHeader($content)
  127. {
  128. $header = "POST /api/chat.postMessage HTTP/1.1\r\n";
  129. $header .= "Host: slack.com\r\n";
  130. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  131. $header .= "Content-Length: " . strlen($content) . "\r\n";
  132. $header .= "\r\n";
  133. return $header;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. *
  138. * @param array $record
  139. */
  140. protected function write(array $record)
  141. {
  142. parent::write($record);
  143. $this->closeSocket();
  144. }
  145. /**
  146. * Returned a Slack message attachment color associated with
  147. * provided level.
  148. *
  149. * @param int $level
  150. * @return string
  151. */
  152. protected function getAttachmentColor($level)
  153. {
  154. switch (true) {
  155. case $level >= Logger::ERROR:
  156. return 'danger';
  157. case $level >= Logger::WARNING:
  158. return 'warning';
  159. case $level >= Logger::INFO:
  160. return 'good';
  161. default:
  162. return '#e3e4e6';
  163. }
  164. }
  165. }