dunp /smsend-laravel

一个提供使用Smsend.it API发送SMS的库的软件包。该软件包旨在简化Laravel应用程序中发送SMS的集成。

安装: 6

依赖: 0

建议: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:laravel-package

1.2 2023-06-27 12:36 UTC

This package is auto-updated.

Last update: 2024-09-27 15:21:34 UTC


README

Smsend是一个Laravel软件包,旨在简化通过意大利供应商smsend.it发送SMS。通过这个软件包,开发者可以轻松地用几行代码将短信发送集成到他们的Laravel应用程序中。

兼容性

该软件包与Laravel的9.x和10.x版本兼容。

安装和配置

要安装和配置Smsend Laravel软件包,请按照以下步骤操作。

通过Composer安装

要安装软件包,请使用Composer执行以下命令:

composer require dunp/smsend-laravel

注册服务提供者

安装软件包后,需要在Laravel中注册服务提供者。检查config/app.php文件下的"providers"部分是否已存在该提供者。如果不存在,请将以下提供者添加到提供者列表中

Dunp\Smsend\SmsendServiceProvider::class,

发布配置文件

要发布配置文件,请执行以下命令:

php artisan vendor:publish --provider="Dunp\Smsend\SmsendServiceProvider"

这会将配置文件smsend.php复制到你的项目的config目录。

如果遇到vendor:publish命令的问题,你可以手动将配置文件从vendor/dunp/smsend-laravel/config复制到你的项目的config目录。

smsend.php配置文件设置

在配置文件中,你可以修改以下设置

输入凭证

要使用Smsend软件包,你需要在你的项目.env文件中输入你的SmSend账户凭证和密钥。打开.env文件,并添加以下行

SMSEND_USERNAME=tuo_username
SMSEND_PASSWORD=tua_password
SMSEND_SECRET_KEY=base64:chiave_segreta 

用你选择的密钥替换"chiave_segreta"。
用你的SmSend用户名替换"tuo_username",用Smsend密码替换"tua_password"。
如果你还没有SmSend账户,你可以在其网站上免费创建一个:smsend.it

输入的凭证将被用于在发送短信时验证SmSend服务。

使用方法

Smsend软件包提供两种类型的短信:普通文本和参数化文本。
在第一种情况下,文本消息对所有接收者都是一致的,而在第二种情况下,你可以使用参数化变量来自定义它。

预设通知

预设通知是使用Smsend Laravel软件包的一种简单有效的方法。它们作为四个现成可用的类实现:SingleSmsendNotification、SingleParamSmsendNotification、MultiSmsendNotification和MultiParamSmsendNotification,可以扩展以自定义发送消息。
要使用Laravel的通知系统,需要在将使用通知的Model上输入"Notifiable"特质。例如,如果我们想将通知应用于User Model,我们需要添加以下代码

namespace App\Models;
...
use Illuminate\Notifications\Notifiable;
...
class User
{
    use Notifiable;
    
    ...

}

此外,如果需要,您可以通过将setColumnNumber()方法添加到通知类中来自定义特定通知使用的电话号码列,该方法用于设置电话号码列。这样,您可以覆盖配置文件smsend.php中包含的全局设置。

namespace Dunp\Smsend\Notifications;

use Dunp\Smsend\SmsendMessage;

class LaMiaNotifica extends SmsendNotification
{
...
    public function setColumnNumber(): string
    {
        return 'numero_telefono';
    }
...
}

单条消息发送

对于发送单条消息,使用继承自抽象类SmsendNotification的SingleSmsendNotification类。

要使用这个类,需要在app/Notifications目录下创建一个名为SingleSmsNotification.php的文件,并定义一个名为SingleSmsNotification的类,该类继承自SingleSmsendNotification。在这个类中,您需要定义一个setContentMessage方法,该方法返回要发送的消息内容。

SingleSmsNotification.php示例

namespace App\Notifications;

use Dunp\Smsend\Notifications\SingleSmsendNotification;

class SingleSmsNotification extends SingleSmsendNotification
{
   public function setContentMessage(): string
   {
       return 'Questo è un messaggio di prova singolo.';
   }
}

然后,要向特定用户发送通知,在控制器中可以使用以下代码

use App\Models\User;
use App\Notifications\SingleSmsNotification;
...
$user = User::find(1);
$user->notify(new SingleSmsNotification());

多条消息发送

要通过预设通知使用多条消息发送方法,需要在app/Notifications目录下创建一个名为MultiSmsNotification.php的文件。在这个文件中,您需要扩展Smsend包中的MultiSmsendNotification类,并实现setContentMessage方法来设置消息内容。

namespace App\Notifications;

use Dunp\Smsend\Notifications\MultiSmsendNotification;

class MultiSmsNotification extends MultiSmsendNotification
{
   public function setContentMessage(): string
   {
       return 'Questo è un messaggio di prova multiplo.';
   }
}

MultiSmsendNotification类期望一个接收者集合,以便通过Smsend API的单个HTTP请求向多个用户发送多条消息。

在控制器中,可以使用Laravel的Notification::send方法来向接收者发送通知。

use App\Models\User;
use App\Notifications\MultiSmsNotification;
use Illuminate\Support\Facades\Notification;
...
$users = User::all();
Notification::send(null, new MultiSmsNotification($users));

在这个例子中,我们想要向数据库中的所有用户发送短信。为此,从数据库中选择所有用户,并将它们作为一个集合传递给MultiSmsNotification类。在MultiSmsNotification类的setContentMessage方法中设置短信内容。

带参数的单条消息发送

如果您想为每个用户发送带有自定义参数的短信,则需要创建一个继承自SingleParamSmsendNotification的类。这个类需要实现setContentMessage方法,在方法内部可以使用${nomevariabile}语法来插入自定义变量。

例如,在app/Notifications目录下创建一个名为SingleParamSmsNotification.php的文件

namespace App\Notifications;

use Dunp\Smsend\Notifications\SingleParamSmsendNotification;

class SingleParamSmsNotification extends SingleParamSmsendNotification
{
   public function setContentMessage(): string
   {
       return 'Ciao ${name}, questo un messaggio di prova singolo.';
   }
}

在我们的例子中,我们使用了${name}变量,它必须包含在User对象中。

为了在控制器中向特定用户发送通知,我们可以插入以下代码

use App\Models\User;
use App\Notifications\SingleParamSmsNotification;
...
$user = User::find(1);
$user->notify(new SingleParamSmsNotification());

这样,用户将收到包含其姓名的消息。

带参数的多条消息发送

带参数的多条消息发送允许使用参数变量来为每个接收者自定义文本消息。

要创建这种类型的通知,在您的Laravel项目app/Notifications目录下创建一个名为MultiParamSmsNotification.php的文件,并插入以下代码

namespace App\Notifications;

use Dunp\Smsend\Notifications\MultiParamSmsendNotification;

class MultiParamSmsNotification extends MultiParamSmsendNotification
{
   public function setContentMessage(): string
   {
       return 'Ciao ${name}, questo un messaggio di prova multiplo.';
   }
}

在这个例子中,使用了${name}变量来为每个接收者自定义消息。请确保使用的变量在User对象中存在。

为了向所有用户发送通知,在您的控制器中插入以下代码

use App\Models\User;
use App\Notifications\MultiParamSmsNotification;
use Illuminate\Support\Facades\Notification;
...
$users = User::all();
Notification::send(null, new MultiParamSmsNotification($users));

这样,消息将为每个接收者使用在MultiParamSmsNotification类中指定的参数变量进行个性化。

自定义通知

如果预设通知不足以满足您的需求,您可以扩展SmsendNotification类并设置toSms()方法来自定义通知。

SmsendNotification类还提供了之前看到的方法,如setContentMessage()和setColumnNumber()。默认情况下,SmsendNotification类在属性$message中实例化一个SmsendMessage对象,您可以利用SmsendMessage对象提供的各种方法来根据您的需求自定义通知。

toSms()方法使用由SmsendNotification类实例化的$message对象来通过使用SmsendMessage对象提供的各种方法来个性化通知。

namespace Dunp\Smsend\Notifications;

use Dunp\Smsend\SmsendMessage;

class LaMiaNotificaPersonalizzata extends SmsendNotification
{
    public function toSms($notifiable): SmsendMessage
    {
        return $this->message
	    ->quality('N')
	    ->content('Questo è il mio messaggio.')
	    ->from('NomeAzienda')
	    ->scheduledAt('20230331121020')
	    ->to($notifiable);
    }
}

传递给 toSms 方法的对象,即 $notifiable,代表使用 Laravel 通知系统所针对的对象。通常情况下,这是一个关联到通知接收者的 Eloquent 模型实例。

$user = User::find(1);
$user->notify(new LaMiaNotificaPersonalizzata());

$notifiable 正是 $user,其中包含 $user->phone_number 以及用于发送的其它参数属性。

此外,您可以在创建通知类实例时直接设置 content、quality、from 和 scheduled_at 的值,以更快地发送通知,而无需在 toSms 方法内定义。以下是一个示例:

// Controller
$user = User::find(1);
$user->notify(new LaMiaNotificaPersonalizzata([
    'content' => 'Questo è il mio messaggio.',
    'quality' => 'N',
    'from' => 'NomeAzienda',
    'scheduledAt' => '20230331121020'
]);

//Notifica
public function toSms($notifiable): SmsendMessage
    {
        return $this->message
	    ->to($notifiable);
    }

为了创建用于批量发送的通知,需要在您的通知类中定义一个构造函数来创建一个 Collection 属性,因为与单次发送不同,我们需要一个对象集合而不是单个对象。这样就可以将用户集合传递给通知,并将其用作批量发送的接收者。

namespace Dunp\Smsend\Notifications;

use Dunp\Smsend\SmsendMessage;
use Illuminate\Database\Eloquent\Collection;

class LaMiaNotificaMultipla extends SmsendNotification
{
    protected Collection $collection;

    public function __construct(Collection $collection)
    {
        parent::__construct();
        $this->collection = $collection;
    }

    public function toSms($notifiable): SmsendMessage
    {
        return $this->message
	    ->multiTo($this->collection);
    }
}

对于带参数的发送,构造逻辑是相同的,区别在于定义接收者的方法。对于单次发送,使用 toParametric(object $recipient) 方法;对于批量发送,使用 multiToParametric(Collection $recipients) 方法。

您也可以仅使用单次发送来执行批量发送,但这种方法并不方便,因为它会对 Smsend API 进行每次发送的 HTTP 请求,而不是像前面描述的批量发送那样进行单次 HTTP 请求。

$users = User::all();
Notification::send($users, new SingleSmsendNotification());

这种方法不太方便,因为每次发送都会对 Smsend API 进行 HTTP 请求,而前面描述的批量发送则仅进行一次 HTTP 请求,从而减轻了服务器的负载。

许可

本软件包在 MIT 许可下发布。有关详细信息,请参阅 许可文件