Bladeren bron

Added emoji support as per slack api doc: https://api.slack.com/methods/chat.postMessage

Derek Clapham 11 jaren geleden
bovenliggende
commit
c63d082444
2 gewijzigde bestanden met toevoegingen van 13 en 5 verwijderingen
  1. 9 1
      src/Monolog/Handler/SlackHandler.php
  2. 4 4
      tests/Monolog/Handler/SlackHandlerTest.php

+ 9 - 1
src/Monolog/Handler/SlackHandler.php

@@ -38,6 +38,12 @@ class SlackHandler extends SocketHandler
      * @var string
      */
     private $username;
+    
+    /**
+     * Emoji icon name
+     * @var string
+     */
+    private $iconEmoji;
 
     /**
      * Whether the message should be added to Slack as attachment (plain text otherwise)
@@ -53,7 +59,7 @@ class SlackHandler extends SocketHandler
      * @param int    $level         The minimum logging level at which this handler will be triggered
      * @param bool   $bubble        Whether the messages that are handled can bubble up the stack or not
      */
-    public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true)
+    public function __construct($token, $channel, $username = 'Monolog', $iconEmoji = 'alien', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true)
     {
         if (!extension_loaded('openssl')) {
             throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
@@ -64,6 +70,7 @@ class SlackHandler extends SocketHandler
         $this->token = $token;
         $this->channel = $channel;
         $this->username = $username;
+        $this->iconEmoji = $iconEmoji;
         $this->useAttachment = $useAttachment;
     }
 
@@ -92,6 +99,7 @@ class SlackHandler extends SocketHandler
             'token' => $this->token,
             'channel' => $this->channel,
             'username' => $this->username,
+            'icon_emoji' => ":{$this->iconEmoji}:",
             'text' => '',
             'attachments' => array()
         );

+ 4 - 4
tests/Monolog/Handler/SlackHandlerTest.php

@@ -54,7 +54,7 @@ class SlackHandlerTest extends TestCase
         fseek($this->res, 0);
         $content = fread($this->res, 1024);
 
-        $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
+        $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&icon_emoji=%3Aalien%3A&text=&attachments=.*$/', $content);
     }
 
     /**
@@ -72,7 +72,7 @@ class SlackHandlerTest extends TestCase
 
     public function testWriteContentWithPlainTextMessage()
     {
-        $this->createHandler('myToken', 'channel1', 'Monolog', false);
+        $this->createHandler('myToken', 'channel1', 'Monolog', 'alien', false);
         $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
         fseek($this->res, 0);
         $content = fread($this->res, 1024);
@@ -94,9 +94,9 @@ class SlackHandlerTest extends TestCase
         );
     }
 
-    private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true)
+    private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $iconEmoji = 'alien', $useAttachment = true)
     {
-        $constructorArgs = array($token, $channel, $username, $useAttachment, Logger::DEBUG, true);
+        $constructorArgs = array($token, $channel, $username, $iconEmoji, $useAttachment, Logger::DEBUG, true);
         $this->res = fopen('php://memory', 'a');
         $this->handler = $this->getMock(
             '\Monolog\Handler\SlackHandler',