corean/laravel-sens

NCloud SENS 通知通道,适用于可爱的 Laravel。

v1.0.1 2024-09-04 02:34 UTC

This package is not auto-updated.

Last update: 2024-10-02 03:06:41 UTC


README

Latest Stable Version Total Downloads License Build Status

此包使用 Laravel 发送通知变得非常容易,使用 ncloud sens

我们正在开发一个非官方的 SDK 开发公共项目,以便 ncloud sens 可以在 PHP 中更灵活地使用。

您可以在此处查看项目。(https://github.com/seungmun/sens-php

官方社区

先决条件

在开始之前,您需要以下内容

  • PHP >= 7.2 (9.x 也兼容)
  • Laravel (9.x / 8.x / 7.x / 6.x)

安装

您可以通过 composer 安装此包

composer require corean/laravel-sens

包将自动注册自己。

您可以使用以下方式发布配置

php artisan vendor:publish --provider="Seungmun\Sens\SensServiceProvider" --tag="config"

此外,您也可以通过添加环境变量而不发布配置文件来使用它

SENS_ACCESS_KEY=your-sens-access-key
SENS_SECRET_KEY=your-sens-secret-key
SENS_SERVICE_ID=your-sens-service-id
SENS_ALIMTALK_SERVICE_ID=your-alimtalk-service-id
SENS_PlUS_FRIEND_ID=your-plus-friend-id

如果您想将 sms_from 值放入 .env 中

config/services.php

/*
|--------------------------------------------------------------------------
| SMS "From" Number
|--------------------------------------------------------------------------
|
| This configuration option defines the phone number that will be used as
| the "from" number for all outgoing text messages. You should provide
| the number you have already reserved within your Naver Cloud Platform
| /sens/sms-calling-number of dashboard.
|
*/
'sens' => [
    'services' => [
        'sms' => [
            'sender' => env('SENS_SMS_FROM'),
        ],
    ],
],

.env

SENS_SMS_FROM=1234567890

用法

此包可以使用 Laravel 的默认通知功能使用。

1) 请求发送 SMS
php artisan make:notification SendPurchaseReceipt
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Seungmun\Sens\Sms\SmsChannel;
use Seungmun\Sens\Sms\SmsMessage;
use Illuminate\Notifications\Notification;

class SendPurchaseReceipt extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [SmsChannel::class];
    }

    /**
     * Get the sens sms representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return SmsMessage
     */
    public function toSms($notifiable)
    {
        return (new SmsMessage)
            ->to($notifiable->phone)
            ->from('055-000-0000')
            ->content('Welcome: https://open.kakao.com/o/g3dWlf0')
            ->contentType('AD')// You can ignore it (default: COMM)
            ->type('SMS');  // You can ignore it (default: SMS)
    }
}
use App\User;
use App\Notifications\SendPurchaseReceipt;

User::find(1)->notify(new SendPurchaseReceipt);
2) 请求发送 MMS
php artisan make:notification SendPurchaseInvoice
<?php

namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Seungmun\Sens\Sms\SmsChannel;
use Seungmun\Sens\Sms\SmsMessage;
use Illuminate\Http\UploadedFile;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class SendPurchaseInvoice extends Notification
{
    use Queueable;
    
    /** @var UploadedFile */
    private $image;
    
    /**
     * Create a new notification instance.
     *
     * @param  UploadedFile  $image
     */
    public function __construct(UploadedFile $image)
    {
        $this->image = $image;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [SmsChannel::class];
    }

    /**
     * Get the sens sms representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return SmsMessage
     * @throws FileNotFoundException
     */
    public function toSms($notifiable)
    {
        return (new SmsMessage)
            ->type('MMS')
            ->to($notifiable->phone)
            ->from('055-000-0000')
            ->content('This is your invoice.\nCheck out the attached image.')
            /* file's path string or UploadedFile object of Illuminate are allowed */
            ->file('filename.jpg', $this->image);
    }
}
<?php

use App\User;
use App\Notifications\SendPurchaseReceipt;

// In this case, you should only pass UploadedFile object as a parameter.
// If when you need to pass a file path string as a parameter, change your notification class up.
User::find(1)->notify(new SendPurchaseReceipt(request()->file('image')));

现在 User id: 1 拥有自己手机属性的用户将很快收到短信或 MMS 消息。

3) 请求发送 AlimTalk
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Seungmun\Sens\AlimTalk\AlimTalkChannel;
use Seungmun\Sens\AlimTalk\AlimTalkMessage;

class SendPurchaseInvoice extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [AlimTalkChannel::class];
    }

    /**
     * Get the sens sms representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Seungmun\Sens\AlimTalk\AlimTalkMessage
     */
    public function toAlimTalk($notifiable)
    {
        return (new AlimTalkMessage())
            ->templateCode('TEMPLATE001') // required
            ->to($notifiable->phone) // required
            ->content('Evans, Your order is shipped.') //required
            ->countryCode('82') // optional
            ->addButton(['type' => 'DS', 'name' => 'Tracking of Shipment']) // optional
            ->setReserved('2020-05-31 14:20', 'Asia/Seoul'); // optional
    }
}

功能

  • SMS(LMS) 和 MMS
  • Kakao Alimtalk