Symfony bundle integration of the popular maknz/slack library.
All the installation and usage instructions are located in this README. Check it for specific version:
- 1.x with support for Symfony
>=2.7
This version of the project requires:
- PHP 5.6+
- Symfony 2.7+
First of all, you need to require this library through composer:
$ composer require nexylan/slack-bundle
Then, enable the bundle on the AppKernel
class:
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Nexy\SlackBundle\NexySlackBundle(),
);
// ...
return $bundles
}
Configure the bundle to your needs (example with default values):
nexy_slack:
# If you want to use your own Guzzle instance, set the service here.
guzzle_service: null
# The Slack API Incoming WebHooks URL.
endpoint: ~ # Required
channel: null
username: null
icon: null
link_names: false
unfurl_links: false
unfurl_media: true
allow_markdown: true
markdown_in_attachments: []
Excepted endpoint
, all the other configuration keys are related to the Slack client default settings.
All those settings are described on the maknz/slack documentation.
The Slack client accepts a third constructor parameter to pass a Guzzle instance instead of instantiating it itself.
You can define it with the guzzle_service
configuration key.
The service can be created by yourself, or by using a Guzzle bundle. Some examples are described below.
First, define your Guzzle service:
# services.yml
services:
guzzle.slack:
class: GuzzleHttp\Client
arguments: # First argument is the Guzzle options array.
- { timeout: 5 }
Then, fill the configuration with your service name:
nexy_slack:
guzzle_service: guzzle.slack
endpoint: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
That's it! Now the Slack client instance will use your Guzzle service.
This bundle lets you define more advanced Guzzle instances with ease. Plus, it is packaged with a profiler integration, so you can see each Guzzle request.
First, read the documentation of this package to setup this bundle.
Now you can define your Guzzle service from the bundle config, and link it to the Slack client:
guzzle:
clients:
slack:
options:
timeout: 5
nexy_slack:
guzzle_service: guzzle.client.slack
endpoint: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
The Slack client instance can be retrieved from the nexy_slack.client
service.
Here is an example:
<?php
namespace AppBundle\Controller;
use Maknz\Slack\Attachment;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction()
{
$slack = $this->get('nexy_slack.client');
$message = $slack->createMessage();
$message
->to('#test')
->from('John Doe')
->withIcon(':ghost:')
->setText('This is an amazing message!')
;
$message->attach(new Attachment([
'color' => '#CCC',
'text' => 'More info about attachment on <https://api.slack.com/docs/formatting|Slack documentation>!',
'mrkdwn_in' => ['text'],
]));
$slack->sendMessage($message);
}
}
All the how to manipulate the Slack client is on the maknz/slack documentation.