sguy / easysms
此包的最新版本(v1.0.1)没有可用的许可证信息。
gh sms gateway 在线短信网关的包装器
v1.0.1
2020-10-22 15:37 UTC
This package is auto-updated.
Last update: 2024-09-23 16:21:43 UTC
README
描述
EasySms 是一个 Laravel 包(https://smsonlinegh.com 短信网关的包装器),它使您在 Laravel 应用程序中发送短信变得非常简单,设置也很少。
此包还附带了一个与 Laravel 强大的通知系统集成得非常好的通知通道。但是请注意,smsonlinegh 只在加纳工作!
您可以通过 composer 获取此包,按照以下步骤完成安装。
- 运行 composer require sguy/easysms
- 将 Sguy\EasySms\EasySmsServiceProvider::class, 添加到 app.php 配置文件中的 providers 数组中
- 通过运行以下命令发布配置文件 php artisan vendor:publish --tag=easysms
- 运行 composer dump-autoload
使用方法
此包允许您发送短信、安排短信、检查短信余额、在发送之前检查每条短信的费用,并最终将其用作通知通道。
第一步
要开始使用此包,您需要免费在 https://smsonlinegh.com 上创建一个账户。您可能想购买一些账户信用以发送短信(信用非常便宜)。在创建账户并购买一些短信信用后,导航到 easysms.php 配置文件并相应更新。easysms.php 配置文件
配置文件只有三个键 ..
- account_login : 将您在 smsonlinegh 上创建的账户的用户名或电子邮件地址作为此键的值。
- account_password : 将您在 smsonlinegh 上创建的账户的密码作为此键的值。
- sender_id : 这是您想发送消息的对象,它不应超过 11 个字符,否则值将被截断并使用前 11 个字符,例如,发送者 ID 可以是一个组织名称, Yara Ghana 或一个电话号码,0543920099。
第二步
发送简单消息
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Sguy\EasySms\Sms\EasySms; class SmsController extends Controller { public function sendSms(Request $request, EasySms $sms) { //get information from request or any source... $mess = $request->message; $phone = $request->phone; //sender id is optional, value from easysms.php config file will be used if not set. $sms->setSenderId("Yara Ghana"); //set message content $sms->setMessage($mess); //send message $status = $sms->sendMessageTo($phone); //status will contain either "success", "invalid credentials", "insufficient balance" or "failed", if($status==="success"){ return redirect()->back()->with('success','Message has been sent successfully'); } //you can do more checks and act accordingly } }
安排短信
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Sguy\EasySms\Sms\EasySms; class SmsController extends Controller { public function scheduleSms(Request $request, EasySms $sms) { //get information from request or any source... $mess = $request->message; $phone = $request->phone; //sender id is optional, value from easysms.php config file will be used if not set. $sms->setSenderId("Yara Ghana"); //Date can be obtained from request or any datasource $date = now()->addMinutes(10); $sms->schedule($date); //set message content $sms->setMessage($mess); //send message //message will be sent in 10 mins time $status = $sms->sendMessageTo($phone); //status will contain either "success", "invalid credentials", "insufficient balance" or "failed", if($status==="success"){ return redirect()->back()->with('success','Message has been sent successfully'); } //you can do more checks and act accordingly } }
向多个收件人发送消息
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Sguy\EasySms\Sms\EasySms; class SmsController extends Controller { public function sendToMultiple(Request $request, EasySms $sms) { //get information from request or any source... $mess = $request->message; $phones = $request->phones; //sender id is optional, value from easysms.php config file will be used if not set. $sms->setSenderId("Yara Ghana"); //set destinations/receipients which accepts an array of phone numbers to send message to $sms->setDestinations($phones); //you can schedule too // $date = now()->addMinutes(10); // $sms->schedule($date); //set message content $sms->setMessage($mess); //send message //Now use sendMessage instead of sendMessage $status = $sms->sendMessage(); //status will contain either "success", "invalid credentials", "insufficient balance" or "failed", if($status==="success"){ return redirect()->back()->with('success','Message has been sent successfully'); } //you can do more checks and act accordingly } }
检查余额和每条短信费用
<?php namespace App\Http\Controllers; use Sguy\EasySms\Sms\EasySms; class SmsController extends Controller { //You can just add a constructor and inject EasySms as opposed to injecting in each function public function getBalance(EasySms $sms) { $balance = $sms->getSmsAccountBalance(); return $balance; } public function getChargePerSms(EasySms $sms) { //Message must be set before the charge can be determined $sms->setMessage("Bunch of Text Here"); $charge = $sms->getChargePerSms(); return $charge; } }
通知通道
创建一个测试通知
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use \Sguy\EasySms\Channel\EasySms; //Import EasySms from Channel, not Sms class TestNotification extends Notification { use Queueable; /** * Create a new notification instance. * * @return void */ public function __construct() { } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [EasySms::class]; //Replace the default ['mail'] with [EasySms::class]. } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ //Replace toArray or toDatabase function with this public function toEasySms($notifiable) { return [ 'message'=>"Hello, Notification from EasySms", //specify a field on your notifiable entity where you save the phone, Easysms uses phone field if set to null 'field'=>null, //whom you want to send the sms as, Easysms uses sender id in config if not set, maximum of 11 chars,it can also be //a phone number 'sender_id'=>'Easy Sms', //'if set, sms will be sent on the specified date but not immediately' 'datetime'=>null, ]; } }
使用方法
请确保您的通知实体使用类似这样的可通知特质
<?php namespace App; use Illuminate\Notifications\Notifiable; class YourModel { use Notifiable; }
<?php namespace App\Http\Controllers; use App\Notifications\TestNotification; class SmsController extends Controller { public function notify() { $user = auth()->user(); //Note you can schedule notifications too, just accept a data, eg. date in your TestNotification Constructor and supply it here //$date = now()->addMinutes(20); //$user->notify(new TestNotification($date)); //notify user $user->notify(new TestNotification);; return redirect()->back(); } }
测试
如果您为您的函数编写单元测试,请将 EasySms 注入到您的函数中,而不是实例化它(您会在所有示例函数中看到 EasySms 被注入)。原因是为了允许您在测试用例中模拟 EasySms。