PHP code example of symfony / slack-notifier

1. Go to this page and download the library: Download symfony/slack-notifier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

symfony / slack-notifier example snippets


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

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackButtonBlockElement;
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('Contribute To Symfony');

$slackOptions = (new SlackOptions())
    ->block((new SlackSectionBlock())
        ->text('Symfony Framework')
        ->accessory(
            new SlackButtonBlockElement(
                'Report bugs',
                'https://symfony.com/doc/current/contributing/code/bugs.html',
                'danger'
            )
        )
    )
    ->block(new SlackDividerBlock())
    ->block((new SlackSectionBlock())
        ->text('Symfony Documentation')
        ->accessory(
            new SlackButtonBlockElement(
                'Improve Documentation',
                'https://symfony.com/doc/current/contributing/documentation/standards.html',
                'primary'
            )
        )
    );

// Add the custom options to the chat message and send the message
$chatMessage->options($slackOptions);

$chatter->send($chatMessage);

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

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Slack Notifier');

$options = (new SlackOptions())
    ->block(
        (new SlackSectionBlock())
            ->field('My **Markdown** content with clickable URL : symfony.com') // Markdown content (default)
            ->field('*Plain text content*', markdown: false) // Plain text content
            ->field('Not clickable URL : symfony.com', verbatim: true) // Only for markdown content
            ->field('Thumbs up emoji code is :thumbsup: ', emoji: false) // Only for plain text content
    );

// Add the custom options to the chat message and send the message
$chatMessage->options($options);

$chatter->send($chatMessage);

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

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

// Add the custom options to the chat message and send the message
$chatMessage->options($options);

$chatter->send($chatMessage);

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

use Symfony\Component\Notifier\Bridge\Slack\SlackSentMessage;
use Symfony\Component\Notifier\Message\ChatMessage;

$sentMessage = $chatter->send(new ChatMessage('Original message'));

// Make sure that Slack transport was used
if ($sentMessage instanceOf SlackSentMessage) {
    $messageId = $sentMessage->getMessageId();
    $channelId = $sentMessage->getChannelId();
}

use Symfony\Component\Notifier\Bridge\Slack\UpdateMessageSlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$options = new UpdateMessageSlackOptions($channelId, $messageId);
$chatter->send(new ChatMessage('Updated message', $options));

use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$options = (new SlackOptions())->postAt(new \DateTime('+1 day'));

$chatMessage = new ChatMessage('Symfony Feature');
$chatMessage->options($options);

$chatter->send($chatMessage);