PHP code example of spatie / laravel-slack-alerts

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

    

spatie / laravel-slack-alerts example snippets


use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message("You have a new subscriber to the {$newsletter->name} newsletter!");

return [
    /*
     * The webhook URLs that we'll use to send a message to Slack.
     */
    'webhook_urls' => [
        'default' => env('SLACK_ALERT_WEBHOOK'),
    ],

    /*
     * This job will send the message to Slack. You can extend this
     * job to set timeouts, retries, etc...
     */
    'job' => Spatie\SlackAlerts\Jobs\SendToSlackChannelJob::class,
    'queue' => env('SLACK_ALERT_QUEUE', 'default'),
];


SlackAlert::message("You have a new subscriber to the {$newsletter->name} newsletter!");

SlackAlert::blocks([
    [
        "type" => "section",
        "text" => [
        "type" => "mrkdwn",
            "text" => "You have a new subscriber to the {$newsletter->name} newsletter!"
        ]
    ]
]);

// in config/slack-alerts.php

'webhook_urls' => [
    'default' => 'https://hooks.slack.com/services/XXXXXX',
    'marketing' => 'https://hooks.slack.com/services/YYYYYY',
],

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::to('marketing')->message("You have a new subscriber to the {$newsletter->name} newsletter!");

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::to('https://custom-url.com')->message("You have a new subscriber to the {$newsletter->name} newsletter!");

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::toChannel('subscription_alerts')->message("You have a new subscriber to the {$newsletter->name} newsletter!");

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::onQueue('some-queue')->message("Some message.");

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message("A message *with some bold statements* and _some italicized text_.");

SlackAlert::message("<https://spatie.be|This is a link to our homepage>");

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message(":smile: :custom-code:");


use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::message("A message that notifies <@username> and everyone else who is <!here>")


use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::withIconURL('https://example.com/tiny-icon.jpg')->message("Some message.");

use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::withUsername('More Descriptive Name')->message("Some message.");

// in a test

use Spatie\SlackAlerts\Facades\SlackAlert;

it('will send an alert to Slack', function() {

    SlackAlert::shouldReceive('message')->once();
    
    // execute code here that does send a message to Slack
});

// in a test

use Spatie\SlackAlerts\Facades\SlackAlert;

it('will not send an alert to Slack', function() {
    SlackAlert::shouldReceive('message')->never();
    
    // execute code here that doesn't send a message to Slack
});
bash
php artisan vendor:publish --tag="slack-alerts-config"