talvbansal/laravel-ms-teams-notification-channel

Basic MS Teams Notifications Channel for Laravel

1.2.0 2022-06-03 15:41 UTC

This package is auto-updated.

Last update: 2024-12-20 14:19:45 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

This package makes it easy to send notifications using MS Teams with Laravel 5.5+ - 8.0.

Contents

Installation

You can install the package via composer:

composer require talvbansal/laravel-ms-teams-notification-channel

Setting up the Connector

Please refer to this article for setting up and adding a webhook connector to your MS Team's channel.

Then, configure your webhook url:

// config/services.php
...
'ms-teams' => [
    'webhook_url' => env('MS_TEAMS_WEBHOOK_URL', 'WEBHOOK URL HERE')
],
...

You can change this to be whatever you like so if you have multiple teams you want to send notifications to you could do the following:

// config/services.php
...
'ms-teams' => [
    'developers_webhook_url' => env('MS_TEAMS_DEVELOPERS_WEBHOOK_URL'),
    'helpdesk_webhook_url' => env('MS_TEAMS_HELPDESK_WEBHOOK_URL'),
],
...

As long as you remember to route the notifications to the correct team.

Usage

You can now use the channel in your via() method inside the Notification class.

Notifications

use NotificationChannels\MsTeams\MsTeamsChannel;
use NotificationChannels\MsTeams\MsTeamsMessage;
use Illuminate\Notifications\Notification;

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

    public function toMsTeams($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        return MsTeamsMessage::create()
            // Optional recipient user id.
            ->to(config('services.ms-teams.webhook_url'))
            // Markdown supported.
            ->content("Hello there!\nYour invoice has been *PAID*")
            // (Optional) Inline Buttons
            ->button('View Invoice', $url)
            ->button('Download Invoice', $url)
            // (Optional) Supporting images
            ->image('https://source.unsplash.com/random/800x800?animals,nature&q='.now())
            ->image('https://source.unsplash.com/random/900x600?building,car&q='.now());
    }
}

Routing the message

You can either send the notification by providing with the webhook url to the recipient to the to($url) method like shown in the above example or add a routeNotificationForMsTeams() method in your notifiable model:

...
/**
 * Route notifications for the MS Teams channel.
 *
 * @return int
 */
public function routeNotificationForMsTeams()
{
    return config('services.ms-teams.webhook_url');
}
...

Available Message methods

  • to($webhookUrl): (string) Recipient's chat id.
  • title(''): (string) Notification title, does not support markdown.
  • content(''): (string) Notification message, supports markdown..
  • button($text, $url): (string) Adds an inline "Call to Action" button. You can add as many as you want.
  • image($url): (string) Adds an inline image from the given url. You can add as many as you want.
  • code($code): (string) Adds a code fragment to the message. You can add as many as you want.
  • type($type): (string) Change the accent of the card sent. You can choose between 'info', 'warning', 'error', 'success'

More examples and information about this package can be found here.

Throttling notifications

If you find you're receiving too many of a given notification you can use this package to help limit the number of notifications you get during a period where something has gone wrong.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email :author_email 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.

Please see this repo for instructions on how to submit a channel proposal.