peteleco / notifier

Teams notify package for Laravel.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel package to notify updated orders on Teams.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This module send notification to Microsoft Teams using a webhook. You can see the request hitting the webhook through the tests. You can clone this repo and change the config('notifier.hooks.orders.updated') to test with your hook without running docker.

Installation

You can install the package via composer:

composer require peteleco/notifier

You can publish the config file with:

php artisan vendor:publish --tag="notifier-config"

This is the contents of the published config file:

return [
  'hooks'=> [
      'orders' => [
          'updated' => env('NOTIFIER_HOOKS_ORDERS_UPDATED', 'https://webhook.site/bb7586cd-24cb-4336-b648-ceb4fd9c6609'),
          'queue' => env('NOTIFIER_HOOKS_ORDERS_queue', 'default'),
      ]
  ]
];

Usage

$notifier = new Peteleco\Notifier();
echo $notifier->sendMessage('Hello, Peteleco!');

// Or you can send using order message updated object
$message = OrderUpdatedMessage::from([
        'uuid' => \Illuminate\Support\Str::uuid(),
        'status' => 'my custom status',
        'updated_at' => \Carbon\Carbon::yesterday()->setHour(18)->setMinutes(30)->setSecond(0),
    ]);
    $request = app(
        Notifier::class,
        ['webhookUrl' => config('notifier.hooks.orders.updated')]
    )->send($message->toMessageCard());
# The toMessageCard method will format in a teams MessageCard
# https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL

Add this code to your model

    protected static function boot(): void
    {
        parent::boot();
        // Dispatch events
        static::created(function (Order $order) {
            OrderUpdated::dispatch(OrderUpdatedMessage::from([
                'uuid' => $order->getAttribute('uuid'),
                'status' => $order->load('orderStatus')->orderStatus->getAttribute('title'),
                'updated_at' => $order->getAttribute('created_at'),
            ]));
        });

        static::updated(function (Order $order) {
            if($order->wasChanged('order_status_id')) {
                OrderUpdated::dispatch(OrderUpdatedMessage::from([
                    'uuid' => $order->getAttribute('uuid'),
                    'status' =>  $order->load('orderStatus')->orderStatus->getAttribute('title'),
                    'updated_at' => $order->getAttribute('created_at'),
                ]));
            }
        });
    }

Testing

composer test

With docker

Quick setup

# run the command below to init docker
./docker/init.sh -g ${GID} -u ${UID}

# if have problem with permissions 
chmod 755 ./docker/init.sh
chmod 755 ./docker/pest.sh
chmod 755 ./docker/interact.sh
./docker/init.sh -g ${GID} -u ${UID}

Testing

./pest

Interacting with the container

./interact

# running commands inside the container
composer test

xDebug

If you want to debug, change the config mode in the docker/xdebug.ini to develop,debug

;xdebug.mode=coverage
xdebug.mode=develop,debug

Then configure your cli to use your docker machine

Setup Docker 01 Setup Docker 02

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

About

Teams notify package for Laravel.

License:MIT License


Languages

Language:PHP 82.7%Language:Shell 9.7%Language:Dockerfile 7.6%