A Laravel Notification Channel for Microsoft Teams

v2.0.0 2025-04-26 09:49 UTC

README

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, 11.x and 12.x

Since v2 we transitioned from traditional message cards to MS Adaptive Cards. In case you need to upgrade please check out our Migration Guide.

return MicrosoftTeamsAdaptiveCard::create()
    ->to(config('services.microsoft_teams.webhook_url'))
    ->title('Subscription Created')
    ->content([
        TextBlock::create()
            ->setText('Yey, you got a **new subscription**.')
            ->setFontType('Monospace')
            ->setWeight('Bolder')
            ->setSize('ExtraLarge')
            ->setSpacing('ExtraLarge')
            ->setStyle('Heading')
            ->setHorizontalAlignment('Center')
            ->setSeparator(true),
        FactSet::create()
            ->setSpacing('ExtraLarge')
            ->setSeparator(true)
            ->setFacts([
                Fact::create()->setTitle('Subscription Created')->setValue('Today'),
            ])
    ])
    ->actions([
        ActionOpenUrl::create()
            ->setMode('Primary')
            ->setStyle('Positive')
            ->setTitle('Contact Customer')
            ->setUrl("https://www.tournamize.com"),
    ]);

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. Please also check out the adaptive card reference which goes in more detail about adaptive cards.

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 MicrosoftTeamsAdaptiveCard::create()
            ->to(config('services.microsoft_teams.webhook_url'))
            ->title('Subscription Created')
            ->content([
                TextBlock::create()
                    ->setText('Yey, you got a **new subscription**.')
                    ->setFontType('Monospace')
                    ->setWeight('Bolder')
                    ->setSize('ExtraLarge')
                    ->setSpacing('ExtraLarge')
                    ->setStyle('Heading')
                    ->setHorizontalAlignment('Center')
                    ->setSeparator(true),
                FactSet::create()
                    ->setSpacing('ExtraLarge')
                    ->setSeparator(true)
                    ->setFacts([
                        Fact::create()->setTitle('Subscription Created')->setValue('Today'),
                    ])
            ])
            ->actions([
                ActionOpenUrl::create()
                    ->setMode('Primary')
                    ->setStyle('Positive')
                    ->setTitle('Contact Customer')
                    ->setUrl("https://www.tournamize.com"),
            ]);
    }
}

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 Adaptive Card methods

  • create(): Static factory method to create a new instance of the MicrosoftTeamsAdaptiveCard.
  • to(string $webhookUrl): Sets the recipient's webhook URL. Required for sending notifications.
  • title(string $title): Sets the title of the adaptive card with appropriate text styling (heading style, bold weight, and large size).
  • content(array $contentBlocks): Adds content blocks to the adaptive card body. Accepts an array of content block objects like TextBlock, FactSet, Icon, etc.
  • actions(array $actions): Adds action buttons to the adaptive card. Accepts an array of action objects like ActionOpenUrl.
  • getWebhookUrl(): Returns the currently set webhook URL.
  • toNotGiven(): Checks if the webhook URL has been provided. Returns true if no webhook URL is set.
  • toArray(): Returns the complete payload as an associative array.

Content Block Types

You can use various content block types in the content() method:

TextBlock::create(): Creates a text block with options for:

  • setText(string $text): Sets the text content (supports markdown formatting)
  • setFontType(string $fontType): Sets the font type (e.g., 'Default', 'Monospace')
  • setWeight(string $weight): Sets text weight (e.g., 'Lighter', 'Default', 'Bolder')
  • setSize(string $size): Sets text size (e.g., 'Small', 'Default', 'Medium', 'Large', 'ExtraLarge')
  • setSpacing(string $spacing): Sets spacing around the element (e.g., 'None', 'Small', 'Default', 'Medium', 'Large', 'ExtraLarge', 'Padding')
  • setStyle(string $style): Sets text style (e.g., 'Default', 'ColumnHeader', 'Heading')
  • setHorizontalAlignment(string $alignment): Sets text alignment (e.g., 'Left', 'Center', 'Right')
  • setSeparator(bool $separator): Adds a separator line above the element when true
  • setWrap(bool $wrap): Sets whether text should wrap or not
  • setColor(string $color): Sets text color (e.g., 'Default', 'Dark', 'Light', 'Accent', 'Good', 'Warning', 'Attention')
  • setIsSubtle(bool $isSubtle): Sets whether text should be subtle or not
  • setMaximumLines(int $maxLines): Sets the maximum number of lines for the text block
  • toArray(): Returns the TextBlock properties as an array

Icon::create(): Creates an icon with options for:

  • setName(string $name): Sets the name of the icon
  • setColor(string $color): Sets icon color (e.g., 'Default', 'Dark', 'Light', 'Accent', 'Good', 'Warning', 'Attention')
  • setSpacing(string $spacing): Sets spacing around the icon (e.g., 'None', 'Small', 'Default', 'Medium', 'Large', 'ExtraLarge', 'Padding')
  • setSeparator(bool $separator): Adds a separator line above the element when true
  • setSize(string $size): Sets icon size (e.g., 'xxSmall', 'xSmall', 'Small', 'Standard', 'Medium', 'Large', 'xLarge', 'xxLarge')
  • setStyle(string $style): Sets icon style (e.g., 'Regular', 'Filled')
  • setHorizontalAlignment(string $alignment): Sets icon alignment (e.g., 'Left', 'Center', 'Right')
  • toArray(): Returns the Icon properties as an array

FactSet::create(): Creates a set of facts with options for:

  • setFacts(array $facts): Sets an array of Fact objects
  • setSpacing(string $spacing): Sets spacing around the element (e.g., 'None', 'Small', 'Default', 'Medium', 'Large', 'ExtraLarge', 'Padding')
  • setSeparator(bool $separator): Adds a separator line above the element when true
  • toArray(): Returns the FactSet properties as an array

Fact::create(): Creates a single fact (key-value pair) with methods:

  • setTitle(string $title): Sets the fact's title/key
  • setValue(string $value): Sets the fact's value
  • toArray(): Returns the Fact properties as an array

Action Types

You can use various action types in the actions() method:

ActionOpenUrl::create(): Creates a button that opens a URL with options for:

  • setTitle(string $title): Sets the button text
  • setUrl(string $url): Sets the URL to open when clicked
  • setStyle(string $style): Sets button style (e.g., 'Default', 'Positive', 'Destructive')
  • setMode(string $mode): Sets the interaction mode (e.g., 'Primary', 'Secondary')
  • toArray(): Returns the ActionOpenUrl properties as an array

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.