> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hypersender.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel Rust WhatsApp Webhooks and Events

> Receive WhatsApp webhooks in Laravel Rust as events: incoming messages, delivery receipts, deletions and poll votes, plus a WhatsApp auto-reply bot.

## Overview

The service provider adds a webhook route to your app's `api` group:

```
POST /api/whatsapp/webhook
```

When a request arrives, the package checks the secret, reads the `event` name from the body, and dispatches the matching Laravel event in the background. It answers Hypersender with `200` right away, so slow listeners never cause a timeout.

Routes in the `api` group skip CSRF protection, so Hypersender can post to it without a token.

## Set up the webhook

**1. Give Hypersender a public URL**

Hypersender can't reach `localhost`. In development, expose your app with a tunnel such as ngrok (`ngrok http 8000`) and use `https://<your-tunnel>/api/whatsapp/webhook`.

**2. Add the webhook in the dashboard**

Open your WhatsApp instance in the [Hypersender dashboard](https://app.hypersender.com), add the URL and pick the events you want.

**3. Set a secret (recommended)**

If you set an **Authorization Secret** on the webhook, put the same value in `.env`. Requests without it get a `401`.

```ini .env theme={null}
HYPERSENDER_WHATSAPP_WEBHOOK_AUTHORIZATION_SECRET=your-secret
```

To use another path, set `HYPERSENDER_WHATSAPP_WEBHOOK_ROUTE=/hooks/whatsapp`. The route stays under `/api`, so this one becomes `/api/hooks/whatsapp`.

## Events

Every event is a struct in `hypersender_laravel_rust::events` with one field, `payload`, holding the full JSON body Hypersender sent.

| Webhook | Event | Sent when |
| - | - | - |
| `message.any` | `MessageAny` | A message is created in any chat, including the ones you send |
| `message.ack` | `MessageAck` | A message you sent is delivered or read |
| `message.revoked` | `MessageRevoked` | Someone deletes a message for everyone |
| `message.waiting` | `MessageWaiting` | WhatsApp shows "waiting for this message" |
| `message.reaction` | `MessageReaction` | Someone reacts to a message |
| `presence.update` | `PresenceUpdate` | A contact comes online or starts typing |
| `poll.vote` | `PollVote` | Someone votes in your poll |
| `poll.vote.failed` | `PollVoteFailed` | A poll vote couldn't be decrypted |

A request with an unknown `event` gets a `422` and dispatches nothing. Payload fields for each event are in the [WhatsApp webhooks reference](https://docs.hypersender.com/v2/api-reference/whatsapp-webhooks/whatsapp-webhooks-introduction.md).

The body looks like this, so the message itself is at `payload["data"]["payload"]`:

```json theme={null}
{
  "event": "message.any",
  "data": {
    "event": "message.any",
    "payload": {
      "id": "false_20123456789@c.us_3EB0C767D26A1D3E",
      "from": "20123456789@c.us",
      "fromMe": false,
      "body": "Hi, is my order ready?",
      "hasMedia": false
    }
  }
}
```

## Listen for an event

Write a listener the way you would for any Laravel Rust event. Your app discovers `#[listener]` functions in its own crate automatically.

```rust src/listeners/log_receipts.rs theme={null}
use hypersender_laravel_rust::events::MessageAck;
use laravel::event::{Event, listener};

#[listener]
pub async fn log_receipt(event: Event<MessageAck>) {
    tracing::info!(payload = %event.payload, "WhatsApp receipt");
}
```

## Build a WhatsApp auto-reply bot

This listener answers every incoming message. It injects `Whatsapp` to send the reply.

```rust src/listeners/auto_reply.rs theme={null}
use hypersender_laravel_rust::Whatsapp;
use hypersender_laravel_rust::events::MessageAny;
use laravel::container::Inject;
use laravel::event::{Event, listener};

#[listener]
pub async fn auto_reply(event: Event<MessageAny>, whatsapp: Inject<Whatsapp>) -> laravel::Result<()> {
    let message = &event.payload["data"]["payload"];

    // message.any also fires for messages you send, so skip those.
    if message["fromMe"] == true {
        return Ok(());
    }

    let chat = message["from"].as_str().unwrap_or_default();
    whatsapp.send_text(chat, "Thanks, we got your message").await?;

    Ok(())
}
```

> [!WARNING]
>
> Always skip `fromMe` messages in a `MessageAny` listener that sends messages. Your own reply fires `message.any` too, and without the check the bot answers itself forever.

## Queued listeners

The webhook runs listeners in a background task. Background tasks live in memory and aren't retried, so a crash or deploy can lose one. For work that must finish, such as writing to your database or calling another API, use a queued listener. Every event derives `Queueable`:

```rust theme={null}
use hypersender_laravel_rust::events::MessageAny;
use laravel::event::{Event, listener};

#[listener(queued)]
pub async fn save_message(event: Event<MessageAny>) -> laravel::Result<()> {
    // Runs on a queue worker, with retries.
    Ok(())
}
```

Start a worker as described in the [Laravel Rust queue docs](https://laravel.com/rust/docs/queues).

## Test your listeners

`event::fake()` records events instead of running listeners:

```rust theme={null}
use hypersender_laravel_rust::events::MessageAny;
use laravel::event;
use laravel::testing::prelude::*;
use serde_json::json;

#[laravel::test]
async fn incoming_messages_dispatch_an_event(app: Application) {
    let events = event::fake();

    app.post("/api/whatsapp/webhook")
        .json(json!({ "event": "message.any", "data": { "payload": { "body": "hi" } } }))
        .await
        .assert_ok();

    events.assert_dispatched_where::<MessageAny>(|event| event.payload["data"]["payload"]["body"] == "hi");
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.