symfony/rocket-chat-notifier
Symfony Notifier bridge for Rocket.Chat. Configure a rocketchat:// DSN with incoming webhook token and default channel, then send ChatMessages. Supports custom payload (alias/avatar/channel overrides) and multiple attachments for rich messages.
Install the Package
Add to composer.json:
composer require symfony/rocket-chat-notifier
For Laravel, ensure compatibility with Symfony’s HTTP client (Laravel 10+ works natively).
Configure DSN
Add to .env:
ROCKETCHAT_DSN=rocketchat://ACCESS_TOKEN@your-rocketchat-host?channel=general
ACCESS_TOKEN with your RocketChat webhook token (URL-encoded if needed).your-rocketchat-host with your RocketChat instance domain (e.g., rocketchat.example.com).channel is optional but recommended for default routing.First Notification Use Symfony’s Notifier in a Laravel controller or command:
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
public function sendAlert(NotifierInterface $notifier)
{
$message = new ChatMessage('Deployment failed! Check logs.');
$notifier->send($message);
}
Register the notifier in Laravel’s service container (see Implementation Patterns).
$notifier = app(NotifierInterface::class);
$message = new ChatMessage('System maintenance in 10 mins.');
$notifier->send($message);
AppServiceProvider:
public function register()
{
$this->app->bind(NotifierInterface::class, function ($app) {
$dsn = getenv('ROCKETCHAT_DSN');
$transport = new RocketChatTransport($dsn);
return new Notifier([$transport]);
});
}
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatOptions;
$payload = [
'alias' => 'Laravel Monitor',
'emoji' => ':robot_face:',
'channel' => '#alerts', // Overrides DSN channel
];
$attachment = [
'title' => 'Database Error',
'text' => 'Query timeout at 2023-10-05T12:34:56Z',
'color' => '#ff0000',
];
$options = new RocketChatOptions($attachment, $payload);
$message = new ChatMessage('Critical DB Issue!', $options);
$notifier->send($message);
OrderShipped, UserRegistered).// Listen to an event in EventServiceProvider
public function boot()
{
OrderShipped::listen(function ($order) {
$notifier = app(NotifierInterface::class);
$message = new ChatMessage(
"Order #{$order->id} shipped to {$order->customer_email}",
new RocketChatOptions(null, [
'channel' => '#orders',
'alias' => 'Order Bot',
])
);
$notifier->send($message);
});
}
$channel = auth()->user()->role === 'admin' ? '#admin-alerts' : '#team-alerts';
$payload = ['channel' => $channel];
$message = new ChatMessage('New login detected.', new RocketChatOptions(null, $payload));
$notifier->send($message);
// Dispatch a job
SendRocketChatNotification::dispatch(
'Server restarting now',
'#ops',
['alias' => 'Server Bot']
);
// Job class
class SendRocketChatNotification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
public function handle(NotifierInterface $notifier)
{
$message = new ChatMessage($this->message, new RocketChatOptions(null, $this->payload));
$notifier->send($message);
}
}
DSN Configuration
Use Laravel’s config() to centralize DSN settings:
// config/rocket_chat.php
return [
'dsn' => env('ROCKETCHAT_DSN'),
'default_channel' => env('ROCKETCHAT_CHANNEL', 'general'),
];
Then reference in your notifier binding:
$dsn = config('rocket_chat.dsn');
HTTP Client Customization If you need to customize the HTTP client (e.g., add headers, timeouts), extend the transport:
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransport;
$dsn = Dsn::fromEnvironment('ROCKETCHAT_DSN');
$client = new Client([
'headers' => ['X-Custom-Header' => 'value'],
'timeout' => 10,
]);
$transport = new RocketChatTransport($dsn, $client);
Testing Mock the notifier in tests:
$notifier = $this->createMock(NotifierInterface::class);
$notifier->expects($this->once())
->method('send')
->with($this->isInstanceOf(ChatMessage::class));
Message Templates Create reusable message templates in a service:
class RocketChatNotifierService
{
public function sendDeploymentAlert(string $status, string $commit)
{
$payload = [
'alias' => 'CI/CD Bot',
'emoji' => $status === 'success' ? ':white_check_mark:' : ':x:',
'channel' => '#devops',
];
$attachment = [
'title' => 'Deployment Alert',
'text' => "Status: {$status}\nCommit: {$commit}",
'color' => $status === 'success' ? '#00ff00' : '#ff0000',
];
$message = new ChatMessage('', new RocketChatOptions($attachment, $payload));
app(NotifierInterface::class)->send($message);
}
}
Webhook Scripts For complex payloads, deploy a RocketChat Incoming Webhook Script to process raw data:
// RocketChat script (deploy via Admin Panel)
class Script {
process_incoming_request({ request }) {
const { content, attachments } = request;
return {
text: `Custom Processed: ${content}`,
attachments: attachments.map(attach => ({
...attach,
footer: 'Processed by RocketChat Script',
})),
};
}
}
Then send raw JSON payloads from Laravel:
$payload = [
'content' => 'Raw data: {"key": "value"}',
'attachments' => [/* ... */],
];
$message = new ChatMessage('', new RocketChatOptions(null, $payload));
DSN Format Errors
@ or ?channel).rocketchat://TOKEN@HOST?channel=CHANNEL
For webhook URLs (e.g., https://host/hooks/...), encode slashes:
rocketchat://TOKEN%2FHOOK_ID@host?channel=CHANNEL
Dsn class for validation errors.Channel Permissions
bot role with post permissions).Payload Overrides
text and attachments in the payload are overridden by ChatMessage content and RocketChatOptions attachments.alias, emoji) and pass data via `ChatMessageHow can I help you explore Laravel packages today?