A basic Slack client library providing simple posting to Slack channels using the webhook API.
{
"require": {
"markenwerk/slack-client": "~2.0"
}
}
require_once('path/to/vendor/autoload.php');
The following example will post to a Slack channel or member looking like this:
use Markenwerk\SlackClient\SlackClient;
$client = new SlackClient();
$client
->setSubdomainName('markenwerk')
->setToken('<YOUR_API_TOKEN>')
->setUsername('PHP SlackClient');
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);
$client->postToChannel('#channel', $message);
$client->postToMember('@member', $message);
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.
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\StringifyExceptionon posting to SlackMarkenwerk\CommonException\NetworkException\UnexpectedResponseExceptionon posting to SlackMarkenwerk\CommonException\NetworkException\ConnectionTimeoutExceptionon posting to SlackMarkenwerk\CommonException\NetworkException\CurlExceptionon posting to Slack
Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.
PHP Slack Client is under the MIT license.
