Skip to content

Commit

Permalink
imporoved documentation & fixed bug with passing params in getSMS/get…
Browse files Browse the repository at this point in the history
…MMS function
  • Loading branch information
sinarahmany committed Apr 15, 2023
1 parent e25480a commit e81f39a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 93 deletions.
50 changes: 44 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

```

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -19,11 +19,11 @@
"extra": {
"laravel": {
"providers": [
"Sinarahmannejad\\LaravelVoipMs\\VoipMsServiceProvider"
"Sinarahmany\\LaravelVoipMs\\VoipMsServiceProvider"
]
}
},
"require": {
"php": ">=7.2.0"
"php": ">=7.4.0"
}
}
2 changes: 1 addition & 1 deletion src/Facades/VoipMs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sinarahmannejad\LaravelVoipMs\Facades;
namespace Sinarahmany\LaravelVoipMs\Facades;

use Illuminate\Support\Facades\Facade;

Expand Down
174 changes: 93 additions & 81 deletions src/VoipMs.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace Sinarahmannejad\LaravelVoipMs;
namespace Sinarahmany\LaravelVoipMs;

use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;

class VoipMs
{
Expand All @@ -29,23 +30,20 @@ public static function sendSMS(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
]);

if ($response['status'] === 'success') {
return $response['sms'];
} else {
throw new Exception($jsonResponse['status']);
throw new Exception($response['status']);
}
}

Expand All @@ -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';

Expand All @@ -71,100 +70,113 @@ 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']);
}
}


/**
* 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']);
}
}
}
2 changes: 1 addition & 1 deletion src/VoipMsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sinarahmannejad\LaravelVoipMs;
namespace Sinarahmany\LaravelVoipMs;

use Illuminate\Support\ServiceProvider;

Expand Down

0 comments on commit e81f39a

Please sign in to comment.