Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from tuyakhov/slack_channel
Add slack channel & after notification send event
- Loading branch information
Showing
10 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* @copyright Anton Tuyakhov <atuyakhov@gmail.com> | ||
*/ | ||
|
||
namespace tuyakhov\notifications\channels; | ||
|
||
|
||
use tuyakhov\notifications\messages\SlackMessage; | ||
use tuyakhov\notifications\NotifiableInterface; | ||
use tuyakhov\notifications\NotificationInterface; | ||
use yii\base\Component; | ||
use yii\di\Instance; | ||
use yii\helpers\ArrayHelper; | ||
use yii\httpclient\Client; | ||
use yii\httpclient\Response; | ||
|
||
class SlackChannel extends Component implements ChannelInterface | ||
{ | ||
/** | ||
* @var Client|array|string | ||
*/ | ||
public $httpClient; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
if (!isset($this->httpClient)) { | ||
$this->httpClient = [ | ||
'class' => Client::className(), | ||
]; | ||
} | ||
$this->httpClient = Instance::ensure($this->httpClient, Client::className()); | ||
} | ||
|
||
/** | ||
* @param NotifiableInterface $recipient | ||
* @param NotificationInterface $notification | ||
* @return Response | ||
*/ | ||
public function send(NotifiableInterface $recipient, NotificationInterface $notification) | ||
{ | ||
/** @var SlackMessage $message */ | ||
$message = $notification->exportFor('slack'); | ||
$webhookUrl = $recipient->routeNotificationFor('slack'); | ||
$text = "*{$message->subject}*\n{$message->body}"; | ||
return $this->httpClient->createRequest() | ||
->setMethod('post') | ||
->setUrl($webhookUrl) | ||
->setData(ArrayHelper::merge(['text' => $text], $message->arguments)) | ||
->send(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* @copyright Anton Tuyakhov <atuyakhov@gmail.com> | ||
*/ | ||
|
||
namespace tuyakhov\notifications\events; | ||
|
||
|
||
use tuyakhov\notifications\NotifiableInterface; | ||
use tuyakhov\notifications\NotificationInterface; | ||
use yii\base\Event; | ||
|
||
class NotificationEvent extends Event | ||
{ | ||
/** | ||
* @var NotificationInterface | ||
*/ | ||
public $notification; | ||
|
||
/** | ||
* @var NotifiableInterface | ||
*/ | ||
public $recipient; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $channel; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
public $response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* @copyright Anton Tuyakhov <atuyakhov@gmail.com> | ||
*/ | ||
|
||
namespace tuyakhov\notifications\messages; | ||
|
||
|
||
class SlackMessage extends AbstractMessage | ||
{ | ||
/** | ||
* @var array optional arguments | ||
* @see https://api.slack.com/methods/chat.postMessage | ||
* Example: | ||
* [ | ||
* 'attachments' => [ | ||
* ['pretext' => 'pre-hello', 'text" => 'text-world'] | ||
* ] | ||
* ] | ||
*/ | ||
public $arguments = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* @copyright Anton Tuyakhov <atuyakhov@gmail.com> | ||
*/ | ||
|
||
namespace tuyakhov\notifications\tests; | ||
|
||
|
||
use tuyakhov\notifications\channels\SlackChannel; | ||
use tuyakhov\notifications\messages\SlackMessage; | ||
use yii\httpclient\Client; | ||
|
||
class SlackChannelTest | ||
{ | ||
public function testSend() | ||
{ | ||
$recipient = $this->createMock('tuyakhov\notifications\NotifiableInterface'); | ||
$recipient->expects($this->once()) | ||
->method('routeNotificationFor') | ||
->with('slack') | ||
->willReturn('https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'); | ||
|
||
$client = $this->createMock(Client::className()); | ||
$client->expects($this->once()) | ||
->method('send'); | ||
$client->method('createRequest') | ||
->willReturn(\Yii::createObject([ | ||
'class' => Request::className(), | ||
'client' => $client | ||
])); | ||
|
||
$channel = \Yii::createObject([ | ||
'class' => SlackChannel::className(), | ||
'httpClient' => $client | ||
]); | ||
|
||
$notification = $this->createMock('tuyakhov\notifications\NotificationInterface'); | ||
$notification->expects($this->once()) | ||
->method('exportFor') | ||
->with('slack') | ||
->willReturn(\Yii::createObject([ | ||
'class' => SlackMessage::className(), | ||
'body' => 'Test message body', | ||
])); | ||
$channel->send($recipient, $notification); | ||
|
||
} | ||
} |