lizhineng/dysms-notification-channel

Dysms Notification Channel for Laravel.

v2.0.0 2022-08-02 07:19 UTC

This package is auto-updated.

Last update: 2024-04-30 00:39:41 UTC


README

This package makes it easy to send notifications using Alibaba Cloud Short Message Service with Laravel 9.x, and also requires PHP 8.0+.

Contents

Installation

You can install the package via Composer:

composer require lizhineng/dysms-notification-channel

Setting up Dysms service

The package includes a configuration file. However, you are not required to export this configuration file to your own application. You can simply set the DYSMS_KEY and DYSMS_SECRET environment variables to define your Dysms API credentials which may be accessed from your Aliyun RAM dashboard.

Based on the principle of least privilege, the only required RAM permission to the credentials is dysms:SendSms, then you're good to go. Here is an example policy in case you need it:

{
    "Version": "1",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "dysms:SendSms",
            "Resource": "*"
        }
    ]
}

After defining your keys, you may set a DYSMS_SIGNATURE environment variable that defines the signature that your SMS messages should be attached to by default. You may apply your signature on Aliyun SMS Console:

DYSMS_SIGNATURE=YOUR_COMPANY_NAME

Usage

You can now use the channel in your via() method inside the notification. Note, before you can successfully send the message out, according to the Aliyun regulation, you need to apply the template to be reviewed first.

use Illuminate\Notifications\Notification;
use Zhineng\Notifications\Channels\DysmsChannel;
use Zhineng\Notifications\Messages\DysmsMessage;

class OrderPlaced extends Notification
{
    public function via($notifiable)
    {
        return ['dysms'];
    }

    public function toDysms($notifiable)
    {
        return (new DysmsMessage)
            ->using('SMS_TEMPLATE_CODE')
            ->with(['if' => 'any'])
            ->signedBy('YOUR_PERMITTED_SIGNATURE');
    }   
}

In order to let the channel know who's the recipient, add the method routeNotificationForDysms to your notifiable class.

// App\Models\User.php

public function routeNotificationForDysms($notification)
{
    return $this->phone_number;
}

Available message methods

  • using(string $templateCode): Accepts the SMS template code.
  • with(array $payload): The parameters which needs to be injected into the template.
  • signedBy(string $signature): The name of the sender, which also needs to be reviewed first.
  • serialNumber(string $serialNumber): The order number, uuid, you name it.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email im@zhineng.li 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.