Skip to content

About

Fluent Slack Block Kit notification builder for Laravel with prebuilt templates

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slack Alerts

A fluent Slack notification builder for Laravel using Block Kit. Route alerts to named channels, customize bot appearance, and use prebuilt templates for common notifications.

Installation

composer require turtlebytes/slack-alerts

The service provider and facade are auto-discovered by Laravel.

Publish the config

php artisan vendor:publish --provider="TurtleBytes\SlackAlerts\SlackAlertsServiceProvider"

Set your webhook

Add your Slack Incoming Webhook URL to .env:

SLACK_ALERT_WEBHOOK=https://hooks.slack.com/services/T00/B00/xxxx

Usage

Basic Alert

use TurtleBytes\SlackAlerts\SlackAlert;

SlackAlert::make()
    ->header('New Signup')
    ->body('*Jane Doe* just created an account.')
    ->send();

Full Example

SlackAlert::make()
    ->header('Payment Received')
    ->fields([
        'Customer' => 'Jane Doe',
        'Amount'   => '$49.00',
        'Plan'     => 'Pro Monthly',
    ])
    ->body('Stripe charge `ch_1234` completed successfully.')
    ->divider()
    ->code('stripe.charges.create({ amount: 4900 })', 'API Call')
    ->action('View in Stripe', 'https://dashboard.stripe.com/payments/ch_1234')
    ->action('View Customer', 'https://app.example.com/customers/42')
    ->context('IP: 192.168.1.1 | 2026-02-17 14:30:00')
    ->send();

Fluent API

Method Description
header(string $text) Add a header block (plain text)
body(string $markdown) Add a markdown text section
fields(array $pairs) Side-by-side key-value fields
code(string $content, ?string $label) Code block with optional label
context(string $text) Small muted footer text
action(string $label, string $url, string $style) Button ('primary' or 'danger')
divider() Horizontal rule between sections
block(array $block) Raw Block Kit block (escape hatch)
text(string $text) Explicit fallback text for notifications
username(string $name) Override bot display name
icon(string $emoji) Override bot icon (e.g. ':rocket:')
to(string $url) Send to a specific webhook URL
toChannel(string $name) Send to a named channel from config
send() Send the message. Returns true/false.
toPayload() Get the payload array without sending

Named Channels

Configure multiple webhook URLs in config/slack-alerts.php:

'channels' => [
    'errors'   => env('SLACK_ALERT_ERRORS_WEBHOOK'),
    'signups'  => env('SLACK_ALERT_SIGNUPS_WEBHOOK'),
    'payments' => env('SLACK_ALERT_PAYMENTS_WEBHOOK'),
],

Then route alerts:

SlackAlert::make()
    ->toChannel('errors')
    ->header('500 Error')
    ->body('Something broke.')
    ->send();

If a named channel isn't configured, it falls back to the default webhook.

Prebuilt Templates

Three ready-made templates for common use cases:

Contact Form Received

use TurtleBytes\SlackAlerts\Templates\ContactReceived;

ContactReceived::send(
    name: 'Jane Doe',
    email: '[email protected]',
    message: 'I have a question about your services.',
    subject: 'General Inquiry',
    ipAddress: '192.168.1.1',
    adminUrl: 'https://app.example.com/admin/contacts/42',
    channel: 'activity',
);

Error Occurred

use TurtleBytes\SlackAlerts\Templates\ErrorOccurred;

ErrorOccurred::send(
    message: 'SQLSTATE[23000]: Integrity constraint violation',
    url: '/api/users',
    trace: $exception->getTraceAsString(),
    channel: 'errors',
);

Spam Detected

use TurtleBytes\SlackAlerts\Templates\SpamDetected;

SpamDetected::send(
    name: 'Spammer McSpamface',
    email: '[email protected]',
    score: 85,
    reasons: ['Disposable email domain', 'Contains spam keywords'],
    channel: 'activity',
);

Using the Facade

use TurtleBytes\SlackAlerts\Facades\SlackAlert;

SlackAlert::header('Deploy Complete')
    ->body('Version *v2.1.0* deployed to production.')
    ->username('Deploy Bot')
    ->icon(':rocket:')
    ->send();

Configuration

Key Env Variable Default Description
default_webhook SLACK_ALERT_WEBHOOK null Default webhook URL
channels.* SLACK_ALERT_*_WEBHOOK null Named channel webhooks
username SLACK_ALERT_USERNAME 'Alert Bot' Bot display name
icon_emoji SLACK_ALERT_ICON ':robot_face:' Bot icon emoji
timeout — 5 HTTP timeout (seconds)
retries — 2 Retry attempts on failure

Testing

composer test

License

MIT

About

Fluent Slack Block Kit notification builder for Laravel with prebuilt templates

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages