laravel-notification-channels / microsoft-teams

Microsoft Teams Notifications Channel for Laravel
https://laravel-notification-channels.com
MIT License
132 stars 15 forks source link

Microsoft Teams Notifications Channel for Laravel

Latest Version on Packagist Software License Total Downloads

This package makes it easy to send notifications using Microsoft Teams with Laravel 5.5+, 6.x, 7.x, 8.x, 9.x, 10.x and 11.x

return MicrosoftTeamsMessage::create()
    ->to(config('services.microsoft_teams.sales_url'))
    ->type('success')
    ->title('Subscription Created')
    ->content('Yey, you got a **new subscription**. Maybe you want to contact him if he needs any support?')
    ->button('Check User', 'https://foo.bar/users/123');

Contents

Installation

You can install the package via composer:

composer require laravel-notification-channels/microsoft-teams

Next, if you're using Laravel without auto-discovery, add the service provider to config/app.php:

'providers' => [
    // ...
    NotificationChannels\MicrosoftTeams\MicrosoftTeamsServiceProvider::class,
],

Setting up the Connector

Please check out this for setting up and adding a webhook connector to your Team's channel. Basic Markdown is supported, please also check out the message card reference article which goes in more detail about the do's and don'ts.

Setting up the MicrosoftTeams service

Then, configure your webhook url:

Add the following code to your config/services.php:

// config/services.php
...
'microsoft_teams' => [
    'webhook_url' => env('TEAMS_WEBHOOK_URL'),
],
...

You can also add multiple webhooks if you have multiple teams or channels, it's up to you.

// config/services.php
...
'microsoft_teams' => [
    'sales_url' => env('TEAMS_SALES_WEBHOOK_URL'),
    'dev_url' => env('TEAMS_DEV_WEBHOOK_URL'),
],
...

Usage

Now you can use the channel in your via() method inside the notification:

use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

class SubscriptionCreated extends Notification
{
    public function via($notifiable)
    {
        return [MicrosoftTeamsChannel::class];
    }

    public function toMicrosoftTeams($notifiable)
    {
        return MicrosoftTeamsMessage::create()
            ->to(config('services.microsoft_teams.sales_url'))
            ->type('success')
            ->title('Subscription Created')
            ->content('Yey, you got a **new subscription**. Maybe you want to contact him if he needs any support?')
            ->button('Check User', 'https://foo.bar/users/123');
    }
}

Instead of adding the to($url) method for the recipient you can also add the routeNotificationForMicrosoftTeams method inside your Notifiable model. This method needs to return the webhook url.

public function routeNotificationForMicrosoftTeams(Notification $notification)
{
    return config('services.microsoft_teams.sales_url');
}

On-Demand Notification Usage

To use on demand notifications you can use the route method on the Notification facade.

Notification::route(MicrosoftTeamsChannel::class,null)
    ->notify(new SubscriptionCreated());

Available Message methods

Sections

It is possible to define one or many sections inside a message card. The following methods can be used within a section

Additionally the title, content, button and action can be also added to a section through the optional params value:

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email tobias.madner@gmx.at instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.