full
This commit is contained in:
@@ -181,6 +181,137 @@ class WhatsAppService
|
||||
return $this->makeRequest('POST', $url, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar imagen
|
||||
*/
|
||||
public function sendImageMessage($to, $imageUrl, $caption = null)
|
||||
{
|
||||
$image = ['link' => $imageUrl];
|
||||
|
||||
if ($caption) {
|
||||
$image['caption'] = $caption;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->formatPhoneNumber($to),
|
||||
'type' => 'image',
|
||||
'image' => $image
|
||||
];
|
||||
|
||||
return $this->sendMessage($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar video
|
||||
*/
|
||||
public function sendVideoMessage($to, $videoUrl, $caption = null)
|
||||
{
|
||||
$video = ['link' => $videoUrl];
|
||||
|
||||
if ($caption) {
|
||||
$video['caption'] = $caption;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->formatPhoneNumber($to),
|
||||
'type' => 'video',
|
||||
'video' => $video
|
||||
];
|
||||
|
||||
return $this->sendMessage($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar audio
|
||||
*/
|
||||
public function sendAudioMessage($to, $audioUrl)
|
||||
{
|
||||
$data = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->formatPhoneNumber($to),
|
||||
'type' => 'audio',
|
||||
'audio' => [
|
||||
'link' => $audioUrl
|
||||
]
|
||||
];
|
||||
|
||||
return $this->sendMessage($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar documento
|
||||
*/
|
||||
public function sendDocumentMessage($to, $documentUrl, $filename = null, $caption = null)
|
||||
{
|
||||
$document = ['link' => $documentUrl];
|
||||
|
||||
if ($filename) {
|
||||
$document['filename'] = $filename;
|
||||
}
|
||||
|
||||
if ($caption) {
|
||||
$document['caption'] = $caption;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->formatPhoneNumber($to),
|
||||
'type' => 'document',
|
||||
'document' => $document
|
||||
];
|
||||
|
||||
return $this->sendMessage($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subir archivo multimedia a WhatsApp
|
||||
*/
|
||||
public function uploadMedia($filePath, $mimeType)
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception("Archivo no encontrado: $filePath");
|
||||
}
|
||||
|
||||
$url = $this->apiUrl . $this->phoneNumberId . '/media';
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
$postFields = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'file' => new CURLFile($filePath, $mimeType),
|
||||
'type' => $mimeType
|
||||
];
|
||||
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => $postFields,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Authorization: Bearer ' . $this->token
|
||||
],
|
||||
CURLOPT_TIMEOUT => 120
|
||||
]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200 && $httpCode !== 201) {
|
||||
throw new Exception("Error subiendo archivo: " . $response);
|
||||
}
|
||||
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if (!isset($result['id'])) {
|
||||
throw new Exception("No se obtuvo ID del archivo subido");
|
||||
}
|
||||
|
||||
return $result['id']; // Retorna el media_id
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar mensaje principal
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user