Laravel Teams Notification is a package for sending notifications to Microsoft Teams using the "Post to a channel when a webhook request is received" workflow webhook. It supports sending normal messages, exception messages with trace, and messages with additional details or JSON blocks, following the JSON structure required by Microsoft Teams adaptive cards. The package also includes custom logging functionality for Laravel, making it easy to integrate with your existing Laravel applications and log important events directly to Microsoft Teams channels.
To install the package, you need PHP 7.0 or higher and Laravel 5.5 or later. Use Composer:
composer require osa-eg/laravel-teams-notification
Then, add your Microsoft Teams webhook URL to your .env
file:
TEAMS_WEBHOOK_URL=your_teams_webhook_url
To publish the config file included with this package to your Laravel project, run:
php artisan vendor:publish --tag=laravel-teams-notification-config
To send a normal message, use the sendMessage
method:
use Osama\LaravelTeamsNotification\TeamsNotification;
$notification = new TeamsNotification();
$message = "System Notification";
$notification->sendMessage($message);
To send a normal message with additional details, use the sendMessage
method with the second parameter:
use Osama\LaravelTeamsNotification\TeamsNotification;
$notification = new TeamsNotification();
$message = "System Notification";
$details = [
'Server' => 'Production',
'Status' => 'Running',
'Uptime' => '24 days'
];
$notification->sendMessage($message, $details);
To send a success message, use the success
method:
use Osama\LaravelTeamsNotification\TeamsNotification;
$notification = new TeamsNotification();
$message = "Operation completed successfully!";
$details = [
'Duration' => '2 seconds',
'Processed Items' => '150'
];
$notification->success()->sendMessage($message, $details);
To send a warning message, use the warning
method:
use Osama\LaravelTeamsNotification\TeamsNotification;
$notification = new TeamsNotification();
$message = "Warning: High Memory Usage Detected";
$details = [
'Memory Usage' => '95%',
'Server' => 'Production'
];
$notification->warning()->sendMessage($message, $details);
To send an error message with trace, use the error
method and bindTrace
method:
use Osama\LaravelTeamsNotification\TeamsNotification;
try {
// Code that may throw an exception
} catch (\Exception $exception) {
$notification = new TeamsNotification();
$notification->bindTrace()->error()->sendException($exception);
}
To send a message with an array as a JSON block, use the sendJsonMessage
method:
use Osama\LaravelTeamsNotification\TeamsNotification;
$notification = new TeamsNotification();
$message = "Data Update";
$data = [
'user_id' => 12345,
'action' => 'update',
'status' => 'success',
'timestamp' => date('Y-m-d H:i:s')
];
$notification->success()->sendJsonMessage($message, $data);
The package also supports custom logging to Microsoft Teams. To set up custom logging, follow these steps:
Configure Logging in Your Laravel Project:
In config/logging.php
, add the following configuration:
'channels' => [
// Other channels...
'teams' => [
'driver' => 'custom',
'via' => \Osama\LaravelTeamsNotification\Logging\TeamsLoggingChannel::class,
'webhook_url' => env('TEAMS_WEBHOOK_URL'),
],
Use the Custom Log Channel:
To log messages to Teams, use the teams
log channel:
Log::channel('teams')->info('This is an info message');
Log::channel('teams')->error('This is an error message');
For a detailed guide on integrating Microsoft Teams notifications with your Laravel application, check out my Medium article:
Streamlining Laravel Notifications with Microsoft Teams Workflow Integration
This package is open-sourced software licensed under the MIT license.
This README now includes a Table of Contents section that links to different parts of the document for easier navigation.