3 Commits

  1. 115
      app/Channels/Messages/SmsMessage.php
  2. 54
      app/Channels/SmsChannel.php
  3. 10
      app/Models/User.php
  4. 15
      app/Notifications/SmsNotification.php
  5. 4
      app/Providers/AppServiceProvider.php
  6. 11
      app/Utilities/HelperClass/NotificationHelper.php
  7. 16
      config/smsirlaravel.php
  8. 12
      resources/lang/fa/notification.php
  9. 4
      routes/api.php

115
app/Channels/Messages/SmsMessage.php

@ -0,0 +1,115 @@
<?php
namespace App\Channels\Messages;
class SmsMessage
{
/**
* The devices token to send the message from.
*
* @var array|string
*/
public $to;
/**
* The data of the Sms message.
*
* @var array
*/
public $data;
/**
* The params that we define in sms.ir template
* when we use ultraFastSend method
*
* @var array
*/
public $params;
/**
* The id of template we define in sms.ir
* when we use ultraFastSend method
*
* @var string
*/
public $template_id;
/**
* The generated verification code.
* when we use sendVerification method.
*
* @var string
*/
public $verification_code;
/**
* Set the devices token to send the message from.
*
* @param array|string $to
* @return $this
*/
public function to($to)
{
$this->to = $to;
return $this;
}
/**
* Set the data of the sms message.
*
* @param array $data
* @return $this
*/
public function data(array $data)
{
$this->data = $data;
return $this;
}
/**
* Set the params of the sms message when we use ultraFastSend method
*
* @param array $params
* @return $this
*/
public function params(array $params)
{
foreach ($parameters as $key => $value) {
$this->params[] = ['Parameter' => $key, 'ParameterValue' => $value];
}
return $this;
}
/**
* Set the template_id of the sms message when we use ultraFastSend method
*
* @param string $template_id
* @return $this
*/
public function templateId(string $template_id)
{
$this->template_id = $template_id;
return $this;
}
/**
* Set verification_code of sms message when we use sendVerification method
*
* @param string $verification_code
* @return $this
*/
public function verificationCode(string $verification_code)
{
$this->verification_code = $verification_code;
return $this;
}
}

54
app/Channels/SmsChannel.php

@ -3,6 +3,7 @@
namespace App\Channels;
use App\Channels\Messages\SmsMessage;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
@ -48,21 +49,68 @@ class SmsChannel
$message->to($notifiable->routeNotificationFor('sms', $notification));
if (! $message->to) {
if (! $message->to || ! ($message->params && $message->template_id) || ! $message->verification_code) {
return;
}
try {
$this->http->post($this->sms_url . 'api/' . $type , [
'headers' => [
'x-sms-ir-secure-token'=>self::getToken()
'x-sms-ir-secure-token'=> $this->getToken()
],
'connect_timeout' => 30,
'json' => ['data' => $message->data],
'json' => $this->buildJsonPayload($message, $type),
]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
report($e);
}
}
/**
* This method used in every request to get the token at first.
*
* @return mixed - the Token for use api
*/
protected function getToken()
{
$body = [
'UserApiKey' => config('smsirlaravel.api-key'),
'SecretKey' => config('smsirlaravel.secret-key'),
'System' => 'laravel_v_1_4'
];
try {
$result = $this->http->post( $this->sms_url . 'api/Token',['json'=>$body,'connect_timeout'=>30]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
report($e);
return;
}
return json_decode($result->getBody(),true)['TokenKey'];
}
/**
* Create sms json payload based on type
*
* @param SmsMessage $message
* @param $type
* @return array
*/
protected function buildJsonPayload(SmsMessage $message, $type) {
if ($type === 'UltraFastSend') {
return[
'ParameterArray' => $message->params,
'TemplateId' => $message->template_id,
'Mobile' => $message->to
];
}
if ($type === 'VerificationCode') {
return[
'Code' => $message->data,
'MobileNumber' => $message->to
];
}
}
}

10
app/Models/User.php

@ -60,6 +60,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
return request('_business_info')['id'] ?? null;
}
/**
* Specifies the user's phone number
*
* @return string
*/
public function routeNotificationForSms()
{
return $this->mobile;
}
public function updateRelations()
{
// projects relations

15
app/Notifications/SmsNotification.php

@ -2,6 +2,7 @@
namespace App\Notifications;
use App\Channels\Messages\SmsMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
@ -14,6 +15,12 @@ class SmsNotification extends Notification
public $message;
/**
* Show type of sms notification.
* [ultraFastSend or sendVerification ]
*
* @var mixed|string
*/
public $type;
/**
@ -21,7 +28,7 @@ class SmsNotification extends Notification
*
* @return void
*/
public function __construct($message, $type = 'MessageSend')
public function __construct($message, $type = 'sendVerification')
{
$this->message = $message;
$this->type = $type;
@ -46,9 +53,11 @@ class SmsNotification extends Notification
*/
public function toSms($notifiable)
{
return [
return (new SmsMessage())
->params($this->message['params'] ?? null)
->templateId($this->message['template_id'] ?? null)
->verificationCode($this->message['verification_code'] ?? null);
];
}
public function getType()

4
app/Providers/AppServiceProvider.php

@ -4,6 +4,7 @@ namespace App\Providers;
use App\Channels\DBChannel;
use App\Channels\FcmChannel;
use App\Channels\SmsChannel;
use App\Channels\SocketChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
@ -29,6 +30,9 @@ class AppServiceProvider extends ServiceProvider
$service->extend('db', function ($app) {
return new DBChannel();
});
$service->extend('sms', function ($app) {
return new SmsChannel(new HttpClient, config('smsirlaravel.webservice-url'));
});
});
}

11
app/Utilities/HelperClass/NotificationHelper.php

@ -8,11 +8,14 @@ use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use App\Notifications\SocketNotification;
use App\Notifications\SmsNotification;
use Illuminate\Support\Facades\Notification;
class NotificationHelper
{
protected $notif;
protected $sms_notif;
/**
* Make notification object
*
@ -33,6 +36,12 @@ class NotificationHelper
return $this;
}
public function makeSmsNotif($template_name, $options = [])
{
$this->sms_notif = __('notification.sms.templates.'.$template_name, $options);
return $this;
}
/**
* Fetch message from notifications lang file
*
@ -56,7 +65,7 @@ class NotificationHelper
public function sendNotifications($users, $level = null) {
switch ($level) {
case "emergency":
// Notification::send($users, new SmsNotification($notif));
Notification::send($users, new SmsNotification($this->sms_notif, 'ultraFastSend'));
case "critical":
Notification::send($users, new MailNotification($this->notif));
case "high":

16
config/smsirlaravel.php

@ -0,0 +1,16 @@
<?php
return [
/* Important Settings */
// ======================================================================
'webservice-url' => env('SMSIR_WEBSERVICE_URL','https://ws.sms.ir/'),
// SMS.ir Api Key
'api-key' => env('SMSIR_API_KEY',null),
// SMS.ir Secret Key
'secret-key' => env('SMSIR_SECRET_KEY',null),
// Your sms.ir line number
'line-number' => env('SMSIR_LINE_NUMBER',null),
// ======================================================================
];

12
resources/lang/fa/notification.php

@ -85,4 +85,16 @@ return [
'suspended' => 'حساب مسدود شد.',
],
'sms' => [
'templates' => [
'template_name' => [
'template_id' => 'asdasd',
'params' => [
'user' => ':user',
'business' => ':business',
]
]
]
]
];

4
routes/api.php

@ -8,7 +8,9 @@ $router->get('/lab', function () {
$router->get('/ntest', function () {
$user = \App\Models\User::find(1);
\Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SocketNotification(['title' => "hello!!!", 'body' => 'sss']));
\Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SmsNotification(['verification_code' => "1234"]));
(new \App\Utilities\HelperClass\NotificationHelper())
->makeSmsNotif('template_name', ['user' => 'myUser', 'business' => 'myBusiness']);
})->middleware('bindBusiness');
$router->group(['prefix' => 'actions'], function () use ($router) {
$router->group(['prefix' => 'businesses'], function () use ($router) {

Loading…
Cancel
Save