-
Notifications
You must be signed in to change notification settings - Fork 5
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
700a973
commit facf1f3
Showing
4 changed files
with
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SevenSpan\WhatsApp\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class WhatsApp extends Facade | ||
{ | ||
public static function getFacadeAccessor() | ||
{ | ||
return 'WhatsApp'; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SevenSpan\WhatsApp\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use SevenSpan\WhatsApp\WhatsApp; | ||
|
||
class WhatsAppServiceProvider extends ServiceProvider | ||
{ | ||
public function boot(): void | ||
{ | ||
$this->app->bind('WhatsApp', function () { | ||
return new WhatsApp(); | ||
}); | ||
} | ||
} |
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,84 @@ | ||
<?php | ||
|
||
namespace SevenSpan\WhatsApp; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
|
||
final class WhatsApp implements WhatsAppInterface | ||
{ | ||
/** | ||
* @return array|mixed | ||
*/ | ||
public function sendMessage(string $WhatsAppBussnessAccountId, string $accessToken, string $to, string $templateName, string $languageCode, string $message, string $from = null) | ||
{ | ||
$response = $this->getAccountNumbers($WhatsAppBussnessAccountId, $accessToken); | ||
|
||
// Throw an exception if a client or server error occurred... | ||
if (isset($response['error'])) { | ||
return $response; | ||
} | ||
$fromPhoneNumberId = $response['data'][0]['id']; // First phone number | ||
|
||
if (!empty($from)) { | ||
foreach ($response['data'] as $value) { | ||
if (str_replace(array('+', '-', ' ', ')', '('), '', $value['display_phone_number']) == $from) { | ||
$fromPhoneNumberId = $value['id']; | ||
} | ||
} | ||
} | ||
|
||
$postInput = [ | ||
'messaging_product' => 'whatsapp', | ||
'to' => $to, | ||
'type' => 'template', | ||
'template' => [ | ||
'name' => $templateName, | ||
'language' => [ | ||
'code' => $languageCode, | ||
], | ||
'components' => [ | ||
[ | ||
'type' => 'body', | ||
'parameters' => $this->getParametersData($message) | ||
], | ||
], | ||
], | ||
]; | ||
$headers = [ | ||
'Authorization' => 'Bearer ' . $accessToken, | ||
]; | ||
$response = Http::withHeaders($headers)->post('https://graph.facebook.com/v12.0/' . $fromPhoneNumberId . '/messages', $postInput); | ||
$response = json_decode($response->getBody(), true); | ||
return $response; | ||
} | ||
|
||
/* | ||
* @return array|mixed | ||
*/ | ||
private function getAccountNumbers($WhatsAppBussnessAccountId, $accessToken) | ||
{ | ||
$response = Http::get('https://graph.facebook.com/v14.0/' . $WhatsAppBussnessAccountId . '/phone_numbers?access_token=' . $accessToken); | ||
return json_decode($response->getBody(), true); | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* | ||
* @return array|mixed | ||
*/ | ||
private function getParametersData($message) | ||
{ | ||
$parameter = explode("~", $message); | ||
$parameters = []; | ||
foreach ($parameter as $value) { | ||
array_push( | ||
$parameters, | ||
[ | ||
'type' => 'text', | ||
'text' => $value | ||
] | ||
); | ||
} | ||
return $parameters; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SevenSpan\WhatsApp; | ||
|
||
interface WhatsAppInterface | ||
{ | ||
/** | ||
* @return array|mixed | ||
*/ | ||
public function sendMessage(string $WhatsAppBussnessAccountId, string $accessToken, string $to, string $templateName, string $languageCode, string $message); | ||
} |