PHP code example of mountain / laraslack

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

    

mountain / laraslack example snippets


use ThomasMountain\LaraSlack\Facades\LaraSlack;

// Simplest possible message.
LaraSlack::send('Deploy finished');

// Same thing, with the builder.
LaraSlack::message('Deploy finished')->send();

LaraSlack::message('Deployment finished')
    ->blocks([
        [
            'type' => 'header',
            'text' => ['type' => 'plain_text', 'text' => 'Deployment finished'],
        ],
        [
            'type' => 'section',
            'fields' => [
                ['type' => 'mrkdwn', 'text' => "*Environment*\nproduction"],
                ['type' => 'mrkdwn', 'text' => "*Duration*\n1m 42s"],
            ],
        ],
    ])
    ->send();

LaraSlack::message('Nightly build failed')
    ->attachment([
        'color' => '#e01e5a',
        'title' => 'Nightly build failed',
        'text' => 'Exit code 1 on `main`',
        'footer' => 'CI',
        'ts' => time(),
    ])
    ->send();

LaraSlack::message('Disk nearly full')->webhook(config('services.slack.ops'))->send();
LaraSlack::message('Disk nearly full')->webhook(config('services.slack.dba'))->send();

LaraSlack::send([
    'text' => 'Nightly build failed',
    'unfurl_links' => false,
    'blocks' => [/* ... */],
]);

use ThomasMountain\LaraSlack\LaraSlackManager;

app(LaraSlackManager::class)->send('Deploy finished');

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use ThomasMountain\LaraSlack\Facades\LaraSlack;

protected function setUp(): void
{
    parent::setUp();

    config()->set('slack.endpoint', 'https://hooks.slack.test/services/T000/B000/XXXX');

    Http::fake(['*' => Http::response('ok')]);
}

public function test_it_alerts_ops_when_a_deploy_finishes(): void
{
    LaraSlack::message('Deploy finished')->send();

    Http::assertSent(function (Request $request) {
        $payload = json_decode($request->data()['payload'], true, flags: JSON_THROW_ON_ERROR);

        $this->assertSame('Deploy finished', $payload['text']);

        return true;
    });
}

use ThomasMountain\LaraSlack\Exceptions\SlackRequestException;

try {
    LaraSlack::message('Deploy finished')->send();
} catch (SlackRequestException $e) {
    $e->status;   // HTTP status, or null if Slack was never reached
    $e->body;     // Slack's response body, e.g. 'invalid_payload'
    $e->payload;  // what was sent
}
bash
php artisan vendor:publish --tag=laraslack-config