-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3a931e
commit a35932e
Showing
6 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters