├── registration.php ├── composer.json ├── etc ├── events.xml ├── module.xml ├── config.xml └── adminhtml │ └── system.xml ├── Test └── Unit │ └── Model │ └── ConfigTest.php ├── README.md ├── Model └── Config.php └── Observer └── Notify.php /registration.php: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | general 16 | Magento 2 17 | rocket 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Test/Unit/Model/ConfigTest.php: -------------------------------------------------------------------------------- 1 | scopeConfigInterface = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') 21 | ->getMockForAbstractClass(); 22 | $this->objectManagerHelper = new ObjectManagerHelper($this); 23 | $this->config = $this->objectManagerHelper->getObject( 24 | 'Staempfli\Slack\Model\Config', 25 | [ 26 | 'scopeConfig' => $this->scopeConfigInterface, 27 | ] 28 | ); 29 | } 30 | 31 | public function testGetMessageFormat() 32 | { 33 | $this->assertSame('mrkdwn', $this->config->getMessageFormat(), 'Message Format should be \'mrkdwn\' by default'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /etc/adminhtml/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
31 |
-------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Magento 2 Slack 2 | 3 | Magento 2 Module to send Notifications to a Slack Channel via Webhook. 4 | 5 | [![Codacy](https://api.codacy.com/project/badge/Grade/f7797be796714384a2dc1eb1b7c0e780)](https://www.codacy.com/app/Staempfli/magento2-module-slack?utm_source=github.com&utm_medium=referral&utm_content=staempfli/magento2-module-slack&utm_campaign=Badge_Grade) 6 | [![Code Climate](https://codeclimate.com/github/staempfli/magento2-module-slack/badges/gpa.svg)](https://codeclimate.com/github/staempfli/magento2-module-slack) 7 | [![Issue Count](https://codeclimate.com/github/staempfli/magento2-module-slack/badges/issue_count.svg)](https://codeclimate.com/github/staempfli/magento2-module-slack) 8 | 9 | Requirements 10 | ------------ 11 | - PHP >= 7.0.* 12 | - Magento >= 2.1.* 13 | - [staempfli/magento2-module-chat-connector](https://github.com/staempfli/magento2-module-chat-connector) ~1.0 14 | - [league/html-to-markdown](https://github.com/thephpleague/html-to-markdown/) ~4.4 15 | 16 | Compatibility 17 | ------------- 18 | - Magento >= 2.1 19 | 20 | ## Installation 21 | 22 | ``` 23 | $ composer require "staempfli/magento2-module-slack":"~1.1" 24 | ``` 25 | 26 | Support 27 | ------- 28 | If you have any issues with this extension, open an issue on [GitHub](https://github.com/staempfli/magento2-module-slack/issues). 29 | 30 | Contribution 31 | ------------ 32 | Any contribution is highly appreciated. The best way to contribute code is to open a [pull request on GitHub](https://help.github.com/articles/using-pull-requests). 33 | 34 | Developer 35 | --------- 36 | Staempfli Webteam, and all other [contributors](https://github.com/staempfli/magento2-module-slack/contributors) 37 | 38 | License 39 | ------- 40 | [Open Software License ("OSL") v. 3.0](https://opensource.org/licenses/OSL-3.0) 41 | 42 | Copyright 43 | --------- 44 | (c) 2017, Stämpfli AG -------------------------------------------------------------------------------- /Model/Config.php: -------------------------------------------------------------------------------- 1 | scopeConfig = $scopeConfig; 34 | } 35 | 36 | /** 37 | * @return mixed 38 | */ 39 | public function getUrl() 40 | { 41 | $url = $this->scopeConfig->getValue(self::XML_PATH_SLACK_URL, ScopeInterface::SCOPE_STORE); 42 | $url = rtrim($url, '/'); 43 | return $url; 44 | } 45 | 46 | /** 47 | * @return mixed 48 | */ 49 | public function getChannel() 50 | { 51 | $channel = $this->scopeConfig->getValue(self::XML_PATH_SLACK_CHANNEL, ScopeInterface::SCOPE_STORE); 52 | return sprintf('#%s', ltrim($channel, '#')); 53 | } 54 | 55 | /** 56 | * @return mixed 57 | */ 58 | public function getUsername() 59 | { 60 | return $this->scopeConfig->getValue(self::XML_PATH_SLACK_USERNAME, ScopeInterface::SCOPE_STORE); 61 | } 62 | 63 | /** 64 | * @return mixed 65 | */ 66 | public function getIcon() 67 | { 68 | $icon = trim($this->scopeConfig->getValue(self::XML_PATH_SLACK_ICON, ScopeInterface::SCOPE_STORE), ':'); 69 | return sprintf(':%s:', $icon); 70 | } 71 | 72 | /** 73 | * @return string 74 | */ 75 | public function getMessageFormat() 76 | { 77 | return self::MESSAGE_FORMAT; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Observer/Notify.php: -------------------------------------------------------------------------------- 1 | message = $message; 51 | $this->messageManagement = $messageManagement; 52 | $this->config = $config; 53 | $this->converter = new HtmlConverter(); 54 | } 55 | 56 | /** 57 | * @param Observer $observer 58 | * @return void 59 | */ 60 | public function execute(\Magento\Framework\Event\Observer $observer) 61 | { 62 | $data = []; 63 | $data['text'] = strip_tags($observer->getMessage()); 64 | 65 | if ($this->config->getMessageFormat() === 'mrkdwn') { 66 | $this->converter->getConfig()->setOption('bold_style', '*'); 67 | $this->converter->getConfig()->setOption('italic_style', '_'); 68 | $this->converter->getConfig()->setOption('strike_style', '~'); 69 | $this->converter->getConfig()->setOption('code_style', "`"); 70 | $data['text'] = substr($this->converter->convert(nl2br($observer->getMessage())), 0, self::MESSAGE_LIMIT); 71 | $data['mrkdwn'] = true; 72 | $data['mrkdwn_in'] = 'text'; 73 | } 74 | 75 | $data['channel'] = $this->config->getChannel(); 76 | $data['username'] = $this->config->getUsername(); 77 | $data['icon_emoji'] = $this->config->getIcon(); 78 | $message = $this->message 79 | ->setUrl($this->config->getUrl()) 80 | ->setEvent($observer->getEvent()) 81 | ->setMessageData($data); 82 | $this->messageManagement->send($message); 83 | } 84 | } 85 | --------------------------------------------------------------------------------