@extends('root') @section('title', 'Test Send Template') @push('styles') @endpush @section('content')
Isi nomor WhatsApp tujuan dan parameter sesuai struktur template. Form dihasilkan otomatis dari template Meta.
<?php
/**
* Contoh Script Kirim Template WhatsApp
* Template: {{ $template->effectiveTemplateName() }}
* API Endpoint: POST /api/v1/send-template
*/
$apiKey = 'YOUR_API_KEY'; // Dapatkan dari menu Developer > API Keys
$endpoint = '{{ url('/api/v1/send-template') }}';
$payload = [
'phone_number' => '628123456789', // Nomor tujuan (Format: 628xxx)
'to_name' => 'John Doe', // Optional: Menyimpan nama kontak
'name_template' => '{{ $template->effectiveTemplateName() }}',
'language_code' => '{{ $template->language }}',
@if(count($placeholders) > 0)
// Body Parameters (Urutan harus sesuai {{1}}, {{2}}, dst)
'body_params' => [
@foreach($placeholders as $idx => $placeholder)
'{{ $placeholder }}', // Parameter {{ $loop->iteration }}
@endforeach
],
@endif
@if(count($headerPlaceholders) > 0)
// Header Parameters (Text)
'header_params' => [
@foreach($headerPlaceholders as $idx => $placeholder)
'{{ $placeholder }}', // Parameter {{ $loop->iteration }}
@endforeach
],
@elseif($headerInfo['type'] === 'media')
// Header Media URL ({{ ucfirst($headerInfo['format'] ?? 'IMAGE') }})
'header_media_url' => 'https://example.com/file.{{ strtolower($headerInfo['format'] === 'IMAGE' ? 'jpg' : 'mp4') }}',
@endif
@if(collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)
// Button URL Parameters (Dynamic URL)
'button_params' => [
@foreach($buttonInfo as $button)
@if(count($button['url_vars']) > 0)
'{{ $button['index'] }}' => ['param_value'], // Index {{ $button['index'] }}: {{ $button['text'] }}
@endif
@endforeach
],
@endif
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $endpoint,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'X-API-KEY: Bearer ' . $apiKey,
],
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
if ($httpCode === 200 && ($result['status'] ?? '') === 'success') {
echo "Success! Message ID: " . $result['message_id'];
} else {
echo "Failed: " . ($result['message'] ?? 'Unknown error');
}
const API_KEY = 'YOUR_API_KEY';
const ENDPOINT = '{{ url('/api/v1/send-template') }}';
const payload = {
phone_number: '628123456789',
to_name: 'John Doe',
name_template: '{{ $template->effectiveTemplateName() }}',
language_code: '{{ $template->language }}',
@if(count($placeholders) > 0)
body_params: [
@foreach($placeholders as $idx => $placeholder)
'{{ $placeholder }}', // Parameter {{ $loop->iteration }}
@endforeach
],
@endif
@if(count($headerPlaceholders) > 0)
header_params: [
@foreach($headerPlaceholders as $idx => $placeholder)
'{{ $placeholder }}', // Parameter {{ $loop->iteration }}
@endforeach
],
@elseif($headerInfo['type'] === 'media')
header_media_url: 'https://example.com/file.{{ strtolower($headerInfo['format'] === 'IMAGE' ? 'jpg' : 'mp4') }}',
@endif
@if(collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)
button_params: {
@foreach($buttonInfo as $button)
@if(count($button['url_vars']) > 0)
"{{ $button['index'] }}": ["param_value"],
@endif
@endforeach
}
@endif
};
fetch(ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-KEY': `Bearer ${API_KEY}`
},
body: JSON.stringify(payload)
})
.then(desc => desc.json())
.then(res => console.log(res))
.catch(err => console.error(err));
import requests
API_KEY = 'YOUR_API_KEY'
ENDPOINT = '{{ url('/api/v1/send-template') }}'
payload = {
"phone_number": "628123456789",
"to_name": "John Doe",
"name_template": "{{ $template->effectiveTemplateName() }}",
"language_code": "{{ $template->language }}",
@if(count($placeholders) > 0)
"body_params": [
@foreach($placeholders as $idx => $placeholder)
"{{ $placeholder }}", # Parameter {{ $loop->iteration }}
@endforeach
],
@endif
@if(count($headerPlaceholders) > 0)
"header_params": [
@foreach($headerPlaceholders as $idx => $placeholder)
"{{ $placeholder }}", # Parameter {{ $loop->iteration }}
@endforeach
],
@elseif($headerInfo['type'] === 'media')
"header_media_url": "https://example.com/file.{{ strtolower($headerInfo['format'] === 'IMAGE' ? 'jpg' : 'mp4') }}",
@endif
@if(collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)
"button_params": {
@foreach($buttonInfo as $button)
@if(count($button['url_vars']) > 0)
"{{ $button['index'] }}": ["param_value"],
@endif
@endforeach
}
@endif
}
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"X-API-KEY": f"Bearer {API_KEY}"
}
response = requests.post(ENDPOINT, json=payload, headers=headers)
print(response.json())
POST {{ url('/api/v1/send-template') }}
{
"Content-Type": "application/json",
"Accept": "application/json",
"X-API-KEY": "Bearer YOUR_API_KEY"
}
{
"phone_number": "628123456789",
"to_name": "John Doe",
"name_template": "{{ $template->effectiveTemplateName() }}",
"language_code": "{{ $template->language }}"@if(count($placeholders) > 0 || count($headerPlaceholders) > 0 || $headerInfo['type'] === 'media' || (collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)),@endif
@if(count($placeholders) > 0)
"body_params": [
@foreach($placeholders as $idx => $placeholder)
"{{ $placeholder }}"@if(!$loop->last), @endif
@endforeach
]@if(count($headerPlaceholders) > 0 || $headerInfo['type'] === 'media' || (collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)),@endif
@endif
@if(count($headerPlaceholders) > 0)
"header_params": [
@foreach($headerPlaceholders as $idx => $placeholder)
"{{ $placeholder }}"@if(!$loop->last), @endif
@endforeach
]@if(count($headerPlaceholders) > 0 && (collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)),@endif
@elseif($headerInfo['type'] === 'media')
"header_media_url": "https://example.com/file.{{ strtolower($headerInfo['format'] === 'IMAGE' ? 'jpg' : 'mp4') }}"@if(collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0),@endif
@endif
@if(collect($buttonInfo)->filter(fn($b) => count($b['url_vars']) > 0)->count() > 0)
"button_params": {
@foreach($buttonInfo as $button)
@if(count($button['url_vars']) > 0)
"{{ $button['index'] }}": ["param_value"]@if(!$loop->last),@endif
@endif
@endforeach
}
@endif
}