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/ */
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.