Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sticky channel option #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (defaults to false)
`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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will force the result to be an boolean. Otherwise it can also be any other type.
return (bool) $this->sticky_channel;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the phpdoc, sticky_channel must be a boolean and developpers should respect this.

Casting anything to a boolean is not the right way IMO.

When this project will use PHP >=7.0, then we will be able to use strict type instead.

}

/**
* @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