## Callback: automation.action

`POST <your callback URL>`

An automation reached a `notify` step for a contact - your product's turn to act (create a follow-up, alert the bursar...). `data` is what you put on the step.

### Headers

| Name | Description |
|---|---|
| `X-PaalChat-Event` |  |
| `X-PaalChat-Delivery` | Unique per event (equals body `id`). De-duplicate on it. |
| `X-PaalChat-Signature` | `t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`. Reject if invalid or older than 300 seconds. |

### Body

| Field | Type | Description |
|---|---|---|
| `id` | string | Equals X-PaalChat-Delivery. |
| `sandbox` | boolean | true for sandbox businesses (test keys) - nothing reached WhatsApp. |
| `event` | any |  |
| `occurred_at` | string |  |
| `business` | object |  |
| `business.external_id` | string |  |
| `data` | object |  |
| `data.automation_id` | integer |  |
| `data.run_id` | integer |  |
| `data.contact_id` | integer |  |
| `data.contact_external_id` | string | null |  |
| `data.conversation_id` | integer | null |  |
| `data.data` | object | The notify step's own data. |

### Example

```json
{
  "id": "2e3f4a5b-6c7d-4e8f-9a0b-1c2d3e4f5a6b",
  "event": "automation.action",
  "occurred_at": "2026-11-02T08:00:04+00:00",
  "business": {"external_id": "presec"},
  "sandbox": false,
  "data": {
    "automation_id": 3,
    "run_id": 881,
    "contact_id": 41,
    "contact_external_id": "parent-77",
    "conversation_id": 7,
    "data": {"reason": "fees_overdue"}
  }
}
```

### Verify and handle (PHP)

```php
// routes/api.php: Route::post('/paalchat/callback', PaalChatCallbackController::class);
public function __invoke(Request $request)
{
    $header = (string) $request->header('X-PaalChat-Signature');
    $raw = $request->getContent();

    if (! preg_match('/^t=(\d+),v1=([a-f0-9]{64})$/', $header, $m)
        || abs(time() - (int) $m[1]) > 300
        || ! hash_equals(hash_hmac('sha256', $m[1].'.'.$raw, config('services.paalchat.callback_secret')), $m[2])) {
        abort(401);
    }

    // Process each delivery once.
    if (! Cache::add('paalchat:'.$request->header('X-PaalChat-Delivery'), true, now()->addDays(2))) {
        return response()->noContent();
    }

    $event = json_decode($raw, true);

    if ($event['event'] === 'automation.action') {
        FollowUp::create(['paalchat_contact_id' => $event['data']['contact_id'], 'details' => $event['data']['data']]);
    }

    return response()->noContent();
}
```
