Esempi pronti
Casi d'uso tipici di gestionali e software verticali. Il template va prima creato e approvato (vedi Template); in sandbox è approvato subito.
Promemoria appuntamento
Template UTILITY con nome e data. Mandalo dal gestionale il giorno prima.
<?php
// Richiede PHP 7.4+ con estensione curl.
$apiKey = getenv('DPL_API_KEY'); // dpl_live_... oppure dpl_test_...
$payload = [
'customer_id' => 'cus_xxxxxxxxxxxx',
'to' => '+393331234567',
'type' => 'template',
'template' => [
'name' => 'promemoria_appuntamento',
'language' => 'it',
// {{1}} = nome, {{2}} = data e ora
'variables' => ['Maria', 'domani alle 10:00'],
],
];
// Idempotency-Key: un id univoco dell'evento nel tuo gestionale (es. appuntamento 123).
// Le chiavi non scadono: la stessa chiave restituisce sempre lo stesso messaggio, anche se fallito.
// Per un nuovo invio usa una chiave nuova.
$idempotencyKey = 'appuntamento-123-promemoria';
$ch = curl_init('https://api.depalmalabs.it/v1/messages');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Idempotency-Key: ' . $idempotencyKey,
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$netError = curl_errno($ch) ? curl_error($ch) : null;
curl_close($ch);
if ($netError !== null || $status === 0) {
// Rete o timeout: il messaggio potrebbe essere partito. Riprova con la STESSA Idempotency-Key.
echo "Esito incerto ($netError): riprova con la stessa chiave\n";
exit;
}
$data = json_decode($response, true);
if ($status === 202) {
echo "Esito da confermare: non rimandare, attendi il webhook\n";
} elseif (($status === 201 || $status === 200) && $data['status'] === 'failed') {
// Con 200 è un replay: questa chiave appartiene a un invio già fallito. Correggi e usa una chiave nuova.
echo "Invio fallito: " . json_encode($data['error']) . "\n";
} elseif ($status === 201 || $status === 200) {
echo "Inviato: " . $data['id'] . "\n"; // salva l'id per seguire lo stato
} else {
echo "Errore " . ($data['error']['code'] ?? $status) . ": " . ($data['error']['message'] ?? '') . "\n";
}Notifica fattura emessa
Template UTILITY con intestazione documento: il PDF arriva direttamente in chat. Il link deve essere https e scaricabile senza login.
<?php
// Richiede PHP 7.4+ con estensione curl.
$apiKey = getenv('DPL_API_KEY'); // dpl_live_... oppure dpl_test_...
$payload = [
'customer_id' => 'cus_xxxxxxxxxxxx',
'to' => '+393331234567',
'type' => 'template',
'template' => [
'name' => 'fattura_emessa',
'language' => 'it',
// Template con intestazione DOCUMENTO: il PDF arriva in chat.
'components' => [
['type' => 'header', 'parameters' => [[
'type' => 'document',
'document' => ['link' => 'https://tuo-gestionale.it/fatture/123.pdf', 'filename' => 'Fattura 123.pdf'],
]]],
['type' => 'body', 'parameters' => [
['type' => 'text', 'text' => '123/2026'],
['type' => 'text', 'text' => '89,00 €'],
]],
],
],
];
// Idempotency-Key: un id univoco dell'evento nel tuo gestionale (es. appuntamento 123).
// Le chiavi non scadono: la stessa chiave restituisce sempre lo stesso messaggio, anche se fallito.
// Per un nuovo invio usa una chiave nuova.
$idempotencyKey = 'appuntamento-123-promemoria';
$ch = curl_init('https://api.depalmalabs.it/v1/messages');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Idempotency-Key: ' . $idempotencyKey,
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$netError = curl_errno($ch) ? curl_error($ch) : null;
curl_close($ch);
if ($netError !== null || $status === 0) {
// Rete o timeout: il messaggio potrebbe essere partito. Riprova con la STESSA Idempotency-Key.
echo "Esito incerto ($netError): riprova con la stessa chiave\n";
exit;
}
$data = json_decode($response, true);
if ($status === 202) {
echo "Esito da confermare: non rimandare, attendi il webhook\n";
} elseif (($status === 201 || $status === 200) && $data['status'] === 'failed') {
// Con 200 è un replay: questa chiave appartiene a un invio già fallito. Correggi e usa una chiave nuova.
echo "Invio fallito: " . json_encode($data['error']) . "\n";
} elseif ($status === 201 || $status === 200) {
echo "Inviato: " . $data['id'] . "\n"; // salva l'id per seguire lo stato
} else {
echo "Errore " . ($data['error']['code'] ?? $status) . ": " . ($data['error']['message'] ?? '') . "\n";
}Conferma ordine
Template UTILITY con numero ordine e data di consegna.
<?php
// Richiede PHP 7.4+ con estensione curl.
$apiKey = getenv('DPL_API_KEY'); // dpl_live_... oppure dpl_test_...
$payload = [
'customer_id' => 'cus_xxxxxxxxxxxx',
'to' => '+393331234567',
'type' => 'template',
'template' => [
'name' => 'conferma_ordine',
'language' => 'it',
'variables' => ['A-1024', 'giovedì 8 ottobre'],
],
];
// Idempotency-Key: un id univoco dell'evento nel tuo gestionale (es. appuntamento 123).
// Le chiavi non scadono: la stessa chiave restituisce sempre lo stesso messaggio, anche se fallito.
// Per un nuovo invio usa una chiave nuova.
$idempotencyKey = 'appuntamento-123-promemoria';
$ch = curl_init('https://api.depalmalabs.it/v1/messages');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Idempotency-Key: ' . $idempotencyKey,
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$netError = curl_errno($ch) ? curl_error($ch) : null;
curl_close($ch);
if ($netError !== null || $status === 0) {
// Rete o timeout: il messaggio potrebbe essere partito. Riprova con la STESSA Idempotency-Key.
echo "Esito incerto ($netError): riprova con la stessa chiave\n";
exit;
}
$data = json_decode($response, true);
if ($status === 202) {
echo "Esito da confermare: non rimandare, attendi il webhook\n";
} elseif (($status === 201 || $status === 200) && $data['status'] === 'failed') {
// Con 200 è un replay: questa chiave appartiene a un invio già fallito. Correggi e usa una chiave nuova.
echo "Invio fallito: " . json_encode($data['error']) . "\n";
} elseif ($status === 201 || $status === 200) {
echo "Inviato: " . $data['id'] . "\n"; // salva l'id per seguire lo stato
} else {
echo "Errore " . ($data['error']['code'] ?? $status) . ": " . ($data['error']['message'] ?? '') . "\n";
}Codice OTP
Template AUTHENTICATION con pulsante “copia codice”: il codice va sia nel corpo sia nel parametro del pulsante.
<?php
// Richiede PHP 7.4+ con estensione curl.
$apiKey = getenv('DPL_API_KEY'); // dpl_live_... oppure dpl_test_...
$payload = [
'customer_id' => 'cus_xxxxxxxxxxxx',
'to' => '+393331234567',
'type' => 'template',
'template' => [
'name' => 'codice_accesso', // categoria AUTHENTICATION con pulsante "copia codice"
'language' => 'it',
'components' => [
['type' => 'body', 'parameters' => [['type' => 'text', 'text' => '482913']]],
['type' => 'button', 'sub_type' => 'url', 'index' => '0', 'parameters' => [['type' => 'text', 'text' => '482913']]],
],
],
];
// Idempotency-Key: un id univoco dell'evento nel tuo gestionale (es. appuntamento 123).
// Le chiavi non scadono: la stessa chiave restituisce sempre lo stesso messaggio, anche se fallito.
// Per un nuovo invio usa una chiave nuova.
$idempotencyKey = 'appuntamento-123-promemoria';
$ch = curl_init('https://api.depalmalabs.it/v1/messages');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Idempotency-Key: ' . $idempotencyKey,
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$netError = curl_errno($ch) ? curl_error($ch) : null;
curl_close($ch);
if ($netError !== null || $status === 0) {
// Rete o timeout: il messaggio potrebbe essere partito. Riprova con la STESSA Idempotency-Key.
echo "Esito incerto ($netError): riprova con la stessa chiave\n";
exit;
}
$data = json_decode($response, true);
if ($status === 202) {
echo "Esito da confermare: non rimandare, attendi il webhook\n";
} elseif (($status === 201 || $status === 200) && $data['status'] === 'failed') {
// Con 200 è un replay: questa chiave appartiene a un invio già fallito. Correggi e usa una chiave nuova.
echo "Invio fallito: " . json_encode($data['error']) . "\n";
} elseif ($status === 201 || $status === 200) {
echo "Inviato: " . $data['id'] . "\n"; // salva l'id per seguire lo stato
} else {
echo "Errore " . ($data['error']['code'] ?? $status) . ": " . ($data['error']['message'] ?? '') . "\n";
}