Skip to content

laravel-notification-channels/microsoft-teams

Repository files navigation

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

  • to(string $webhookUrl): Recipient's webhook url.
  • title(string $title): Title of the message.
  • summary(string $summary): Summary of the message.
  • type(string $type): Type which is used as theme color (any valid hex code or one of: primary|secondary|accent|error|info|success|warning).
  • content(string $content): Content of the message (Markdown supported).
  • button(string $text, string $url = '', array $params = []): Text and url of a button. Wrapper for an potential action.
  • action(string $text, $type = 'OpenUri', array $params = []): Text and type for a potential action. Further params can be added depending on the action. For more infos about different types check out this link.
  • options(array $options, $sectionId = null): Add additional options to pass to the message payload object.

Sections

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

  • addStartGroupToSection($sectionId = 'standard_section'): Add a startGroup property which marks the start of a logical group of information.
  • activity(string $activityImage = '', string $activityTitle = '', string $activitySubtitle = '', string $activityText = '', $sectionId = 'standard_section'): Add an activity to a section.
  • fact(string $name, string $value, $sectionId = 'standard_section'): Add a fact to a section (Supports Markdown).
  • image(string $imageUri, string $title = '', $sectionId = 'standard_section'): Add an image to a section.
  • heroImage(string $imageUri, string $title = '', $sectionId = 'standard_section'): Add a hero image to a section.

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

  • title(string $title, array $params = ['section' => 'my-section']): Title of the message and add it to my-section.
  • content(string $content, array $params = ['section' => 'my-section']): Content of the message and add it to my-section (Markdown supported).
  • button(string $text, string $url = '', array $params = ['section' => 'my-section']): Text and url of a button and add it to my-section.
  • action(string $text, $type = 'OpenUri', array $params = ['section' => 'my-section']): Text and type of an potential action and add it to my-section.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] 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.