PHP code example of hkwise / laravel-slack-notifier
1. Go to this page and download the library: Download hkwise/laravel-slack-notifier 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/ */
hkwise / laravel-slack-notifier example snippets
use HKWise\LaravelSlackNotifier\Facades\SlackNotifier;
// Simple webhook message
SlackNotifier::text('Hello from Laravel!')
->send();
// Send to a specific channel using Web API
SlackNotifier::via('api')
->to('#general')
->text('Hello from Laravel!')
->send();
// Send to a user
SlackNotifier::via('api')
->to('@username')
->text('Hey! Check this out.')
->send();
// Send an ephemeral message (only visible to specific user)
SlackNotifier::via('api')
->to('#general')
->toUser('U1234567890') // Slack user ID
->text('This message is only visible to you!')
->asEphemeral()
->send();
// Quick ephemeral message
SlackNotifier::quickEphemeralMessage(
'You have a pending approval request',
'U1234567890',
'#approvals'
);
// Ephemeral block message
SlackNotifier::via('api')
->to('#team')
->toUser('U1234567890')
->addHeader('Private Notification')
->addSection('This is visible only to you')
->asEphemeral()
->send();
// Quick simple message
SlackNotifier::quickMessage('Server is up and running!', '#monitoring');
// Quick block message with header
SlackNotifier::quickBlockMessage(
'Deployment Complete',
'Your application has been successfully deployed to production.',
'#deployments'
);
namespace App\Http\Controllers;
use HKWise\LaravelSlackNotifier\Facades\SlackNotifier;
class OrderController extends Controller
{
public function store(Request $request)
{
$order = Order::create($request->all());
// Notify team about new order
SlackNotifier::via('api')
->to('#orders')
->addHeader('🛍️ New Order Received')
->addSection("*Order ID:* #{$order->id}")
->addSection("*Customer:* {$order->customer_name}")
->addSection("*Total:* \${$order->total}")
->send();
return response()->json($order);
}
}
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use HKWise\LaravelSlackNotifier\Facades\SlackNotifier;
class NotifySlack implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
SlackNotifier::quickMessage('Background job completed!', '#notifications');
}
}
->addHeader('My Header')
->addSection('*Bold text* and _italic text_')
->addSection('Plain text', 'plain_text')