Slack Notifier
Warning: You are browsing the documentation for Symfony 6.1, which is no longer maintained.
Consider upgrading your projects to Symfony 7.1.
Slack Notifier
The Slack Notifier package allows to use Slack via the Symfony Notifier component. Read the main Notifier docs to learn about installing and configuring that component.
Adding Interactions to a Message
With a Slack message, you can use the SlackOptions class to add some interactive options called Block elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackActionsBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackImageBlockElement;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Contribute To Symfony');
// Create Slack Actions Block and add some buttons
$contributeToSymfonyBlocks = (new SlackActionsBlock())
->button(
'Improve Documentation',
'https://symfony.com/doc/current/contributing/documentation/standards.html',
'primary'
)
->button(
'Report bugs',
'https://symfony.com/doc/current/contributing/code/bugs.html',
'danger'
);
$slackOptions = (new SlackOptions())
->block((new SlackSectionBlock())
->text('The Symfony Community')
->accessory(
new SlackImageBlockElement(
'https://symfony.com/favicons/apple-touch-icon.png',
'Symfony'
)
)
)
->block(new SlackDividerBlock())
->block($contributeToSymfonyBlocks);
// Add the custom options to the chat message and send the message
$chatMessage->options($slackOptions);
$chatter->send($chatMessage);
Adding Fields and Values to a Message
To add fields and values to your message you can use the SlackSectionBlock::field() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Symfony Feature');
$options = (new SlackOptions())
->block((new SlackSectionBlock())->text('My message'))
->block(new SlackDividerBlock())
->block(
(new SlackSectionBlock())
->field('*Max Rating*')
->field('5.0')
->field('*Min Rating*')
->field('1.0')
);
// Add the custom options to the chat message and send the message
$chatMessage->options($options);
$chatter->send($chatMessage);
The result will be something like:
Adding a Header to a Message
To add a header to your message use the SlackHeaderBlock class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackHeaderBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Symfony Feature');
$options = (new SlackOptions())
->block((new SlackHeaderBlock('My Header')))
->block((new SlackSectionBlock())->text('My message'))
->block(new SlackDividerBlock())
->block(
(new SlackSectionBlock())
->field('*Max Rating*')
->field('5.0')
->field('*Min Rating*')
->field('1.0')
);
// Add the custom options to the chat message and send the message
$chatMessage->options($options);
$chatter->send($chatMessage);
The result will be something like:
Adding a Footer to a Message
To add a footer to your message use the SlackContextBlock class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Symfony Feature');
$contextBlock = (new SlackContextBlock())
->text('My Context')
->image('https://symfony.com/logos/symfony_white_03.png', 'Symfony Logo')
;
$options = (new SlackOptions())
->block((new SlackSectionBlock())->text('My message'))
->block(new SlackDividerBlock())
->block(
(new SlackSectionBlock())
->field('*Max Rating*')
->field('5.0')
->field('*Min Rating*')
->field('1.0')
)
->block($contextBlock)
;
$chatter->send($chatMessage);
The result will be something like:
Sending a Message as a Reply
To send your slack message as a reply in a thread use the SlackOptions::threadTs() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Symfony Feature');
$options = (new SlackOptions())
->block((new SlackSectionBlock())->text('My reply'))
->threadTs('1621592155.003100')
;
// Add the custom options to the chat message and send the message
$chatMessage->options($options);
$chatter->send($chatMessage);
The result will be something like: