From d75eff7061883f56261e51ae6773bcabfb97ca16 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 14:09:44 +0330 Subject: [PATCH 1/8] add sms message and config --- app/Channels/Messages/SmsMessage.php | 115 +++++++++++++++++++++++++++ config/smsirlaravel.php | 16 ++++ 2 files changed, 131 insertions(+) create mode 100644 app/Channels/Messages/SmsMessage.php create mode 100644 config/smsirlaravel.php diff --git a/app/Channels/Messages/SmsMessage.php b/app/Channels/Messages/SmsMessage.php new file mode 100644 index 0000000..0d8991f --- /dev/null +++ b/app/Channels/Messages/SmsMessage.php @@ -0,0 +1,115 @@ +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; + } + +} diff --git a/config/smsirlaravel.php b/config/smsirlaravel.php new file mode 100644 index 0000000..3ef586a --- /dev/null +++ b/config/smsirlaravel.php @@ -0,0 +1,16 @@ + 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), + // ====================================================================== +]; From aa7c780ba992935c793cc6566e472533a69b2aee Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 14:10:42 +0330 Subject: [PATCH 2/8] connect SmsChannel.php to sms.ir with two different function --- app/Channels/SmsChannel.php | 54 +++++++++++++++++++++++++-- app/Notifications/SmsNotification.php | 15 ++++++-- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/app/Channels/SmsChannel.php b/app/Channels/SmsChannel.php index 49d72d4..c54e34a 100644 --- a/app/Channels/SmsChannel.php +++ b/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 + ]; + } + } } diff --git a/app/Notifications/SmsNotification.php b/app/Notifications/SmsNotification.php index f44abcb..44d5852 100644 --- a/app/Notifications/SmsNotification.php +++ b/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() From 90b1c635ef0efb2b7c86b4d068071209790026bd Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 14:11:25 +0330 Subject: [PATCH 3/8] add sms make notif func to helper and some change --- app/Models/User.php | 10 ++++++++++ app/Providers/AppServiceProvider.php | 4 ++++ app/Utilities/HelperClass/NotificationHelper.php | 11 ++++++++++- resources/lang/fa/notification.php | 12 ++++++++++++ routes/api.php | 4 +++- 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index ea04af2..aa0357c 100644 --- a/app/Models/User.php +++ b/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 diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d25685e..77c57a4 100644 --- a/app/Providers/AppServiceProvider.php +++ b/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')); + }); }); } diff --git a/app/Utilities/HelperClass/NotificationHelper.php b/app/Utilities/HelperClass/NotificationHelper.php index d128e9c..2d8859f 100644 --- a/app/Utilities/HelperClass/NotificationHelper.php +++ b/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": diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 856ea0d..39d08d2 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -85,4 +85,16 @@ return [ 'suspended' => 'حساب مسدود شد.', ], + 'sms' => [ + 'templates' => [ + 'template_name' => [ + 'template_id' => 'asdasd', + 'params' => [ + 'user' => ':user', + 'business' => ':business', + ] + ] + ] + ] + ]; diff --git a/routes/api.php b/routes/api.php index adb8f8f..1f7b178 100644 --- a/routes/api.php +++ b/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) { From fb2e512c29a24e423e5550417357f4d0895f6a65 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 19:19:36 +0330 Subject: [PATCH 4/8] replace db to database in DBNotification.php --- app/Notifications/DBNotification.php | 2 +- app/Providers/AppServiceProvider.php | 13 +++++++++---- ...2021_03_08_114700_create_notifications_table.php | 1 - 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/Notifications/DBNotification.php b/app/Notifications/DBNotification.php index 1711b56..61a553f 100644 --- a/app/Notifications/DBNotification.php +++ b/app/Notifications/DBNotification.php @@ -29,7 +29,7 @@ class DBNotification extends Notification */ public function via($notifiable) { - return ['db']; + return ['database']; } /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 77c57a4..d757d16 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -27,11 +27,16 @@ class AppServiceProvider extends ServiceProvider $service->extend('socket', function ($app) { return new SocketChannel(new HttpClient, config('socket.url')); }); - $service->extend('db', function ($app) { - return new DBChannel(); - }); +// $service->extend('db', function ($app) { +// return new DBChannel(); +// }); $service->extend('sms', function ($app) { - return new SmsChannel(new HttpClient, config('smsirlaravel.webservice-url')); + return new SmsChannel( + new HttpClient, + config('smsirlaravel.webservice-url'), + config('smsirlaravel.api-key'), + config('smsirlaravel.secret-key'), + ); }); }); } diff --git a/database/migrations/2021_03_08_114700_create_notifications_table.php b/database/migrations/2021_03_08_114700_create_notifications_table.php index 16b08ad..9797596 100644 --- a/database/migrations/2021_03_08_114700_create_notifications_table.php +++ b/database/migrations/2021_03_08_114700_create_notifications_table.php @@ -17,7 +17,6 @@ class CreateNotificationsTable extends Migration $table->uuid('id')->primary(); $table->string('type'); $table->morphs('notifiable'); - $table->unsignedBigInteger('business_id'); $table->text('data'); $table->timestamp('read_at')->nullable(); $table->timestamps(); From c73bf9478c376eb444d5a5ee028d53299d3310e6 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 19:19:48 +0330 Subject: [PATCH 5/8] add sms enum --- app/Enums/sms.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app/Enums/sms.php diff --git a/app/Enums/sms.php b/app/Enums/sms.php new file mode 100644 index 0000000..0741360 --- /dev/null +++ b/app/Enums/sms.php @@ -0,0 +1,12 @@ + [ + 'verification_code' => [ + 'name' => 'VerificationCode' + ], + 'ultra_fast_send' => [ + 'name' => 'ultraFastSend' + ] + ], +]; From a4cff3a0ed5021f4c68315ca15e0269f84d7c44c Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 19:21:08 +0330 Subject: [PATCH 6/8] complete (not test) and some change in sms (channel, message, notification) --- app/Channels/Messages/SmsMessage.php | 8 ++++---- app/Channels/SmsChannel.php | 26 +++++++++++++++++++++----- app/Notifications/SmsNotification.php | 6 +++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/Channels/Messages/SmsMessage.php b/app/Channels/Messages/SmsMessage.php index 0d8991f..36f5214 100644 --- a/app/Channels/Messages/SmsMessage.php +++ b/app/Channels/Messages/SmsMessage.php @@ -75,12 +75,12 @@ class SmsMessage /** * Set the params of the sms message when we use ultraFastSend method * - * @param array $params + * @param array|mixed $params * @return $this */ - public function params(array $params) + public function params($params) { - foreach ($parameters as $key => $value) { + foreach ($params as $key => $value) { $this->params[] = ['Parameter' => $key, 'ParameterValue' => $value]; } return $this; @@ -92,7 +92,7 @@ class SmsMessage * @param string $template_id * @return $this */ - public function templateId(string $template_id) + public function templateId($template_id) { $this->template_id = $template_id; diff --git a/app/Channels/SmsChannel.php b/app/Channels/SmsChannel.php index c54e34a..47ef5d4 100644 --- a/app/Channels/SmsChannel.php +++ b/app/Channels/SmsChannel.php @@ -10,12 +10,26 @@ use GuzzleHttp\Client as HttpClient; class SmsChannel { /** - * The API URL for Socket. + * The API URL for sms. * * @var string */ protected $sms_url; + /** + * The api key for sms inside sms.ir panel. + * + * @var string + */ + protected $api_key; + + /** + * The secret key for sms inside sms.ir panel. + * + * @var string + */ + protected $secret_key; + /** * The HTTP client instance. * @@ -29,10 +43,12 @@ class SmsChannel * @param \GuzzleHttp\Client $http * @return void */ - public function __construct(HttpClient $http, string $sms_url) + public function __construct(HttpClient $http, string $sms_url, string $api_key, string $secret_key) { $this->http = $http; $this->sms_url = $sms_url; + $this->api_key = $api_key; + $this->secret_key = $secret_key; } /** @@ -49,7 +65,7 @@ class SmsChannel $message->to($notifiable->routeNotificationFor('sms', $notification)); - if (! $message->to || ! ($message->params && $message->template_id) || ! $message->verification_code) { + if (! $message->to || (! ($message->params && $message->template_id) && ! $message->verification_code)) { return; } @@ -75,8 +91,8 @@ class SmsChannel protected function getToken() { $body = [ - 'UserApiKey' => config('smsirlaravel.api-key'), - 'SecretKey' => config('smsirlaravel.secret-key'), + 'UserApiKey' => $this->api_key, + 'SecretKey' => $this->secret_key, 'System' => 'laravel_v_1_4' ]; diff --git a/app/Notifications/SmsNotification.php b/app/Notifications/SmsNotification.php index 44d5852..773b6ae 100644 --- a/app/Notifications/SmsNotification.php +++ b/app/Notifications/SmsNotification.php @@ -28,10 +28,10 @@ class SmsNotification extends Notification * * @return void */ - public function __construct($message, $type = 'sendVerification') + public function __construct($message, $type = null) { $this->message = $message; - $this->type = $type; + $this->type = $type ?? enum('sms.types.verification_code.name'); } /** @@ -54,7 +54,7 @@ class SmsNotification extends Notification public function toSms($notifiable) { return (new SmsMessage()) - ->params($this->message['params'] ?? null) + ->params($this->message['params'] ?? []) ->templateId($this->message['template_id'] ?? null) ->verificationCode($this->message['verification_code'] ?? null); From 61911d5cdb962e7dd13e8ddeeade17cae514424d Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 19:22:07 +0330 Subject: [PATCH 7/8] send socket notif in info method and notif handler listener --- app/Listeners/NotifHandler.php | 8 +++++++- app/Models/Business.php | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Listeners/NotifHandler.php b/app/Listeners/NotifHandler.php index 1fed4db..c53def9 100644 --- a/app/Listeners/NotifHandler.php +++ b/app/Listeners/NotifHandler.php @@ -3,8 +3,10 @@ namespace App\Listeners; use App\Events\ModelSaved; +use App\Notifications\SocketNotification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Support\Facades\Notification; class NotifHandler { @@ -29,8 +31,12 @@ class NotifHandler $message = json_decode($event->message); $event_class = 'App\Events\\'.enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'); if (class_exists($event_class)) { -// event(new ('App\Events\\'.$event_class($message))); $event_class::dispatch($message); } + Notification::send(auth()->user(), new SocketNotification( + [ + 'message' => enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'), + 'payload'=>$business_info + ])); } } diff --git a/app/Models/Business.php b/app/Models/Business.php index 80e869f..e308bad 100644 --- a/app/Models/Business.php +++ b/app/Models/Business.php @@ -5,6 +5,8 @@ namespace App\Models; use App\Models\File; use App\Models\Model; use App\Models\SoftDeletes; +use App\Notifications\SocketNotification; +use Illuminate\Support\Facades\Notification; use Illuminate\Validation\Rule; use Illuminate\Http\UploadedFile; use Spatie\MediaLibrary\HasMedia; @@ -234,6 +236,8 @@ class Business extends Model implements HasMedia Cache::put('business_info'.$businessId , $business_info, config('app.cache_ttl')); + Notification::send(auth()->user(), new SocketNotification(['message' => 'business info update','payload'=>$business_info])); + return $business_info; } From 5dc7407ddae3803ed4a14fc5677a846bee720e6b Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 19:22:55 +0330 Subject: [PATCH 8/8] some chang in notif helper class and SocketNotification.php message --- app/Notifications/SocketNotification.php | 4 ++-- app/Utilities/HelperClass/NotificationHelper.php | 6 +++--- routes/api.php | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/Notifications/SocketNotification.php b/app/Notifications/SocketNotification.php index a734ba9..9a6a733 100644 --- a/app/Notifications/SocketNotification.php +++ b/app/Notifications/SocketNotification.php @@ -45,8 +45,8 @@ class SocketNotification extends Notification { return (new SocketMessage()) ->data([ - 'title' => $this->message['title'], - 'body' => $this->message['body'], + 'message' => $this->message['message'], + 'payload' => $this->message['payload'], ]); } } diff --git a/app/Utilities/HelperClass/NotificationHelper.php b/app/Utilities/HelperClass/NotificationHelper.php index 2d8859f..f668871 100644 --- a/app/Utilities/HelperClass/NotificationHelper.php +++ b/app/Utilities/HelperClass/NotificationHelper.php @@ -65,7 +65,7 @@ class NotificationHelper public function sendNotifications($users, $level = null) { switch ($level) { case "emergency": - Notification::send($users, new SmsNotification($this->sms_notif, 'ultraFastSend')); + Notification::send($users, new SmsNotification($this->sms_notif, enum('sms.types.ultra_fast_send.name'))); case "critical": Notification::send($users, new MailNotification($this->notif)); case "high": @@ -73,10 +73,10 @@ class NotificationHelper case "medium": Notification::send($users, new FcmNotification($this->notif)); case "low": - Notification::send($users, new SocketNotification($this->notif)); +// Notification::send($users, new SocketNotification($this->notif)); break; default: - Notification::send($users, new SocketNotification($this->notif)); +// Notification::send($users, new SocketNotification($this->notif)); } } diff --git a/routes/api.php b/routes/api.php index 1f7b178..087aeb0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -9,8 +9,8 @@ $router->get('/lab', function () { $router->get('/ntest', function () { $user = \App\Models\User::find(1); \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']); +// (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) {