Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nikunj-7span committed Jul 11, 2022
1 parent 700a973 commit facf1f3
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Facades/WhatsApp.php
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';
}
}
18 changes: 18 additions & 0 deletions src/Providers/WhatsAppServiceProvider.php
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();
});
}
}
84 changes: 84 additions & 0 deletions src/WhatsApp.php
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;
}
}
13 changes: 13 additions & 0 deletions src/WhatsAppInterface.php
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);
}

0 comments on commit facf1f3

Please sign in to comment.