PHP code example of labrodev / laravel-slack

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

    

labrodev / laravel-slack example snippets


use Labrodev\Slack\Services\SlackClient;
use Labrodev\Slack\Exceptions\SlackRequestFailed;
use Labrodev\Slack\Exceptions\SlackWebhookRevoked;

$slackClient = new SlackClient();

$blocks = [
    [
        'type' => 'section',
        'text' => [
            'type' => 'mrkdwn',
            'text' => 'A new incident was reported.',
        ],
    ],
];

try {
    $slackClient->send(
        webhookUrl: $webhookUrl,
        blocks: $blocks,
        fallbackText: 'A new incident was reported.',
    );
} catch (SlackWebhookRevoked) {
    // the webhook was deleted or revoked on Slack's side
} catch (SlackRequestFailed) {
    // any other failed request
}

use Labrodev\Slack\Support\SlackWebhookUrl;
use Labrodev\Slack\Exceptions\SlackWebhookUrlSchemeInvalid;
use Labrodev\Slack\Exceptions\SlackWebhookUrlHostInvalid;
use Labrodev\Slack\Exceptions\SlackWebhookUrlPathInvalid;

try {
    new SlackWebhookUrl()->validate($request->input('webhook_url'));
} catch (SlackWebhookUrlSchemeInvalid|SlackWebhookUrlHostInvalid|SlackWebhookUrlPathInvalid) {
    throw ValidationException::withMessages([
        'webhook_url' => 'That does not look like a Slack incoming-webhook URL.',
    ]);
}

use Labrodev\Slack\Exceptions\SlackWebhookRevoked;

try {
    $slackClient->send(webhookUrl: $channel->webhook_url, blocks: $blocks, fallbackText: $fallbackText);
} catch (SlackWebhookRevoked) {
    $channel->update(['webhook_url' => null]);
}
bash
php artisan vendor:publish --tag=slack-config