Skip to content

Commit

Permalink
Add new drivers (#920)
Browse files Browse the repository at this point in the history
* feat: add rahyabcp driver

* style: fix code style

* feat: add rahyabir driver

* feat: add d7networks driver

* refactor: change rahyabcp driver send method

* feat: add from option

* feat: add hamyarsms driver

* Squashed commit of the following:

commit 2eaea22
Author: Laravel Shift <[email protected]>
Date:   Wed Feb 15 11:32:22 2023 -0500

    Laravel 10.x Compatibility (#922)

    * Bump dependencies for Laravel 10

    * Update GitHub Actions for Laravel 10

    * Drop PHP 8.0 and Use Pint

    * Remove php8.0 from test workflow

    ---------

    Co-authored-by: tzsk <[email protected]>

commit 4533f04
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Wed Feb 1 19:34:38 2023 +0000

    chore(deps): bump symfony/http-kernel from 6.0.6 to 6.0.20

    Bumps [symfony/http-kernel](https://github.com/symfony/http-kernel) from 6.0.6 to 6.0.20.
    - [Release notes](https://github.com/symfony/http-kernel/releases)
    - [Changelog](https://github.com/symfony/http-kernel/blob/6.2/CHANGELOG.md)
    - [Commits](symfony/http-kernel@v6.0.6...v6.0.20)

    ---
    updated-dependencies:
    - dependency-name: symfony/http-kernel
      dependency-type: indirect
    ...

    Signed-off-by: dependabot[bot] <[email protected]>

commit 363c72f
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Jan 30 23:01:47 2023 +0000

    chore(deps-dev): bump friendsofphp/php-cs-fixer from 3.13.2 to 3.14.1

    Bumps [friendsofphp/php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) from 3.13.2 to 3.14.1.
    - [Release notes](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases)
    - [Changelog](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/CHANGELOG.md)
    - [Commits](PHP-CS-Fixer/PHP-CS-Fixer@v3.13.2...v3.14.1)

    ---
    updated-dependencies:
    - dependency-name: friendsofphp/php-cs-fixer
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...

    Signed-off-by: dependabot[bot] <[email protected]>

* Apply Pint Fixes

* Rename commit message

---------

Co-authored-by: tzsk <[email protected]>
  • Loading branch information
mohaphez and tzsk authored Feb 15, 2023
1 parent 2eaea22 commit b515458
Show file tree
Hide file tree
Showing 33 changed files with 577 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ jobs:
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply php-cs Fixes
commit_message: Apply Pint Fixes
commit_author: Kazi Ahmed <[email protected]>
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ List of supported gateways:
- [Sms77](https://www.sms77.io)
- [SabaPayamak](https://sabapayamak.com)
- [LSim](https://sendsms.az/)
- [Rahyabcp](https://rahyabcp.ir/)
- [Rahyabir](https://sms.rahyab.ir/)
- [D7networks](https://d7networks.com/)
- [Hamyarsms](https://hamyarsms.com/)

- Others are under way.

Expand Down Expand Up @@ -69,7 +73,7 @@ Then fill the credentials for that gateway in the drivers array.
'key' => 'Your AWS SNS Access Key',
'secret' => 'Your AWS SNS Secret Key',
'region' => 'Your AWS SNS Region',
'sender' => 'Your AWS SNS Sender ID',
'from' => 'Your AWS SNS Sender ID', //sender
'type' => 'Tansactional', // Or: 'Promotional'
],
...
Expand Down Expand Up @@ -171,6 +175,16 @@ sms()->via('gateway')->send("this message", function($sms) {

sms()->via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();

# Change the from|sender|sim value with from() option:

sms()->via('gateway')->send("this message")->from('Your From Number | Sender Value | Sim Value ')->to(['Number 1', 'Number 2'])->dispatch();

# Sending argument and pattern code in pattern drivers such as melipayamakpattern and farazsmspattern.

#Note: The first argument is always known as the pattern code.

sms()->via('melipayamakpattern')->send("patterncode=123 \n arg1=name \n arg2=family")->to(['Number 1', 'Number 2'])->dispatch();

```

## :heart_eyes: Channel Usage
Expand Down
44 changes: 39 additions & 5 deletions config/sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
'key' => 'Your AWS SNS Access Key',
'secret' => 'Your AWS SNS Secret Key',
'region' => 'Your AWS SNS Region',
'sender' => 'Your AWS SNS Sender ID',
'from' => 'Your AWS SNS Sender ID', // sender
'type' => 'Transactional', // Or: 'Promotional'
],
'textlocal' => [
'url' => 'http://api.textlocal.in/send/', // Country Wise this may change.
'username' => 'Your Username',
'hash' => 'Your Hash',
'sender' => 'Sender Name',
'from' => 'Sender Name', // sender
],
// Install: composer require twilio/sdk
'twilio' => [
Expand All @@ -51,7 +51,7 @@
'url' => 'http://simple.pswin.com', // Country Wise this may change.
'username' => 'Your Username',
'password' => 'Your Password',
'sender' => 'Sender name',
'from' => 'Sender name', // sender
],
// Install: composer require melipayamak/php
'melipayamak' => [
Expand Down Expand Up @@ -101,7 +101,7 @@
'url' => 'https://smsgateway24.com/getdata/addsms',
'token' => 'Your Api Token',
'deviceid' => 'Your Default Device ID',
'sim' => 'Device SIM Slot. 0 or 1',
'from' => 'Device SIM Slot. 0 or 1', // sim
],
'ghasedak' => [
'url' => 'http://api.iransmsservice.com',
Expand All @@ -124,7 +124,37 @@
'lsim' => [
'username' => 'Your LSIM login',
'password' => 'Your LSIM password',
'sender' => 'Your LSIM Sender ID',
'from' => 'Your LSIM Sender ID', // sender
],
'rahyabcp' => [
'url' => 'https://p.1000sms.ir/Post/Send.asmx?wsdl',
'username' => 'Your Rahyabcp login',
'password' => 'Your Rahyabcp password',
'from' => 'Your Default From Number',
'flash' => false,
],
'rahyabir' => [
'url' => 'https://api.rahyab.ir',
'username' => 'Your Rahyabir Username',
'password' => 'Your Rahyabir Password',
'company' => 'Your Rahyabir Company',
'from' => 'Your Default From Number',
'token_valid_day' => 1,
],
'd7networks' => [
'url' => 'https://api.d7networks.com',
'username' => 'Your D7networks ClientId',
'password' => 'Your D7networks clientSecret',
'originator' => 'SignOTP',
'report_url' => '',
'token_valid_day' => 1,
],
'hamyarsms' => [
'url' => 'http://payamakapi.ir/SendService.svc?singleWsdl',
'username' => 'Your Hamyarsms Username',
'password' => 'Your Hamyarsms Password',
'from' => 'Your Default From Number',
'flash' => false,
],
],

Expand Down Expand Up @@ -158,5 +188,9 @@
'sms77' => \Tzsk\Sms\Drivers\Sms77::class,
'sabapayamak' => \Tzsk\Sms\Drivers\SabaPayamak::class,
'lsim' => \Tzsk\Sms\Drivers\LSim::class,
'rahyabcp' => \Tzsk\Sms\Drivers\Rahyabcp::class,
'rahyabir' => \Tzsk\Sms\Drivers\Rahyabir::class,
'd7networks' => \Tzsk\Sms\Drivers\D7networks::class,
'hamyarsms' => \Tzsk\Sms\Drivers\Hamyarsms::class,
],
];
10 changes: 10 additions & 0 deletions src/Contracts/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ abstract class Driver

protected string $body = '';

protected ?string $sender = '';

public function __construct(array $settings)
{
$this->settings = $settings;
$this->from(data_get($settings, 'from'));
$this->boot();
}

Expand All @@ -35,6 +38,13 @@ public function to($numbers): self
return $this;
}

public function from(?string $sender): self
{
$this->sender = $sender;

return $this;
}

public function message(string $message): self
{
$message = trim($message);
Expand Down
145 changes: 145 additions & 0 deletions src/Drivers/D7networks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Tzsk\Sms\Drivers;

use Illuminate\Support\Facades\Http;
use Tzsk\Sms\Contracts\Driver;
use Tzsk\Sms\Exceptions\InvalidMessageException;
use Tzsk\Sms\Exceptions\InvalidSmsConfigurationException;

class D7networks extends Driver
{
private ?string $baseUrl;

private ?string $username;

private ?string $password;

private ?string $token;

protected function boot(): void
{
$this->baseUrl = trim($this->settings['url'], '/');
$this->username = $this->settings['username'];
$this->password = $this->settings['password'];
$this->token = cache('d7networks_token');
}

public function send()
{
if (empty($this->token)) {
$this->login();
}

if (count($this->recipients) > 200) {
throw new InvalidMessageException('Recipients cannot be more than 100 numbers.');
}

$response = Http::withToken($this->token)->withHeaders($this->getHttpHeaders())
->post($this->getSendSmsApiUrl(), [
'messages' => [
'channel' => 'sms',
'recipients' => $this->recipients,
'content' => $this->body,
'msg_type' => 'text',
'data_coding' => $this->getDataCoding($this->body),
],
'message_globals' => [
'originator' => data_get($this->settings, 'originator'),
'report_url' => data_get($this->settings, 'report_url'),
],
]);

$response = $response->json();

if (isset($response['detail']['code'])) {
cache()->forget('rahyabir_token');

return $this->send();
}

if (! isset($response['detail'])) {
return $response;
}

throw new InvalidMessageException(json_encode($response));
}

private function login(): void
{
$this->validateConfiguration();

$response = Http::post($this->getLoginApiUrl(), [
'client_id' => $this->username,
'client_secret' => $this->password,
]);

$response = $response->json();

if (isset($response['detail'])) {
$error = $response['detail'];

throw new InvalidMessageException($error['message'], $error['code']);
}

$token = $response['access_token'];

cache([
'd7networks_token' => $token,
], now()->addDays($this->getTokenValidDay()));

$this->token = $token;
}

private function validateConfiguration(): void
{
if (empty($this->baseUrl)) {
throw new InvalidSmsConfigurationException('d7networks config not found: api base url');
}

if (empty($this->username)) {
throw new InvalidSmsConfigurationException('d7networks config not found: username');
}

if (empty($this->password)) {
throw new InvalidSmsConfigurationException('d7networks config not found: password');
}

if (empty($this->number)) {
throw new InvalidSmsConfigurationException('d7networks config not found: from number');
}
}

private function getErrorMessage(array $errors)
{
return array_shift($errors)['message'];
}

private function getLoginApiUrl(): string
{
return $this->baseUrl.'/auth/v1/login/application';
}

private function getSendSmsApiUrl(): string
{
return $this->baseUrl.'/messages/v1/send';
}

private function getTokenValidDay()
{
return $this->settings['token_valid_day'];
}

private function getHttpHeaders(): array
{
return [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
}

private function getDataCoding(string $string): string
{
return strlen($string) != strlen(utf8_decode($string)) ? 'text' : 'auto';
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Farazsms.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function payload($recipient): array
'form_params' => [
'uname' => data_get($this->settings, 'username'),
'pass' => data_get($this->settings, 'password'),
'from' => data_get($this->settings, 'from'),
'from' => $this->sender,
'message' => $this->body,
'to' => json_encode([$recipient]),
'op' => 'send',
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Farazsmspattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function payload($recipient): array
return [
'username' => data_get($this->settings, 'username'),
'password' => data_get($this->settings, 'password'),
'from' => data_get($this->settings, 'from'),
'from' => $this->sender,
'to' => json_encode([$recipient]),
'pattern_code' => $pattern_code,
'input_data' => json_encode($input_data),
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Ghasedak.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function send()
[
'form_params' => [
'Receptor' => $recipient,
'sender' => $this->settings['from'],
'sender' => $this->sender,
'message' => $this->body,
],
'headers' => [
Expand Down
37 changes: 37 additions & 0 deletions src/Drivers/Hamyarsms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tzsk\Sms\Drivers;

use SoapClient;
use Tzsk\Sms\Contracts\Driver;
use Tzsk\Sms\Exceptions\InvalidMessageException;

class Hamyarsms extends Driver
{
protected SoapClient $client;

protected function boot(): void
{
$this->client = new SoapClient(data_get($this->settings, 'url'));
}

public function send()
{
if (count($this->recipients) > 100) {
throw new InvalidMessageException('Recipients cannot be more than 100 numbers.');
}

$response = $this->client->SendSMS([
'userName' => data_get($this->settings, 'username'),
'password' => data_get($this->settings, 'password'),
'fromNumber' => $this->sender,
'toNumbers' => $this->recipients,
'messageContent' => $this->body,
'isFlash' => data_get($this->settings, 'flash'),
'recId' => [0],
'status' => 0x0,
]);

return $response;
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Kavenegar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function send()
foreach ($this->recipients as $recipient) {
$response->put(
$recipient,
$this->client->Send(data_get($this->settings, 'from'), $recipient, $this->body)
$this->client->Send($this->sender, $recipient, $this->body)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/LSim.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function send()
'login' => data_get($this->settings, 'username'),
'msisdn' => $recipient,
'text' => $this->body,
'sender' => data_get($this->settings, 'sender'),
'sender' => $this->sender,
'key' => $this->getKey($this->body, $recipient),
]);

Expand All @@ -36,7 +36,7 @@ private function getKey(string $message, string $number): string
.data_get($this->settings, 'username')
.$message
.$number
.data_get($this->settings, 'sender')
.$this->sender
);
}
}
Loading

0 comments on commit b515458

Please sign in to comment.