From e81f39a55836b1b044aab42856c54e8e8d672a28 Mon Sep 17 00:00:00 2001 From: Sina Rahmannejad <46980823+sinarahmany@users.noreply.github.com> Date: Sat, 15 Apr 2023 12:36:59 -0700 Subject: [PATCH] imporoved documentation & fixed bug with passing params in getSMS/getMMS function --- README.md | 50 ++++++++-- composer.json | 8 +- src/Facades/VoipMs.php | 2 +- src/VoipMs.php | 174 ++++++++++++++++++---------------- src/VoipMsServiceProvider.php | 2 +- 5 files changed, 143 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 60695b9..a0e4885 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # laravel-voipms -Laravel Voip Ms API Integration +Laravel Voip Ms API Integration - the perfect wingman for your Laravel project to connect with your VoIP Ms crush! [![Total Downloads](https://img.shields.io/packagist/dt/sinarahmannejad/laravel-voipms.svg?style=flat)](https://packagist.org/packages/sinarahmannejad/laravel-voipm) [![Latest Stable Version](https://img.shields.io/packagist/v/sinarahmannejad/laravel-voipms.svg?style=flat)](https://packagist.org/packages/sinarahmannejad/laravel-voipm) @@ -14,8 +14,8 @@ Begin by installing this package through Composer. Run this command from the Ter ```bash - composer require sinarahmannejad/laravel-voipms - php artisan vendor:publish --provider="Sinarahmannejad\LaravelVoipMs\VoipMsServiceProvider" + composer require sinarahmany/laravel-voipms + php artisan vendor:publish ``` @@ -25,11 +25,49 @@ To run this project, you will need to add the following environment variables to `VOIPMS_API_URL=https://voip.ms/api/v1/rest.php` -`VOIPMS_USERNAME={YOURUSERNAME}` +`VOIPMS_USERNAME={YOUR_USERNAME}` -`VOIPMS_PASSWORD={YOURPASSWORD` +`VOIPMS_PASSWORD={YOUR_PASSWORD}` -`VOIPMS_DID={YOURDID}` +`VOIPMS_DID={YOUR_DID}` + + +## Usage + +First, include the Facade class at the top of your file: + +```php +use Sinarahmany\LaravelVoipMs\VoipMs; +``` +To send an SMS message to a destination number, use the `sendSMS` method: + +```php +VoipMs::sendSMS($yourNumber, $yourText); +``` + +To send an MMS message to a destination number, use the `sendMMS` method: + +```php +VoipMs::sendMMS($yourNumber, $yourText, $imageurl (optional)); +// Note: $imageUrl is optional. +``` +To retrieve a list of SMS messages by date range, SMS type, DID number, and contact, use the `getSMS` method: +```php +VoipMs::getSMS(['id' => $smsId, 'from' => $fromDate, 'to' => $toDate, 'type' => $smsType, 'contact' => $contactNumber, 'limit' => $limit]); +// Note: passed params are optional. +``` + +## Note for Developers: + + +This is a development package and may be subject to frequent changes and updates as development continues. It is recommended that you regularly check for updates and incorporate any changes as needed. + +Thank you for using this development package, and happy coding! + + +## Feedback + +If you have any feedback, please reach out to us at info@sinarahmannejad.com ## Credits diff --git a/composer.json b/composer.json index aeeecca..ee3d0c4 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { - "name": "sinarahmannejad/laravel-voipms", + "name": "sinarahmany/laravel-voipms", "description": "VOIP Ms API for Laravel", "type": "library", "keywords": ["sms", "voipms", "laravel", "api"], "license": "MIT", "autoload": { "psr-4": { - "Sinarahmannejad\\LaravelVoipMs\\": "src/" + "Sinarahmany\\LaravelVoipMs\\": "src/" } }, "authors": [ @@ -19,11 +19,11 @@ "extra": { "laravel": { "providers": [ - "Sinarahmannejad\\LaravelVoipMs\\VoipMsServiceProvider" + "Sinarahmany\\LaravelVoipMs\\VoipMsServiceProvider" ] } }, "require": { - "php": ">=7.2.0" + "php": ">=7.4.0" } } diff --git a/src/Facades/VoipMs.php b/src/Facades/VoipMs.php index e68dde9..4bb0a43 100644 --- a/src/Facades/VoipMs.php +++ b/src/Facades/VoipMs.php @@ -1,6 +1,6 @@ get($request); - - if ($jsonResponse['status'] === 'success') { - return $jsonResponse['sms']; + $response = Http::withUserAgent('ReqBin Curl Client/1.0') + ->get(config('voipms.api_url'), [ + 'api_username' => config('voipms.username'), + 'api_password' => config('voipms.password'), + 'method' => 'getMMS', + 'did' => config('voipms.did'), + 'dst' => $dst, + 'message' => $message + ]); + + if ($response['status'] === 'success') { + return $response['sms']; } else { - throw new Exception($jsonResponse['status']); + throw new Exception($response['status']); } } @@ -54,12 +52,13 @@ public static function sendSMS(int $dst, string $message): string * * @param int $dst The DID to retrieve SMS messages for. * @param string $message The message + * @param string $media The message * * @return array The array of SMS messages for the specified DID. * * @throws Exception if the API response indicates an error. */ - public static function sendMMS(int $dst, string $message): string + public static function sendMMS(int $dst, string $message, string $mediaUrl = null): string { $method = 'sendMMS'; @@ -71,23 +70,21 @@ public static function sendMMS(int $dst, string $message): string return 'Text can not be empty!'; } - $request = config('voipms.api_url') . - '?api_username=' . config('voipms.username') . - '&api_password=' . config('voipms.password') . - '&method=' . $method . - '&did=' . config('voipms.did') . - '&dst=' . $dst . - '&message=' . $message; - - $jsonResponse = Http::withHeaders([ - 'User-Agent: ReqBin Curl Client/1.0' - ]) - ->get($request); - - if ($jsonResponse['status'] === 'success') { - return $jsonResponse['sms']; + $response = Http::withUserAgent('ReqBin Curl Client/1.0') + ->get(config('voipms.api_url'), [ + 'api_username' => config('voipms.username'), + 'api_password' => config('voipms.password'), + 'method' => 'getMMS', + 'did' => config('voipms.did'), + 'dst' => $dst, + 'message' => $message, + 'media1' => $mediaUrl, + ]); + + if ($response['status'] === 'success') { + return $response['mms']; } else { - throw new Exception($jsonResponse['status']); + throw new Exception($response['status']); } } @@ -95,76 +92,91 @@ public static function sendMMS(int $dst, string $message): string /** * Retrieves a list of SMS messages by: date range, sms type, DID number, and contact. * - * @param int $dst The DID to retrieve SMS messages for. - * @param string $message The message - * + * @param array|null $params * @return array The array of SMS messages for the specified DID. * * @throws Exception if the API response indicates an error. */ - public static function getSMS(int $id = null, string $from = null, string $to = null, bool $type = true, int $contactNo = null, int $limit = null): Array + public static function getSMS(array|null $params = ['id', 'from', 'to', 'type', 'contact', 'limit']): array { $method = 'getSMS'; - $request = config('voipms.api_url') . - '?api_username=' . config('voipms.username') . - '&api_password=' . config('voipms.password') . - '&method=' . $method . - '&did=' . config('voipms.did') . - '&sms=' . $id . - '&from=' . $from . - '&to=' . $to . - '&type=' . $type . - '&contact=' . $contactNo . - '&limit=' . $limit; - - $jsonResponse = Http::withHeaders([ - 'User-Agent: ReqBin Curl Client/1.0' - ]) - ->get($request); - - if ($jsonResponse['status'] === 'success') { - return $jsonResponse['sms']; + $validator = Validator::make($params, [ + 'id' => 'nullable|int', + 'from' => 'nullable|date', + 'to' => 'nullable|date', + 'type' => 'nullable|numeric', + 'contact' => 'nullable|numeric', + 'limit' => 'nullable|numeric' + ]); + if ($validator->fails()) { + throw new Exception($validator->messages()); + } + + $response = Http::withUserAgent('ReqBin Curl Client/1.0') + ->get(config('voipms.api_url'), [ + 'api_username' => config('voipms.username'), + 'api_password' => config('voipms.password'), + 'method' => 'getMMS', + 'did' => config('voipms.did'), + 'sms' => $params['id'] ?? null, + 'from' => $params['from'] ?? null, + 'to' => $params['to'] ?? null, + 'type' => $params['type'] ?? null, + 'contact' => $params['contact'] ?? null, + 'limit' => $params['limit'] ?? null, + ]); + + if ($response['status'] === 'success') { + return $response['sms']; } else { - throw new Exception($jsonResponse['status']); + throw new Exception($response['status']); } } /** * Retrieves a list of MMS messages by: date range, sms type, DID number, and contact. * - * @param int $dst The DID to retrieve SMS messages for. - * @param string $message The message + * @param array|null $params The params to filter messages * * @return array The array of SMS messages for the specified DID. * * @throws Exception if the API response indicates an error. */ - public static function getMMS(int $id = null, string $from = null, string $to = null, bool $type = true, int $contactNo = null, int $limit = null): Array + public static function getMMS(array|null $params = ['id', 'from', 'to', 'type', 'contact', 'limit']): array { $method = 'getMMS'; - $request = config('voipms.api_url') . - '?api_username=' . config('voipms.username') . - '&api_password=' . config('voipms.password') . - '&method=' . $method . - '&did=' . config('voipms.did') . - '&sms=' . $id . - '&from=' . $from . - '&to=' . $to . - '&type=' . $type . - '&contact=' . $contactNo . - '&limit=' . $limit; - - $jsonResponse = Http::withHeaders([ - 'User-Agent: ReqBin Curl Client/1.0' - ]) - ->get($request); - - if ($jsonResponse['status'] === 'success') { - return $jsonResponse['sms']; + $validator = Validator::make($params, [ + 'id' => 'nullable|int', + 'from' => 'nullable|date', + 'to' => 'nullable|date', + 'type' => 'nullable|numeric', + 'contact' => 'nullable|numeric', + 'limit' => 'nullable|numeric' + ]); + if ($validator->fails()) { + throw new Exception($validator->messages()); + } + + $response = Http::withUserAgent('ReqBin Curl Client/1.0') + ->get(config('voipms.api_url'), [ + 'api_username' => config('voipms.username'), + 'api_password' => config('voipms.password'), + 'method' => 'getMMS', + 'did' => config('voipms.did'), + 'sms' => $params['id'] ?? null, + 'from' => $params['from'] ?? null, + 'to' => $params['to'] ?? null, + 'type' => $params['type'] ?? null, + 'contact' => $params['contact'] ?? null, + 'limit' => $params['limit'] ?? null, + ]); + + if ($response['status'] === 'success') { + return $response['mms']; } else { - throw new Exception($jsonResponse['status']); + throw new Exception($response['status']); } } } diff --git a/src/VoipMsServiceProvider.php b/src/VoipMsServiceProvider.php index 49887f5..d89f599 100644 --- a/src/VoipMsServiceProvider.php +++ b/src/VoipMsServiceProvider.php @@ -1,6 +1,6 @@