Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

A basic Slack client library providing simple posting to Slack channels using the webhook API written in PHP.

License

Notifications You must be signed in to change notification settings

markenwerk/php-slack-client

Repository files navigation

PHP Slack Client

Build Status Test Coverage Dependency Status SensioLabs Insight Code Climate Latest Stable Version Total Downloads License

A basic Slack client library providing simple posting to Slack channels using the webhook API.

Installation

{
   	"require": {
        "markenwerk/slack-client": "~2.0"
    }
}

Usage

Autoloading and namesapce

require_once('path/to/vendor/autoload.php');

Posting to a channel or member

The following example will post to a Slack channel or member looking like this:

Slack Example Post

Setup the API client

use Markenwerk\SlackClient\SlackClient;

$client = new SlackClient();
$client
	->setSubdomainName('markenwerk')
	->setToken('<YOUR_API_TOKEN>')
	->setUsername('PHP SlackClient');

Setup a Slack message with attachments

use Markenwerk\SlackClient\SlackAttachment;
use Markenwerk\SlackClient\SlackAttachmentField;
use Markenwerk\SlackClient\SlackMessage;

$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);

Post to a channel

$client->postToChannel('#channel', $message);

Post to a member

$client->postToMember('@member', $message);

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.

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 Slack Client.

  • Markenwerk\CommonException\ParserException\StringifyException on posting to Slack
  • Markenwerk\CommonException\NetworkException\UnexpectedResponseException on posting to Slack
  • Markenwerk\CommonException\NetworkException\ConnectionTimeoutException on posting to Slack
  • Markenwerk\CommonException\NetworkException\CurlException on posting to Slack

Contribution

Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.

License

PHP Slack Client is under the MIT license.

About

A basic Slack client library providing simple posting to Slack channels using the webhook API written in PHP.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages