PHP code example of agencelex / notifications-slack

1. Go to this page and download the library: Download agencelex/notifications-slack 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/ */

    

agencelex / notifications-slack example snippets


$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['notifications_slack'] = [
    'botToken' => getenv('SLACK_BOT_TOKEN') ?: '',
    'webhooks' => [
        'monitoring' => getenv('SLACK_WEBHOOK_MONITORING') ?: '',
    ],
    'apiEndpoint' => 'https://slack.com/api', // GovSlack: https://slack-gov.com/api
    'timeout' => 10,
    'connectTimeout' => 5,
];

use Lex\Notifications\Slack\Extension;
use Lex\Notifications\Notification;
use Lex\Notifications\NotificationManager;
use Lex\Notifications\Slack\Configuration\SlackRoute;
use Lex\Notifications\Slack\Message\SlackMessage;

final class JobFinished extends Notification
{
    public function via(object $notifiable): array { return [Extension::CHANNEL_NAME]; }

    public function toSlack(object $notifiable): SlackMessage
    {
        return SlackMessage::text(':white_check_mark: *Job finished*');
    }
}

// Inject NotificationDispatcherInterface using TYPO3 DI:
$dispatcher->route(Extension::CHANNEL_NAME, SlackRoute::webhookNamed('monitoring'))->notify(new JobFinished());
$dispatcher->route(Extension::CHANNEL_NAME, SlackRoute::api('C0123456789'))->notify(new JobFinished());
// shorthand:
$dispatcher->route(Extension::CHANNEL_NAME, 'https://hooks.slack.com/services/...')->notify(new JobFinished());
$dispatcher->route(Extension::CHANNEL_NAME, 'webhook:monitoring')->notify(new JobFinished());

use Lex\Notifications\Domain\Model\Ability\Notifiable;
use Lex\Notifications\Notification;
use Lex\Notifications\Slack\Configuration\SlackRoute;
use Lex\Notifications\Slack\Domain\Model\Ability\HasRouteNotificationForSlack;

final class MyRecipient
{
    use Notifiable, HasRouteNotificationForSlack;

    public function routeNotificationForSlack(?Notification $notification = null): SlackRoute|string|null
    {
        return SlackRoute::webhookNamed('monitoring'); // Or any logic to determine the route
    }
}

(new MyRecipient())->notify(new JobFinished());

$builderJson = <<<'JSON'
{"blocks":[{"type":"section","text":{"type":"mrkdwn","text":"*TYPO3 alert*"}}]}
JSON;

$message = SlackMessage::fromJson($builderJson)->withText('TYPO3 alert');
// Bare JSON arrays of blocks are supported as well.

use SlackPhp\BlockKit\Kit;

$message = SlackMessage::fromBlockKit(
    Kit::message(blocks: [Kit::section('*TYPO3 alert*'), Kit::divider()])
)->withText('TYPO3 alert');

$message = SlackMessage::text('Alert')->withAttachments([
    ['fallback' => 'Production issue', 'color' => '#D92D20', 'title' => 'Alert', 'text' => 'Investigate TYPO3'],
]);

$message = SlackMessage::text('Update')->inThread('1729000000.123456')->withoutUnfurling();