tzsk/sms

Laravel 短信网关集成包

维护者

详细信息

github.com/tzsk/sms

主页

源代码

问题

资助包维护!
tzsk
paypal.me/KMAhmed

安装: 107 774

依赖者: 5

建议者: 0

安全: 0

星标: 287

关注者: 12

分支: 79

开放问题: 6


README

SMS Cover

GitHub License Latest Version on Packagist GitHub Tests Action Status Total Downloads

这是一个用于短信网关集成的 Laravel 包。现在发送短信变得简单。

支持网关列表

📦 安装

通过 Composer

$ composer require tzsk/sms

⚡ 配置

发布配置文件

$ php artisan sms:publish

在配置文件中,您可以设置用于所有短信的默认驱动程序。但您也可以在运行时更改驱动程序。

选择您想用于应用程序的网关。然后将其设置为默认驱动程序,这样您就无需在每处都指定它。但,您也可以在项目中使用多个网关。

// Eg. if you want to use SNS.
'default' => 'sns',

然后在该驱动程序数组中填写该网关的凭据。

// Eg. for SNS.
'drivers' => [
    'sns' => [
        // Fill all the credentials here.
        'key' => 'Your AWS SNS Access Key',
        'secret' => 'Your AWS SNS Secret Key',
        'region' => 'Your AWS SNS Region',
        'from' => 'Your AWS SNS Sender ID', //sender
        'type' => 'Tansactional', // Or: 'Promotional'
    ],
    ...
]

Textlocal 配置

Textlocal 已默认添加。您只需更改 textlocal 驱动程序部分中的凭据。

AWS SNS 配置

如果您想使用 AWS SNS,则必须首先拉取一个 composer 库。

composer require aws/aws-sdk-php

Clockwork 配置

如果您想使用 Clockwork,则必须首先拉取一个 composer 库。

composer require mediaburst/clockworksms

Twilio 配置

如果您想使用 Twilio,则必须首先拉取一个 composer 库。

composer require twilio/sdk

然后,您只需更改 twilio 驱动程序部分中的凭据。

Melipayamak 或 Melipayamakpattern 配置

如果您想使用 Melipayamak 或 Melipayamakpattern,则必须首先拉取一个 composer 库。

composer require melipayamak/php

Kavenegar 配置

如果您想使用 Kavenegar,则必须首先拉取一个 composer 库。

composer require kavenegar/php

SMS Gateway Me 配置

如果您想使用 SMS Gateway Me,则必须首先拉取一个 composer 库。

composer require smsgatewayme/client

🔥 使用方法

在您的代码中,就像这样使用。

# On the top of the file.
use Tzsk\Sms\Facades\Sms;

////

# In your Controller.
Sms::send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']); # The numbers to send to.
});
# OR...
Sms::send("this message")->to(['Number 1', 'Number 2'])->dispatch();

# If you want to use a different driver.
Sms::via('gateway')->send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']);
});
# OR...
Sms::via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();

# Here gateway is explicit : 'twilio' or 'textlocal' or any other driver in the config.
# The numbers can be a single string as well.

# If you are not a Laravel's facade fan, you can use sms helper:

sms()->send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']); # The numbers to send to.
});

sms()->send("this message")->to(['Number 1', 'Number 2'])->dispatch();

sms()->via('gateway')->send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']);
});

sms()->via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();

# Change the from|sender|sim value with from() option:

sms()->via('gateway')->send("this message")->from('Your From Number | Sender Value | Sim Value ')->to(['Number 1', 'Number 2'])->dispatch();

# Sending argument and pattern code in pattern drivers such as melipayamakpattern and farazsmspattern.

#Note: The first argument is always known as the pattern code.

sms()->via('melipayamakpattern')->send("patterncode=123 \n arg1=name \n arg2=family")->to(['Number 1', 'Number 2'])->dispatch();

😍 通道使用

首先,您必须使用 php artisan make:notification 命令创建您的通知。然后,SmsChannel::class 可以像下面一样用作通道

namespace App\Notifications;

use Tzsk\Sms\Builder;
use Illuminate\Bus\Queueable;
use Tzsk\Sms\Channels\SmsChannel;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class InvoicePaid extends Notification
{
    use Queueable;

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

    /**
     * Get the repicients and body of the notification.
     *
     * @param  mixed  $notifiable
     * @return Builder
     */
    public function toSms($notifiable)
    {
        return (new Builder)->via('gateway') # via() is Optional
            ->send('this message')
            ->to('some number');
    }
}

提示:您可以在发送方法中使用相同的 Builder 实例。

$builder = (new Builder)->via('gateway') # via() is Optional
    ->send('this message')
    ->to('some number');

Sms::send($builder);

# OR...
$builder = (new Builder)->send('this message')
    ->to(['some number']);

Sms::via('gateway')->send($builder);

自定义驱动程序,如何操作

首先,您必须在驱动程序数组中命名您的驱动程序,并指定您想要的任何配置参数。

'drivers' => [
    'textlocal' => [...],
    'twilio' => [...],
    'my_driver' => [
        ... # Your Config Params here.
    ]
]

现在,您必须创建一个驱动程序映射类,该类将用于发送短信。在您的驱动程序中,您只需扩展 Tzsk\Sms\Contracts\Driver

例如,您创建了一个类: App\Packages\SMSDriver\MyDriver

namespace App\Packages\SMSDriver;

use Tzsk\Sms\Contracts\Driver;

class MyDriver extends Driver
{
    /**
    * You Should implement these methods:
    *
    * 1. boot() -> (optional) Initialize any variable or configuration that you need.
    * 2. send() -> Main method to send messages.
    *
    * Note: settings array will be automatically assigned in Driver class' constructor.
    *
    * Example Given below:
    */

    /**
    * @var mixed
    */
    protected $client;

    protected function boot() : void
    {
        $this->client = new Client(); # Guzzle Client for example.
    }

    /**
    * @return object Ex.: (object) ['status' => true, 'data' => 'Client Response Data'];
    */
    public function send()
    {
        $this->recipients; # Array of Recipients.
        $this->body; # SMS Body.

        # Main logic of Sending SMS.
        ...
    }

}

一旦创建该类,您必须在该 sms.php 配置文件的 map 部分中指定它。

'map' => [
    ...
    'my_driver' => App\Packages\SMSDriver\MyDriver::class,
]

注意:您必须确保 map 数组的键与 drivers 数组的键相同。

🔬 测试

composer test

📅 更新日志

请参阅变更日志获取更多关于最近更改的信息。

❤️ 贡献

请参阅贡献指南以获取详细信息。

🔒 安全漏洞

请查看我们的安全策略了解如何报告安全漏洞。

👑 致谢

👮‍♂️ 许可证

MIT 许可证 (MIT)。请参阅许可证文件获取更多信息。