Microsoft Teams Laravel Notification Channel
--
How to send notification using Microsoft Teams with Laravel Framework?
There is no doubt that Microsoft Teams is one of the best communication platform in business and technology industries.
Having your Laravel Application sending notification to such a platform can be a huge plus for you and your business.
In this tutorial, you will learn how to install and configure Laravel Notification Channel’s Microsoft Teams package.
Install the package via composer:
composer require laravel-notification-channels/microsoft-teams
Next, if you are using auto-discovery than you can skip this step, otherwise you must register the package in config/app.php
// config/app.php
'providers' => [
// ...
NotificationChannels\MicrosoftTeams\MicrosoftTeamsServiceProvider::class,
],
After, you will need to set up the connector ( Check this article to have more info on setting up and adding a webhook connector to your Team’s channel).
// config/services.php
...
'teams' => [
'webhook_url' => env('TEAMS_WEBHOOK_URL'),
],
...
Adding more than one webhook is also possible, like described below.
// config/services.php
...
'teams' => [
'main_url' => env('TEAMS_MAIN_WEBHOOK_URL'),
'test_url' => env('TEAMS_TEST_WEBHOOK_URL'),
],
...
Usage
The usage is fairly simple, you can use the channel in your via()
method inside the notification class.
use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
class OrderProcessed extends Notification
{
public function via($notifiable)
{
return [MicrosoftTeamsChannel::class];
}
public function toMicrosoftTeams($notifiable)
{
return MicrosoftTeamsMessage::create()
->to(config('services.teams.webhook_url'))
->type('success')
->title('Order Processed')
->content('You order has been processed successfully.')
->button('Check User', 'https://xyz.abc/users/123');
}
}
Instead of adding the
to($url)
method for the recipient you can also add therouteNotificationForMicrosoftTeams
method inside your Notifiable model. This method needs to return the webhook URL.