This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Brecht-Precht
committed
May 2, 2016
1 parent
6597606
commit ea4ec1b
Showing
10 changed files
with
1,002 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,135 @@ | ||
# php-slack-client | ||
A PHP library acting as a Slack webhook emitting client. | ||
# PHP Slack Client | ||
|
||
[![Build Status](https://travis-ci.org/markenwerk/php-slack-client.svg?branch=master)](https://travis-ci.org/markenwerk/php-slack-client) | ||
[![Test Coverage](https://codeclimate.com/github/markenwerk/php-slack-client/badges/coverage.svg)](https://codeclimate.com/github/markenwerk/php-slack-client/coverage) | ||
[![Dependency Status](https://www.versioneye.com/user/projects/571f8827fcd19a00415b2836/badge.svg)](https://www.versioneye.com/user/projects/571f8827fcd19a00415b2836) | ||
[![Code Climate](https://codeclimate.com/github/markenwerk/php-slack-client/badges/gpa.svg)](https://codeclimate.com/github/markenwerk/php-slack-client) | ||
[![Latest Stable Version](https://poser.pugx.org/markenwerk/slack-client/v/stable)](https://packagist.org/packages/markenwerk/slack-client) | ||
[![Total Downloads](https://poser.pugx.org/markenwerk/slack-client/downloads)](https://packagist.org/packages/markenwerk/slack-client) | ||
[![License](https://poser.pugx.org/markenwerk/slack-client/license)](https://packagist.org/packages/markenwerk/slack-client) | ||
|
||
A basic Slack client library providing simple posting to Slack channels using the webhook API. | ||
|
||
## Installation | ||
|
||
```{json} | ||
{ | ||
"require": { | ||
"markenwerk/slack-client": "~1.0" | ||
} | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
### Autoloading and namesapce | ||
|
||
```{php} | ||
require_once('path/to/vendor/autoload.php'); | ||
``` | ||
|
||
### Posting to a channel | ||
|
||
The following example will produce a post in a Slack channel looking like this: | ||
|
||
![Slack ] | ||
|
||
```{php} | ||
use SlackClient\SlackAttachment; | ||
use SlackClient\SlackAttachmentField; | ||
use SlackClient\SlackClient; | ||
use SlackClient\SlackMessage; | ||
$client = new SlackClient(); | ||
$client | ||
->setSubdomainName('markenwerk') | ||
->setToken('<YOUR_API_TOKEN>') | ||
->setUsername('PHP SlackClient') | ||
->setChannel('#log'); | ||
$message = new SlackMessage(); | ||
$message | ||
->setText('A basic Slack client library providing simple posting to Slack channels using the webhook API.') | ||
->setIconUrl('https://avatars2.githubusercontent.com/u/5921253?v=3&s=200') | ||
->setUnfurlLinks(true) | ||
->setUnfurlMedia(true); | ||
$attachment = new SlackAttachment(); | ||
$attachment | ||
->setText('A basic Slack client library providing simple posting to Slack channels using the webhook API.') | ||
->setPretext('A basic Slack client library.') | ||
->setFallback('A basic Slack client library providing simple posting to Slack channels using the webhook API.') | ||
->setColor(SlackAttachment::COLOR_WARNING); | ||
$shortAttachmentField = new SlackAttachmentField(); | ||
$shortAttachmentField | ||
->setTitle('Short field') | ||
->setValue('Some chars') | ||
->setShort(true); | ||
$anotherShortAttachmentField = new SlackAttachmentField(); | ||
$anotherShortAttachmentField | ||
->setTitle('Short field') | ||
->setValue('Some chars') | ||
->setShort(true); | ||
$attachmentField = new SlackAttachmentField(); | ||
$attachmentField | ||
->setTitle('Regular field') | ||
->setValue('Some more chars') | ||
->setShort(false); | ||
$anotherAttachmentField = new SlackAttachmentField(); | ||
$anotherAttachmentField | ||
->setTitle('Regular field') | ||
->setValue('Some more chars') | ||
->setShort(false); | ||
$attachment | ||
->addField($shortAttachmentField) | ||
->addField($anotherShortAttachmentField) | ||
->addField($attachmentField) | ||
->addField($anotherAttachmentField); | ||
$message->addAttachment($attachment); | ||
$client->post($message); | ||
``` | ||
|
||
--- | ||
|
||
## Extending the Basic HTTP Client | ||
|
||
Every part of the client is based upon proper interfaces. Most class instances can get injected into the client itself. | ||
If you want to extend the client just write some classes implementing the according interface and you´re done with that. | ||
|
||
Take a look at the [PHP JSON HTTP Client](https://github.com/markenwerk/php-json-http-client) which is an extension of the PHP Basic HTTP Client. | ||
|
||
--- | ||
|
||
## Exception handling | ||
|
||
PHP Basic HTTP Client provides different exceptions – also provided by the PHP Common Exceptions project – for proper handling. | ||
You can find more information about [PHP Common Exceptions at Github](https://github.com/markenwerk/php-common-exceptions). | ||
|
||
### Exceptions to be expected | ||
|
||
In general you should expect that any setter method could thrown an `\InvalidArgumentException`. The following exceptions could get thrown while using PHP Basic HTTP Client. | ||
|
||
- `CommonException\IoException\FileNotFoundException` on configuring a `ClientCertificateAuthentication`instance | ||
- `CommonException\IoException\FileReadableException` on configuring a `ClientCertificateAuthentication`instance | ||
- `BasicHttpClient\Exception\HttpRequestAuthenticationException` on performing a request | ||
- `BasicHttpClient\Exception\HttpRequestException` on performing a request | ||
- `CommonException\NetworkException\ConnectionTimeoutException` on performing a request | ||
- `CommonException\NetworkException\CurlException` on performing a request | ||
|
||
--- | ||
|
||
## Contribution | ||
|
||
Contributing to our projects is always very appreciated. | ||
**But: please follow the contribution guidelines written down in the [CONTRIBUTING.md](https://github.com/markenwerk/php-slack-client/blob/master/CONTRIBUTING.md) document.** | ||
|
||
## License | ||
|
||
PHP Slack Client is under the MIT license. |
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,225 @@ | ||
<?php | ||
|
||
namespace SlackClient; | ||
|
||
/** | ||
* Class SlackAttachment | ||
* | ||
* @package SlackClient | ||
*/ | ||
class SlackAttachment implements SlackAttachmentInterface | ||
{ | ||
|
||
const COLOR_GOOD = 'good'; | ||
const COLOR_WARNING = 'warning'; | ||
const COLOR_DANGER = 'danger'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $fallback = ''; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $text = null; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $pretext = null; | ||
|
||
/** | ||
* Can either be one of 'good', 'warning', 'danger', or any hex color code | ||
* | ||
* @var string | ||
*/ | ||
private $color = self::COLOR_GOOD; | ||
|
||
/** | ||
* @var SlackAttachmentFieldInterface[] | ||
*/ | ||
private $fields = array(); | ||
|
||
/** | ||
* @param string $color | ||
* @return $this | ||
*/ | ||
public function setColor($color) | ||
{ | ||
if (!is_string($color)) { | ||
$argumentType = (is_object($color)) ? get_class($color) : gettype($color); | ||
throw new \InvalidArgumentException('Expected the color as string. Got ' . $argumentType); | ||
} | ||
$this->color = $color; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getColor() | ||
{ | ||
return $this->color; | ||
} | ||
|
||
/** | ||
* @param string $fallback | ||
* @return $this | ||
*/ | ||
public function setFallback($fallback) | ||
{ | ||
if (!is_string($fallback)) { | ||
$argumentType = (is_object($fallback)) ? get_class($fallback) : gettype($fallback); | ||
throw new \InvalidArgumentException('Expected the fallback text as string. Got ' . $argumentType); | ||
} | ||
$this->fallback = $fallback; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFallback() | ||
{ | ||
return $this->fallback; | ||
} | ||
|
||
/** | ||
* @param SlackAttachmentFieldInterface[] $fields | ||
* @return $this | ||
*/ | ||
public function setFields($fields) | ||
{ | ||
if (!is_array($fields)) { | ||
$argumentType = (is_object($fields)) ? get_class($fields) : gettype($fields); | ||
throw new \InvalidArgumentException('Expected the attachment fields as array. Got ' . $argumentType); | ||
} | ||
foreach ($fields as $field) { | ||
if (!$field instanceof SlackAttachmentFieldInterface) { | ||
$argumentType = (is_object($field)) ? get_class($field) : gettype($field); | ||
throw new \InvalidArgumentException('Expected the attachment fields as array of SlackAttachmentInterface implementations. Found ' . $argumentType); | ||
} | ||
} | ||
$this->fields = $fields; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param SlackAttachmentFieldInterface $field | ||
* @return $this | ||
*/ | ||
public function addField(SlackAttachmentFieldInterface $field) | ||
{ | ||
$this->fields[] = $field; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param SlackAttachmentFieldInterface $field | ||
* @return $this | ||
*/ | ||
public function removeField(SlackAttachmentFieldInterface $field) | ||
{ | ||
for ($i = 0; $i < count($this->fields); $i++) { | ||
if ($this->fields[$i] == $field) { | ||
unset($this->fields[$i]); | ||
return $this; | ||
} | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasFields() | ||
{ | ||
return count($this->fields) > 0; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function countFields() | ||
{ | ||
return count($this->fields); | ||
} | ||
|
||
/** | ||
* @return SlackAttachmentFieldInterface[] | ||
*/ | ||
public function getFields() | ||
{ | ||
return $this->fields; | ||
} | ||
|
||
/** | ||
* @param string $pretext | ||
* @return $this | ||
*/ | ||
public function setPretext($pretext) | ||
{ | ||
if (!is_string($pretext)) { | ||
$argumentType = (is_object($pretext)) ? get_class($pretext) : gettype($pretext); | ||
throw new \InvalidArgumentException('Expected the pretext as string. Got ' . $argumentType); | ||
} | ||
$this->pretext = $pretext; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPretext() | ||
{ | ||
return $this->pretext; | ||
} | ||
|
||
/** | ||
* @param string $text | ||
* @return $this | ||
*/ | ||
public function setText($text) | ||
{ | ||
if (!is_string($text)) { | ||
$argumentType = (is_object($text)) ? get_class($text) : gettype($text); | ||
throw new \InvalidArgumentException('Expected the text as string. Got ' . $argumentType); | ||
} | ||
$this->text = $text; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getText() | ||
{ | ||
return $this->text; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
$attachment = array( | ||
'fallback' => $this->getFallback(), | ||
'color' => $this->getColor(), | ||
); | ||
if (!is_null($this->getText())) { | ||
$attachment['text'] = $this->getText(); | ||
} | ||
if (!is_null($this->getPretext())) { | ||
$attachment['pretext'] = $this->getPretext(); | ||
} | ||
foreach ($this->getFields() as $field) { | ||
if (!isset($attachment['fields'])) { | ||
$attachment['fields'] = array(); | ||
} | ||
$attachment['fields'][] = $field->toArray(); | ||
} | ||
return $attachment; | ||
} | ||
|
||
} |
Oops, something went wrong.