Skip to Content

Email, WhatsApp and push

Send email, WhatsApp messages and push notifications from a backend with one call, using templates in English and Urdu.

This page shows how a backend sends a message through Floor, how templates work, and what has to be set up first for each channel. You need a backend (see Your first backend) and, for each channel, the setup in its section below.

Floor sends every message for the product: it fills the template, queues the message, retries what can be retried, and keeps a log for 30 days. One call covers all three channels:

ChannelGoes throughto
EmailAmazon SES's SMTP relay, signed with the sending domain's DKIM key by Flooran address
WhatsAppa phone number linked to Floor through OpenWAa phone number
PushExpo's push service, to Apple and Googleuser:<id>, or one Expo push token

1. Write a template

Templates belong to a project and keep the wording out of your code. Each has an English and an Urdu version (or one of them), and {{name}} variables. For street-lights' fault alert, sl_fault_alert.json:

{
  "channel": "email",
  "description": "A switch or gateway stopped answering",
  "content": {
    "en": { "subject": "Fault at {{device}}",
            "text": "Switch {{switch}} at {{device}} is not responding.",
            "html": "<p>Switch {{switch}} at <b>{{device}}</b> is not responding.</p>" },
    "ur": { "subject": "{{device}} پر خرابی",
            "text": "{{device}} پر سوئچ {{switch}} جواب نہیں دے رہا۔" }
  },
  "variables": [ { "name": "device", "required": true, "sample": "SL-GW-0217" },
                 { "name": "switch", "required": true, "sample": "3" } ]
}
floor templates put sl sl_fault_alert --file sl_fault_alert.json
floor templates test sl sl_fault_alert --to you@example.com      # sends it with the sample values
  • channel is email, whatsapp or push, and a template is sent only on its own channel. An email version needs a subject; a WhatsApp one only text; a push one uses subject as the title.
  • Variables. Values are HTML-escaped in the HTML body. A required variable must be present and non-empty; an unknown name becomes empty.
  • HTML. Without an html version the email is plain text; with one, it's sent with both.
  • Versions. Each save is the next version, and every message records the version it used.

In the console: the project's Messages › Templates, with English and Urdu side by side, a preview from the sample values, and send test.

2. Send it from the backend

use floor::prelude::*;

let sent = cx
    .notify(
        Notification::email("crew.lahore@example.com")
            .template("sl_fault_alert")
            .lang("ur")
            .var("device", "SL-GW-0217")
            .var("switch", 3)
            .idempotency_key("fault-SL-GW-0217-3"),
    )
    .await?;
// sent.id, sent.channel ("email"), sent.status ("queued")

cx.notify returns once Floor has queued the message, not when it's delivered. The other forms:

// WhatsApp: any common form of the number; local Pakistani numbers get +92.
// The template must be a WhatsApp one ("channel": "whatsapp").
cx.notify(
    Notification::whatsapp("+92 300 1234567")
        .template("sl_fault_whatsapp")
        .var("device", "SL-GW-0217")
        .var("switch", 3),
)
.await?;

// One-off text, no template.
cx.notify(
    Notification::email("ops@example.com")
        .subject("Gate 3 is open")
        .text("Opened by the guard at 18:02."),
)
.await?;

// Push to every device a person registered; data goes to the app.
cx.notify(
    Notification::push("user:42")
        .subject("Fault at SL-GW-0217")
        .text("Switch 3 is not responding")
        .data(serde_json::json!({ "screen": "fault", "device": "SL-GW-0217" })),
)
.await?;
Builder
Notification::email(to), ::whatsapp(to), ::push(to)the channel and recipient
.template(name), .var(name, value)a template and its variables
.lang("en" | "ur")the version; English by default, else the template's only one
.subject(s), .text(s), .html(s)a message without a template
.data(json)push only: JSON for the app
.idempotency_key(k)sending the same key again returns the first message and sends nothing new

Idempotency. Triggers are delivered at least once, so a handler can run twice for one fault. Give each message a key made from what it's about (fault-SL-GW-0217-3): the same key within 30 days returns the first message instead of sending another.

When Floor refuses. An unknown template, a missing required variable, a From address the project may not use, a project with no WhatsApp number, or a person with no push device: nothing is queued, and cx.notify returns an error. Returned from a route with ?, it answers 502.

3. Follow what happened

floor messages list --project sl --status failed
floor messages show <id>             # its timeline and bodies
floor messages retry <id>            # a failed one, with fresh attempts

In the console: Messages › Message log, filtered by status, with each message's timeline and a retry button.

Statuses only move forward:

  • Email: queued → sent (the relay took it), or failed.
  • WhatsApp: queued → sent → delivered → read, or failed.
  • Push: queued → sent (Expo took it) → delivered (Apple or Google took it), or failed.

Each change is also posted to the project's webhooks as message.status.

Email setup

Each product sends from its own subdomain, such as street-lights.example.com. A Floor operator adds the domain and a sender once:

floor email add-domain street-lights.example.com --project sl     # prints the DNS records
floor email add-sender alerts@street-lights.example.com --name "Street Lights" --project sl
floor email check-domain street-lights.example.com
  • DNS. Adding a domain makes a 2048-bit DKIM key and prints three TXT records: SPF (allowing Amazon SES), DKIM at floor._domainkey.<domain>, and DMARC at _dmarc.<domain>.
  • Until SPF and DKIM pass, mail from the domain waits in the queue, then goes out on its own. Floor checks the records every 10 minutes.
  • The From address. from is one of the project's senders; without it, the project's first sender.
  • Retries. Temporary refusals (4xx) and connection problems are retried after 1 min, 5 min, 15 min, 1 h and 3 h, then the message fails. Permanent refusals (5xx) fail at once.
  • Suppressions. When the relay says a recipient doesn't exist (550, 551, 553), the address goes on the suppression list and later mail to it fails without being sent. Lift it once the address works again: floor email unsuppress <address>. floor email suppressions lists them.

In the console: Messages › Email shows the relay, today's sends and failures, suppressions, domains with their DNS state, and senders.

WhatsApp setup

WhatsApp goes through phone numbers linked to Floor with OpenWA, an unofficial client: WhatsApp can restrict a number that uses it. Use dedicated SIMs, message people who expect it, and keep a backup number.

A Floor operator adds and links a number for the project:

floor whatsapp add --label "SL fault alerts" --project sl --purpose "Fault alerts and crew replies" --role primary
floor whatsapp link <id>                  # saves the QR code to whatsapp-qr.png
floor whatsapp warmup <id> --start midnight

What Floor enforces for each number, which shapes how fast messages go out:

  • Warm-up. A new number's daily cap rises over 14 days from 20 to its full cap (1,000 by default). Extra messages wait for the next day.
  • Pacing. 4 to 10 seconds, at random, between two sends.
  • Quiet hours (on by default). Sends only between 08:00 and 22:00 Pakistan time.
  • Failover. A project's numbers are tried primary first; a backup is used only when the ones before it can't send.

A number that isn't on WhatsApp fails at once. Replies to the project's numbers are kept in the message log and posted to the project's webhooks as message.received. In the console: Messages › WhatsApp.

Push setup

Push is for products with an Expo (React Native) app. The app gets its Expo push token (Notifications.getExpoPushTokenAsync()) and the product registers it with Floor for the signed-in person. Cx has no call for this yet, so register tokens with Floor's HTTP API and a service client's token (Sign-in):

PUT /v1/projects/sl/push/tokens
{ "token": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]", "user": "42",
  "platform": "android", "device": "Pixel 8" }
  • user is the product's own id for the person; one person can have several devices. Send to all of them with to: "user:42".
  • On sign-out, remove the device: DELETE /v1/projects/sl/push/tokens/ExponentPushToken[…].
  • Retired devices. When Expo says the app is gone from a device, Floor stops sending to it until the app registers again.
  • Size. Title, text and data together are at most about 4 KB.

floor push tokens sl --user 42 lists a person's devices.

Testing

floor::testing::TestApp's fake Floor takes every notify call and records it; t.sent() returns them. See Testing.

On this page