insense/laravel-sms

本软件包将为短信通道提供短信功能

2.0 2020-10-07 19:55 UTC

README

这是一个用于Laravel框架发送短信的软件包。该软件包目前支持Textlocal和MSG91 API,但也很容易扩展到其他API。我们欢迎PR以扩展其他服务。

该软件包为支持的短信API内部短信实现提供了一个包装器外观。

基本示例
$isTransactionalMsg = false;
$recipient1 = "XXXXXXXXXX";
$recipient2 = "XXXXXXXXXX";
$recipients = [$recipient1, $recipient2];
$msgText = "Your SMS text should be typed here";
SMS::driver()->sendSms($recipients, $msgText, $isTransactionalMsg);	

版本兼容性

该软件包目前支持Laravel 5.1及以上版本。

安装

通过composer安装此软件包

composer require insense/laravel-sms

如果使用Laravel 5.1到5.4,请注册ServiceProvider和(可选)外观

// config/app.php

'providers' => [
    ...
    \Insense\LaravelSMS\Providers\SMSChannelServiceProvider::class,

];

...

'aliases' => [
	...
    'SMS' => \Insense\LaravelSMS\Facades\SMS::class,
],

接下来,使用以下artisan命令发布配置文件。

php artisan vendor:publish --provider="Insense\LaravelSMS\Providers\SMSChannelServiceProvider" --tag="config"

如果使用Laravel 5.5+

php artisan vendor:publish

现在,运行迁移

php artisan migrate

发布后,将以下值添加到您的.env文件

# Default SMS Driver value can be from "msg91" or "textlocal"
SMS_DRIVER=

# msg91 (private) API key
MSG91_APIKEY=

# msg91 api end point, this can be changed check same from msg91 api doc
MSG91_ENDPOINT=https://api.msg91.com/api/

# msg91 promotional message sender code to be shown to user
MSG91_PROMOSENDER=777777

# msg91 transactional message sender code to be shown to user
MSG91_TRANSSENDER=INSMS

# Now add Text Local APIs configurations

# TextLocal (private) API key
TEXTLOCAL_APIKEY=

# TextLocal Api registration username
TEXTLOCAL_USERNAME=

# TextLocal Api Hashcode 
TEXTLOCAL_HASH=

# textlocal api end point, this can be changed check same from textlocal api doc
TEXTLOCAL_ENDPOINT=https://api.textlocal.in/

# textlocal promotional message sender code to be shown to user
TEXTLOCAL_TRANSSENDER=TXTLCL

# textlocal transactional message sender code to be shown to user
TEXTLOCAL_PROMOSENDER=TXTLCL

您还可以在config/sms.php中配置该软件包。

在您的模型软件包中添加此内容,以保存短信投递报告

class SMSReport extends BaseModel
{
	/**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'sms_report_id';
	
	/**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = "sms_reports";
}

在您的EventServiceProvider中添加适当的短信事件监听器

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    // to listen for Delivery Report of SMS, populate `SMSReport` Model
    'Insense\LaravelSMS\Events\SMSDeliveryEvent' => [
        'App\Listeners\YourListener1',
    ],
    // to listen for sms sent api triggered, client can now wait for delivery report
    'Insense\LaravelSMS\Events\SMSSentEvent' => [
        'App\Listeners\YourListener2',
    ],
    // to listen for unsubscribed any user(Laravel User) from application because of incorrect number
    'Insense\LaravelSMS\Events\SMSUnsubscribeEvent' => [
        'App\Listeners\YourListener3',
    ],
];

HTTP客户端依赖:Guzzle Http

Guzzle Http客户端用于向API发送请求,并且是该软件包的依赖项。

用法

Insense SMS软件包提供了发送分组短信和批量短信的大部分功能

可以使用SMS::sendSms()方法发送短信消息

/**
 * Send an SMS to one or more comma separated numbers
 * @param       $numbers
 * @param       $message
 * @param bool  $transactional
 * @param bool  $unicode
 * @param array $options country, flash, ignoreNdnc, campaign
 * @return array|mixed
 * @throws Exception
*/
SMS::sendSms(array $numbers, $message = null, $transactional = false, $unicode = false, Carbon $scheduleTime = null, array $options = []);

可以使用SMS::sendSmsGroup()方法发送短信消息

/**
 * Send an SMS to a Group of contacts - group IDs can be retrieved from getGroups()
 * @param       $groupId
 * @param       $message
 * @param bool  $transactional
 * @param bool  $unicode
 * @param array $options test, receipt_url, custom, optouts, validity
 * @return array|mixed
 * @throws Exception
*/
SMS::sendSmsGroup($groupId, $message = null, $transactional = false, $unicode = false, Carbon $scheduleTime = null, array $options = []);

可以使用SMS::createGroup()方法创建短信组

/**
 * Create a new contact group
 * @param $group_name
 * @return string group id
*/
SMS::createGroup($group_name);

可以使用SMS::getContacts()方法获取联系人短信组

/**
 * Get contacts from a group - Group IDs can be retrieved with the getGroups() function
 * @param     $groupId
 * @return array SMSContacts
*/
SMS::getContacts($groupId = null);


许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件