meta | gitName | |||||
---|---|---|---|---|---|---|
|
laravel-aruba-sms |
This package makes it easy to send SMS notifications using SMS Aruba API with Laravel.
You can install the package via composer:
composer require offline-agency/laravel-aruba-sms
Laravel notifications system is a way to notify users via email or SMS.
Each notification is represented by a single class that is typically stored
in the app/Notifications
directory.
Here is an example:
<?php
namespace OfflineAgency\LaravelArubaSms\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;
class SendSmsNotification extends Notification
{
use Queueable;
public $message;
public $recipient;
public $message_type;
public function __construct(
$message,
$recipient
)
{
$this->setMessage(
$message
);
$this->setRecipient(
$recipient
);
$this->setMessageType();
}
public function via(
$notifiable
): array
{
return [
'aruba-sms'
];
}
public function getMessage()
{
return $this->message;
}
public function setMessage(
$message
): void
{
$this->message = $message;
}
public function getRecipient()
{
return $this->recipient;
}
public function setRecipient(
$recipient
): void
{
$this->recipient = $recipient;
}
public function getMessageType()
{
return $this->message_type;
}
public function setMessageType(): void
{
$this->message_type = config('aruba.message_type');
}
}
Every notification class has a via
method that determines on which channels the
notification will be delivered, in this case aruba-sms
.
The via
method also receives a $notifiable instance, which will be an instance of
the class to which the notification is being sent.
Then set the two variables in question, $message and $recipient.
First of all, to create a new command, you may use the make:command
Artisan command.
After generating your command, you should define appropriate values for the signature and description properties of the class.
Let’s take ArubaSmsCommand
as an example:
protected $signature = 'aruba:sms {command_type} {--from=} {--to=} {--pageNumber=} {--pageSize=} {--recipient=} {--phoneNumber=*}';
protected $description = '';
The handle method will be called when your command is executed.
public function handle()
{
$argument = $this->argument('command_type');
switch ($argument) {
case 'status':
$this->checkArubaSmsStatus();
break;
case 'history':
$this->checkArubaSmsHistory();
break;
case 'recipient-history':
$this->checkArubaSmsRecipientHistory();
break;
case 'notification':
$this->testSendSms();
break;
default:
$this->warn('Command type not valid');
}
}
The method to check aruba sms status is as follows:
public function checkArubaSmsStatus(): string
{
$aruba_sms_service = new ArubaSmsService;
$response = $aruba_sms_service->checkSmsStatus();
return $this->getStatusMessage(
$response
);
}
To start the command run:
php artisan aruba:sms status
The method to check aruba sms history is as follows:
public function checkArubaSmsHistory(): string
{
$aruba_sms_service = new LaravelArubaSms;
$from = !is_null($this->option('from')) ? $this->option('from') : $this->getFromDate();
$to = $this->option('to');
$pageNumber = $this->option('pageNumber');
$pageSize = $this->option('pageSize');
$response = $aruba_sms_service->getSmsHistory(
$from,
$to,
$pageNumber,
$pageSize
);
$status = $response->status();
if ($status === 200) {
$message = 'History API return "' . $status . '" - ' . $response->body();
$this->info($message);
} else {
$message = 'History API return "' . $status . '" - ' . $response->body();
$this->error($message);
}
return $response->body();
}
To start the command run:
php artisan aruba:sms history
The method to check aruba sms recipient-history is as follows:
public function checkArubaSmsRecipientHistory()
{
if (!is_null($this->option('recipient'))) {
$aruba_sms_service = new LaravelArubaSms;
$recipient = $this->option('recipient');
$from = !is_null($this->option('from')) ? $this->option('from') : $this->getFromDate();
$to = $this->option('to');
$pageNumber = $this->option('pageNumber');
$pageSize = $this->option('pageSize');
$response = $aruba_sms_service->getSmsRecipientHistory(
$recipient,
$from,
$to,
$pageNumber,
$pageSize
);
$status = $response->status();
if ($status === 200) {
$message = 'Recipient History API return "' . $status . '" - ' . $response->body();
$this->info($message);
} else {
$message = 'Recipient History API return "' . $status . '" - ' . $response->body();
$this->error($message);
}
} else {
$this->error('Missing require parameter "recipient". Please see project docs.');
}
}
To start the command run:
php artisan aruba:sms recipient-history
The method to test the sending of sms is as follows
public function testSendSms()
{
if (
!is_null($this->option('phoneNumber')) &&
!empty($this->option('phoneNumber'))
) {
$phoneNumbers = $this->option('phoneNumber');
$recipient = [];
$message = 'Sms from console command';
foreach ($phoneNumbers as $phoneNumber) {
$phoneNumber = Str::contains($phoneNumber, '+39') ? $phoneNumber : '+39' . $phoneNumber;
if (Str::contains($phoneNumber, ' ')) {
$phoneNumber = str_replace(
' ',
'',
$phoneNumber
);
}
array_push(
$recipient,
$phoneNumber
);
}
$user = new User;
Notification::send(
$user,
new SendSmsNotification(
$message,
$recipient
)
);
} else {
$this->error('Missing require parameter "phoneNumber". Please see project doc.');
}
}
To start the command run:
php artisan aruba:sms notification
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.