Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-a-elngar committed Jan 23, 2023
1 parent f5709c9 commit a0fd6e3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "elngar/whatsapp",
"description": "helper to send whatsapp messages",
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
"require": {},
"authors": [
{
"name": "Ahmed AbdElaziz",
"email": "a.elngar965@gmail.com"
}
],
"autoload": {
"psr-4": {
"Elngar\\Whatsapp\\": "src/"
}
}
}
51 changes: 51 additions & 0 deletions src/Services/Whatsapp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Elngar\Whatsapp\Services;

use Illuminate\Support\Facades\Http;

class Whatsapp
{
static protected array $data;
static protected mixed $result;

static public function send( string $receiver, string $content)
{
self::prepareData($receiver, $content);

return self::sendWithResponse();
}

static protected function prepareData($receiver, $content) :void
{
self::$data= [
"messaging_product" => "whatsapp",
"to" => $receiver,
"type" => "text",
"text" => json_encode([ 'body' => $content])
];

}

static protected function getUrl() :string
{
return "https://graph.facebook.com/v15.0/". config('whatsapp.phone_number_id') ."/messages";
}

static protected function sendWithResponse() :bool
{
self::$result = Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . config('whatsapp.access_token')
])->post(self::getUrl(), self::$data);

return self::$result->successful();
}

static public function getSendResult()
{
return self::$result->successful() ? json_decode( self::$result->body() )->messages : json_decode( self::$result->body() )->error->message;
}

}
20 changes: 20 additions & 0 deletions src/WhatsappServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Elngar\Whatsapp;

use Illuminate\Support\ServiceProvider;

class WhatsappServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__ . '/config/whatsapp.php' => config_path('whatsapp.php')
]);
}

public function register()
{

}
}
6 changes: 6 additions & 0 deletions src/config/whatsapp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
"access_token" => '',
"phone_number_id" => ''
];

0 comments on commit a0fd6e3

Please sign in to comment.