<?php
/**
 * Example webhook endpoint for Tavros Connect (PHP 7.0+).
 * 1. Put this file on your server, e.g. https://your-app.example.com/whatsapp/webhook.php
 * 2. Tavros Connect → Developers → Webhook: enter that URL, the verify token and the
 *    signing secret below, then click "Verify and save" and "Send test event".
 */
require __DIR__ . '/TavrosConnect.php';

const VERIFY_TOKEN   = 'choose-any-verify-token';      // same as entered in Tavros Connect
const SIGNING_SECRET = 'paste-your-signing-secret';     // same as entered in Tavros Connect

TavrosConnect::handleWebhook(VERIFY_TOKEN, SIGNING_SECRET, function (array $e) {
    switch ($e['type']) {
        case 'message':
            // A customer wrote to you.
            //   $e['from'] = "919876543210", $e['name'] = WhatsApp profile name,
            //   $e['text'] = text / caption / button title (null for e.g. stickers),
            //   $e['message_type'] = text|image|document|button|interactive|…, $e['message'] = full Meta object.
            // Received photos/files: $e['message'][$e['message_type']]['id'] → $tc->downloadMedia($id)
            error_log("WhatsApp from {$e['from']}: {$e['text']}");
            break;

        case 'status':
            // Delivery update for a message sent from anywhere (your software, Tavros Connect Chats, campaigns).
            //   $e['wamid'] matches the 'wamid' returned when you sent it; $e['status'] = sent|delivered|read|failed
            //   $e['error'] = reason when failed
            error_log("WhatsApp {$e['wamid']} is now {$e['status']}" . ($e['error'] ? " ({$e['error']})" : ''));
            break;

        default:
            // e.g. 'message_template_status_update': $e['value']['event'] = APPROVED|REJECTED|…
            break;
    }
});
