Facade
unknown
php
2 years ago
5.1 kB
10
Indexable
<?php
namespace App\Services\Provider;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
class WhatsAppProvider
{
private $whatsapp = [];
private $attachment = [];
private $body = [];
function __construct()
{
}
public function to($number)
{
$this->whatsapp['to'] = $number;
return $this;
}
public function type($type)
{
$this->whatsapp['type'] = $type;
return $this;
}
public function template($name, $language)
{
$this->whatsapp['template'] = [
"name" => $name,
"language" => ["code" => "$language"]
];
return $this;
}
public function attachment($type, $data)
{
$this->attachment[] = [
"type" => $type,
$type => $data
];
$this->setHeader();
return $this;
}
public function reply($id)
{
$this->whatsapp['type'] = [
"context" => [
"message_id" => $id
]
];
return $this;
}
public function sendDocument($link, $filename=null, $caption=null)
{
$this->whatsapp['type'] = "document";
$this->whatsapp['document'] = [
"link" => $link,
"filename" => $filename,
"caption" => $caption
];
return $this;
}
public function sendImage($link)
{
$this->whatsapp['type'] = "image";
$this->whatsapp['image'] = [
"link" => $link
];
return $this;
}
public function sendText($text, $preview=false)
{
$this->whatsapp['type'] = "text";
$this->whatsapp['text'] = [
"preview_url" => $preview,
"body" => $text
];
return $this;
}
public function text($text)
{
$this->body[] = [
"type" => "text",
"text" => $text
];
return $this;
}
public function currency($fallback, $code, $amount_1000)
{
$this->body[] = [
"type" => "currency",
"currency" => [
"fallback_value" => $fallback,
"code" => $code,
"amount_1000" => $amount_1000
]
];
return $this;
}
private function setHeader()
{
$this->whatsapp[$this->whatsapp['type']]['components'][] = [
"type" => "header",
"parameters" => $this->attachment
];
return $this;
}
public function addButtonCode($text)
{
$this->whatsapp[$this->whatsapp['type']]['components'][] = [
"type" => "button",
"sub_type" => "url",
"index" => 0,
"parameters" => [
0 => [
"type" => "text",
"text" => $text
]
]
];
return $this;
}
public function prepare()
{
$this->whatsapp[$this->whatsapp['type']]['components'][] = [
"type" => "body",
"parameters" => $this->body
];
return $this;
}
public function dd()
{
dd($this->whatsapp);
}
public function getMediaProfile()
{
$this->whatsapp['messaging_product'] = 'whatsapp';
$this->whatsapp['recipient_type'] = 'individual';
$response = Http::withToken(config('app.access_waba'))
->acceptJson()
->accept('application/json')
->get('https://graph.facebook.com/v17.0/5515996603199/media');
return $this;
}
public function getMedia($id, $tenant_id='') {
$this->whatsapp['messaging_product'] = 'whatsapp';
$this->whatsapp['recipient_type'] = 'individual';
$response = Http::withToken(config('app.access_waba'))
->acceptJson()
->accept('application/json')
->get('https://graph.facebook.com/v17.0/'.$id);
$response = json_decode($response->body());
$type = explode("/", $response->mime_type);
$file = Http::withToken(config('app.access_waba'))->withHeaders([
'accept' => 'application/octet-stream',
])->get($response->url);
$file_name = $tenant_id.'/'.$type[0].'/'.time().'.'.$type[1];
$file = Storage::put($file_name, $file->body());
return $file_name;
}
public function sendMessage()
{
$this->whatsapp['messaging_product'] = 'whatsapp';
$this->whatsapp['recipient_type'] = 'individual';
$response = Http::withToken(config('app.access_waba'))
->acceptJson()
->accept('application/json')
->post('https://graph.facebook.com/v17.0/200173996516071/messages', $this->whatsapp);
return $response;
}
}
Editor is loading...
Leave a Comment