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"),
]);You can install the package via composer:
composer require laravel-notification-channels/microsoft-teamsNext, if you're using Laravel without auto-discovery, add the service provider to config/app.php:
'providers' => [
// ...
NotificationChannels\MicrosoftTeams\MicrosoftTeamsServiceProvider::class,
],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.
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'),
],
...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');
}To use on demand notifications you can use the route method on the Notification facade.
Notification::route(MicrosoftTeamsChannel::class,null)
->notify(new SubscriptionCreated());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).fullWidth(): Sets the adaptive card to take up the full width of the Teams channel or chat instead of the default width.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.
You can use various content block types in the content() method:
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 truesetWrap(bool $wrap): Sets whether text should wrap or notsetColor(string $color): Sets text color (e.g., 'Default', 'Dark', 'Light', 'Accent', 'Good', 'Warning', 'Attention')setIsSubtle(bool $isSubtle): Sets whether text should be subtle or notsetMaximumLines(int $maxLines): Sets the maximum number of lines for the text blocktoArray(): Returns the TextBlock properties as an array
setName(string $name): Sets the name of the iconsetColor(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 truesetSize(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
setFacts(array $facts): Sets an array of Fact objectssetSpacing(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 truetoArray(): Returns the FactSet properties as an array
setTitle(string $title): Sets the fact's title/keysetValue(string $value): Sets the fact's valuetoArray(): Returns the Fact properties as an array
You can use various action types in the actions() method:
setTitle(string $title): Sets the button textsetUrl(string $url): Sets the URL to open when clickedsetStyle(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
This library was built in my free time out of passion for the community. If you find it useful, please consider sponsoring me to help maintain and improve the project. Your support is greatly appreciated! Thank you! ❤️
Please see CHANGELOG for more information what has changed recently.
$ composer testIf you discover any security related issues, please email tobias.madner@gmx.at instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.