# Automations

React automatically to a message, a contact or a campaign.

An automation reacts on its own to an event: a customer writes, a contact is created, a campaign ends. It checks conditions, then runs up to five actions. It belongs to one number (`connectionId`).

Manage automations from the portal (Automations menu) or through the API. You need the `automations.manage` permission to create or edit, `automations.view` to read.

## Triggers

| `trigger` | When |
|---|---|
| `message_received` | A customer sends a message to the number. |
| `contact_created` | A new contact is added. |
| `campaign_completed` | A campaign on the number finishes. |
| `conversation_unanswered` | An open conversation has been waiting for a reply longer than the delay you set (checked every 5 minutes). |

## Conditions

Up to five conditions, all must be true. With none, the automation fires on every event.

| `type` | `value` |
|---|---|
| `text_contains` | The message contains this text, case-insensitive. |
| `has_tag` | The contact has this tag. |
| `not_has_tag` | The contact does not have this tag. |
| `unanswered_minutes` | Minutes to wait before the alert (5 to 10,080, default 60). For the `conversation_unanswered` trigger. |

## Actions

Up to five actions, run in order.

| `type` | Fields | Effect |
|---|---|---|
| `send_smart` | `text`, `template_name`, `template_language` | Sends `text` if the [24-hour window](/en/docs/messages) is open, otherwise the template. Both are required. |
| `send_template` | `template_name`, `template_language`, `template_components` | Sends an approved template. |
| `add_tag` | `tag` | Adds a tag to the contact. |
| `notify_team` | `title`, `body` | Sends a notification to team members. |
| `webhook` | `url` | Calls your server with a JSON `POST`. The address must be `https://`. |
| `send_flow` | `flow_id`, `body`, `button`, `mode` | Sends a WhatsApp Flow form. `flow_id` is the `flw_...` id of one of your forms. `mode` is `navigate` (default) or `data_exchange`. |
| `assign_conversation` | `user_id` | Assigns the conversation to a team member who has access to the number. |

In `text`, `{{nom}}`, `{{name}}`, `{{phone}}`, `{{attente}}` (minutes waiting, for `conversation_unanswered`) and the contact's imported fields (`{{city}}`) are replaced. An opted-out contact (STOP) never receives a message: the action fails without sending anything.

## Unanswered conversation alert

A conversation is "waiting" when the customer has the last word. It stops waiting when an agent replies from the inbox, or someone replies from the phone (coexistence). Messages sent by a campaign or an automation do not count as a human reply. Each conversation triggers the alert only once per customer message.

The inbox has a "No reply" tab and shows the waiting time on each conversation.

## Create an automation

```json
POST /automations
{
  "connectionId": "con_...",
  "name": "Welcome",
  "trigger": "message_received",
  "conditions": [{ "type": "has_tag", "value": "customer" }],
  "actions": [
    { "type": "send_smart", "text": "Hello {{name}}, an agent will reply shortly.", "template_name": "welcome", "template_language": "en" },
    { "type": "add_tag", "tag": "has_written" }
  ]
}
```

`PATCH /automations/{id}` changes one field at a time; `enabled: false` pauses the automation. `DELETE /automations/{id}` removes it.

## What your webhook receives

The `webhook` action sends this body, with a maximum wait of 8 seconds:

```json
{
  "event": "automation.campaign_completed",
  "automationId": "auto_...",
  "connectionId": "con_...",
  "context": { },
  "contact": { "id": "...", "phone": "+237600000000", "name": "Awa", "tags": ["VIP"] },
  "at": "2026-11-25T10:00:00+00:00"
}
```

`contact` is `null` when the event is not tied to a contact (campaign end).

## Follow the runs

`GET /automations/{id}/runs` lists every run: `status` (`pending`, `running`, `succeeded`, `failed` or `skipped`), `context`, `result` per action and `error`. The portal shows the same history on the automation page.

## Limits

- An automation fires at most once every 30 seconds per contact, to prevent reply loops.
- It runs at most 200 times per hour.
- An event dropped by these limits leaves no run behind.
