jorgenwdm / laravel-beak
一个实现使用您喜欢的群发消息提供商进行群发消息的 Laravel 扩展包
dev-main
2021-11-24 00:51 UTC
Requires
- php: ^8.0
- guzzlehttp/guzzle: >=6.5.x-dev
This package is auto-updated.
Last update: 2024-08-29 05:07:28 UTC
README
需求 | 安装 | 如何发送消息 | 如何获取投递报告Laravel Beak 群发短信消息系统
介绍
此项目实现了发送单个或多个消息以及检索报告(例如,消息状态、账户余额等)所需的所有必要功能。
此项目专为 Laravel 平台构建。
目前,此项目使用 smscanal.com 群发短信提供商进行操作,但已构建了一个接口,可以帮助您为其他提供商开发附加模块。
需求
- PHP >= 8.0
- Laravel >= 8.6.6
安装
安装非常简单。
首先,您需要安装此包
composer require jorgenwdm/laravel-beak
然后,您可以发布此包的配置文件。
php artisan vendor:publish --provider="Jorgenwdm\Beak\BeakServiceProvider"
注意:我们的配置文件是 laravel-beak.php
,您可以在 Laravel 的 config
文件夹中找到它。
在配置文件 laravel-beak.php
中,您可以用提供商提供的凭据填充相应的行。例如
/* |-------------------------------------------------------------------------- | Settings for provider Canal (smscanal.com) |-------------------------------------------------------------------------- | | Here we define the credentials given to us by the provider | Note: Keep in mind that you can use the respective env keys in capitals in your .env file | */ 'canal_api_url' => env('BEAK_CANAL_API_URL', 'http://messaging.smscanal.com/sms'), 'canal_api_user' => env('BEAK_CANAL_API_USER', ''), 'canal_api_password' => env('BEAK_CANAL_API_PASSWORD', ''), 'canal_api_sender' => env('BEAK_CANAL_API_SENDER', ''),
现在我们可以开始了!
如何发送消息
发送短信的方式非常简单。您初始化所需的服务,例如 CanalSmsMessage,用来自、目的地、文本等参数填充它,然后触发发送过程。
我们可以使用三种格式化方法:from()
、to()
、text()
来构建我们的消息。
然后我们可以使用 send()
触发发送过程。
use Jorgenwdm\Beak\Messengers\CanalSmsMessage; $message = new CanalSmsMessage(); $message->from('DEMOSENDER')->to('306942931111')->text("HELLO WORLD")->send();
重要提示:一些短信提供商不希望在国际电话号码前加加号。
发送过程返回一个我们称之为响应对象的对象。
响应对象具有一些重要的方法和属性:hasException
、getJson()
、getException()
、getRaw()
use Jorgenwdm\Beak\Messengers\CanalSmsMessage; $message = new CanalSmsMessage(); $result = $message->from('DEMOSENDER')->to('306942931111')->text("HELLO WORLD")->send(); if( !$result->hasException ) { // we read the data of the result as a json object $json = $result->getJson(); // now based on the documentation of the provider, we present our results if( !isset($json->error) ) echo 'Your message was sent to ' . $json->sms->{'mobile-no'}; else echo 'Your message was not sent. API returned error: ' . $json->error->{'error-description'}; } else { // inform the user about the exception echo 'An exception happened: ' . $result->getException()->getMessage(); }
如何获取投递报告
检索投递报告的方式非常简单,与消息发送过程非常相似。
您初始化所需的服务,例如 CanalSmsReport,用 for
参数填充它,然后触发投递报告请求过程。
use \Jorgenwdm\Beak\Reporters\CanalSmsReport; $report = new CanalSmsReport(); $result = $report->for(["825252128"])->request(); if(!$result->hasException) { // we read the data of the result as a json object $json = $result->getJson(); // now based on the documentation of the provider, we present our results echo "Report Code: " . $json->responsecode . "<br/>"; echo "Report Description: " . $json->resposedescription . "<br/>"; } else { // inform the user about the exception echo 'An exception happened: ' . $result->getException()->getMessage(); }
许可证
此项目是开源软件,许可协议为 MIT 许可证。