Skip to content

Commit

Permalink
Add sticky channel option
Browse files Browse the repository at this point in the history
With this option, all message will be sent to the default channel only.

This is useful for dev environment when you want to concentrate all test message to a specific test channel.
  • Loading branch information
soullivaneuh committed Aug 31, 2016
1 parent e9bab77 commit 2066ab4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The default settings are pretty good, but you may wish to set up default behavio
Field | Type | Description
----- | ---- | -----------
`channel` | string | The default channel that messages will be sent to
`sticky_channel` | bool | If set to true, all messages will be sent to the default channel only
`username` | string | The default username for your bot
`icon` | string | The default icon that messages will be sent with, either `:emoji:` or a URL to an image
`link_names` | bool | Whether names like `@regan` or `#accounting` should be linked in the message (defaults to false)
Expand Down
32 changes: 31 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class Client
*/
protected $channel;

/**
* If set to true, all messages will be sent to the default channel,
* even if you specify another one during the runtime.
*
* This is useful for dev environment.
*
* @var bool
*/
protected $sticky_channel = false;

/**
* The default username to send messages as.
*
Expand Down Expand Up @@ -95,6 +105,10 @@ public function __construct($endpoint, array $attributes = [], Guzzle $guzzle =
$this->setDefaultChannel($attributes['channel']);
}

if (isset($attributes['sticky_channel'])) {
$this->setStickyChannel($attributes['sticky_channel']);
}

if (isset($attributes['username'])) {
$this->setDefaultUsername($attributes['username']);
}
Expand Down Expand Up @@ -181,6 +195,22 @@ public function setDefaultChannel($channel)
$this->channel = $channel;
}

/**
* @return bool
*/
public function isStickyChannel()
{
return $this->sticky_channel;
}

/**
* @param bool $sticky_channel
*/
public function setStickyChannel($sticky_channel)
{
$this->sticky_channel = $sticky_channel;
}

/**
* Get the default username messages will be created for.
*
Expand Down Expand Up @@ -385,7 +415,7 @@ public function preparePayload(Message $message)
{
$payload = [
'text' => $message->getText(),
'channel' => $message->getChannel(),
'channel' => $this->isStickyChannel() ? $this->getDefaultChannel() : $message->getChannel(),
'username' => $message->getUsername(),
'link_names' => $this->getLinkNames() ? 1 : 0,
'unfurl_links' => $this->getUnfurlLinks(),
Expand Down
25 changes: 25 additions & 0 deletions tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,31 @@ public function testMessageWithAttachmentsAndActions()
$this->assertEquals($expectedHttpData, $payload);
}

public function testMessageWithStickyChannel()
{
$expectedHttpData = [
'username' => 'Archer',
'channel' => 'test',
'text' => 'Message',
'link_names' => 0,
'unfurl_links' => false,
'unfurl_media' => true,
'mrkdwn' => true,
'attachments' => [],
];

$client = new Client('http://fake.endpoint', [
'channel' => 'test',
'sticky_channel' => true,
]);

$message = $client->to('@regan')->from('Archer')->setText('Message');

$payload = $client->preparePayload($message);

$this->assertEquals($expectedHttpData, $payload);
}

public function testBadEncodingThrowsException()
{
$client = $this->getNetworkStubbedClient();
Expand Down

0 comments on commit 2066ab4

Please sign in to comment.