Skip to content

Commit

Permalink
Add WhatsApp OTP
Browse files Browse the repository at this point in the history
  • Loading branch information
HasanAlyazidi committed Nov 5, 2023
1 parent b3a931e commit a35932e
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ $otp = new OtpVerifier('966000000000');
$otp->send();
```

- WhatsApp:\
Add a message template named `otp_code`, type (`Authentication`) with your app supported languages.

---

## Contributing
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"prefer-stable":true,
"require": {
"php": ">=7.1|^8.0",
"adrii/whatsapp-api": "^0.7.0",
"guzzlehttp/guzzle": "^7.3",
"illuminate/contracts": "^6|^7|^8|^9|^10",
"illuminate/support": "^6|^7|^8|^9|^10"
Expand Down
11 changes: 11 additions & 0 deletions config/sendables.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,16 @@
'firebase' => [
'apiKey' => '',
],

/**
* WhatsApp
*
* URL: https://business.facebook.com
*/
'whatsapp' => [
'phoneNumberId' => '',
'accessToken' => '',
'graphVersion' => '',
],
]
];
24 changes: 24 additions & 0 deletions src/Services/WhatsApp/WhatsAppOtpProvidor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace HasanAlyazidi\Sendables\Services\WhatsApp;

use HasanAlyazidi\Sendables\OTP\Providers\SystemOtpProvider;
use HasanAlyazidi\Sendables\SMS\Providers\ISMSProvider;

class WhatsAppOtpProvidor extends SystemOtpProvider
{
public function smsProvider(string $message, string $code): ISMSProvider
{
return new WhatsAppOtpTemplateProvider($message, $code, $this->mobile);
}

public function getClientType(): string
{
return 'whatsapp';
}

public function getCodeDigitsCount(): int
{
return 6;
}
}
115 changes: 115 additions & 0 deletions src/Services/WhatsApp/WhatsAppOtpTemplateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace HasanAlyazidi\Sendables\Services\WhatsApp;

use Adrii\Whatsapp\Whatsapp;
use HasanAlyazidi\Sendables\SMS\Providers\ISMSProvider;
use SendablesHelpers;
use Throwable;

class WhatsAppOtpTemplateProvider implements ISMSProvider
{
protected $message;
protected $code;
protected $mobileNumbers;

protected $ws;
protected $isMassageSent = false;

/**
* Our SMS Provider
*
* @param string $message
* @param array|string $mobileNumbers
*/
public function __construct(string $message, string $code, $mobileNumbers) {
$this->message = $message;
$this->code = $code;
$this->mobileNumbers = is_array($mobileNumbers) ? $mobileNumbers : [$mobileNumbers];

$this->initWhatsApp();
}

public function send(): void
{
foreach ($this->mobileNumbers as $mobileNumber) {
$this->sendWhatsAppMessage($mobileNumber);
}
}

public function isSent(): bool
{
return $this->isMassageSent;
}

protected function getPhoneNumberId(): string
{
return $this->getConfigValue('phoneNumberId');
}

protected function getAccessToken(): string
{
return $this->getConfigValue('accessToken');
}

protected function getGraphVersion(): string
{
return $this->getConfigValue('graphVersion');
}

protected function sendWhatsAppMessage(string $mobileNumber): void
{
try {
$bodyComponent = [
'type' => 'body',
'parameters' => [
[
'type' => 'text',
'text' => $this->code,
],
],
];

$buttonComponent = [
'type' => 'button',
'sub_type' => 'url',
'index' => '0',
'parameters' => [
[
'type' => 'text',
'text' => $this->code,
],
],
];

$this->ws->send_message()
->addComponent($bodyComponent, $buttonComponent);

$response = $this->ws->send_message()
->template('otp_code', $mobileNumber, SendablesHelpers::getCurrentLocale());

$this->isMassageSent = isset($response[2]) && $response[2] === 'OK';

if (!$this->isMassageSent) {
throw new \Exception('OTP not sent, response: ' . json_encode($response));
}
} catch (Throwable $ex) {
$class = self::class;
\Log::error("Error in `$class`: " . $ex->getMessage());
}
}

protected function initWhatsApp(): void
{
$this->ws = new Whatsapp(
$this->getPhoneNumberId(),
$this->getAccessToken(),
$this->getGraphVersion()
);
}

protected function getConfigValue(string $key): ?string
{
return config("sendables.services.whatsapp.$key");
}
}
10 changes: 10 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ public static function removeLeadingPlus($value)
{
return ltrim($value, '+');
}

/**
* Get app current locale
*
* @return string
*/
public static function getCurrentLocale()
{
return config('app.locale');
}
}

0 comments on commit a35932e

Please sign in to comment.