A WhatsApp shopping bot, built on Meta's WhatsApp Cloud API, greets a returning shopkeeper with an interactive carousel: Meta's message type for a row of swipeable cards, each with its own image and button. Ours shows one card per brand. Right after it comes a second interactive message, a row of quick-reply buttons (Today's deals, My orders, Help).
Order matters. The buttons say "for more actions, tap below", so they have to land under the carousel.
On one retailer's "hi", the carousel arrived after 4.8 seconds and the buttons after 19.4. Across the day's greetings the buttons took 8 to 19 seconds. The retailer had long since started typing.
Why there was a wait at all
Meta's Cloud API answers 200 when it accepts a message, not when it
delivers it. Awaiting the HTTP call doesn't order anything. A carousel is media-heavy:
Meta fetches every card's image from its URL before it ships the message. Meanwhile a
plain button message sent 300ms later sails through and appears above the
carousel.
The truth arrives later, on Meta's status webhook: sent,
delivered, read or failed. So the welcome had been
changed to send the carousel, wait for its delivered status (up to 12
seconds), and only then send the buttons.
That fixed the order. It also put the retailer's phone on the critical path.
Delivered means the phone
delivered fires when the message reaches the device. That depends on
the shopkeeper's network, whether WhatsApp is in the background, and how long the phone
takes to pull a carousel of images. For that retailer it ran past the 12-second cap. So
the bot sat out the whole timeout and sent the buttons anyway, at +19s, with no guarantee
it had bought anything.
Sent is the one that orders
sent fires when Meta has finished processing the message, carousel images
included, and handed it to WhatsApp's delivery pipeline. That is the step the overtaking
happened in. Once the carousel is sent, the buttons sent after it queued
behind it on every greeting I measured. Meta doesn't document that ordering, so treat it
as observed, not promised.
So the wait changed to one condition:
// 'sent' = Meta has processed it (carousel images fetched) and queued it in order.
const d = await delivery.waitForDelivery(wamid, {
timeoutMs,
until: ['sent', 'delivered', 'read'],
});
with a 5-second cap and a 300ms gap after it. If failed comes back, a
plain-text welcome goes out instead, so a broken carousel never costs the greeting.
On four real greetings, the buttons landed at 7.7 to 10.9 seconds instead of 8 to 19, and the carousel was above them every time.
The wait is mostly the webhook
That was still slow, so I logged two things on every status: Meta's own timestamp for it, and when the webhook actually reached us. The gap was 1.0 to 2.2 seconds, every time. Meta stamps statuses in whole seconds, so call it ยฑ1, but the pattern held across forty-odd statuses.
Most of the ~2 seconds a sent wait cost wasn't the carousel being
processed. It was the news of it being processed, in transit. The order
guarantee costs about one webhook delivery per message you wait on.
Which means you should wait on as few messages as possible. Only a slow message can be overtaken. A short text greeting sent before the carousel can't be: it's processed first and nothing heavy is ahead of it. So the greeting goes out with no wait, and only the carousel holds the line for the buttons.
On the next three greetings the carousel landed at about 3 seconds and the buttons at 6.1 to 6.5, still in order. From 19.4 seconds at the start of the day.
seconds until the buttons appear, real greetingswait for delivered โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 8.0โ19.4s wait for sent โโโโโโโโโโโโโโโโโโโโโ 7.7โ10.9s sent, greeting unwaited โโโโโโโโโโโโโ 6.1โ6.5s
One outlier: the first greeting after a deploy waited 5 seconds for sent on the
carousel, then 2 seconds on every one after. My guess is Meta fetching the card images cold
and caching them by URL, but I haven't proven that.
What's left is about 3 seconds between the carousel and the buttons: roughly 2 of webhook lag and 1 for the next send. That's the price of the order guarantee, and I'll pay it.
How to keep messages in order after a WhatsApp carousel
If you send an interactive carousel (or any media-heavy message) through the Cloud API and something has to land after it, this is the whole recipe:
-
Keep the message id. The send response carries it as
messages[0].id(awamid). That's the only handle you get. -
Route statuses to it. Subscribe your webhook to the
messagesfield; statuses arrive there asstatuses[], each with the sameid. Keep a small map of id โ waiter, and record a status even if it arrives before anyone waits on it. -
Wait for
sent, with a cap. Resolve onsent,deliveredorread(any of them proves "processed and queued"), or onfailed. Cap it at a few seconds and proceed anyway, so a lost webhook costs a delay, never a stuck chat. -
Fall back on
failed. A carousel can be rejected after the200(a bad image URL, say). Send the same content as plain text so the user still gets something. - Only wait after slow messages. Text and button messages are processed fast; nothing needs to wait for them. Wait after the carousel, not before it.
- Mind your replicas. If you run more than one instance, the status webhook can land on a different one than the waiter. An in-memory map is fine on one instance; beyond that, use a shared store (Redis pub/sub works).
The waiter itself is small:
const seen = new Map(); // wamid -> latest status
const waiters = new Map(); // wamid -> resolve()
// webhook: for each entry.changes[].value.statuses[]
function onStatus(s) {
seen.set(s.id, s.status);
waiters.get(s.id)?.(s.status);
}
function waitForSent(wamid, timeoutMs = 5000) {
const done = (st) => ['sent', 'delivered', 'read', 'failed'].includes(st);
if (done(seen.get(wamid))) return Promise.resolve(seen.get(wamid));
return new Promise((resolve) => {
const t = setTimeout(() => finish('timeout'), timeoutMs);
function finish(st) { clearTimeout(t); waiters.delete(wamid); resolve(st); }
waiters.set(wamid, (st) => done(st) && finish(st));
});
}
const res = await sendCarousel(to, cards);
const status = await waitForSent(res.messages[0].id);
if (status === 'failed') await sendText(to, fallbackText);
await sendButtons(to);
Expire old entries in seen so it stays bounded; a minute is plenty.
What I'd reuse
-
A
200from a messaging API is "accepted". If order matters, wait for a status, not the HTTP call. - Pick the earliest status that proves what you need. Ordering needs "processed and queued", not "on the user's phone". Waiting on a later status puts someone else's network inside your latency.
- A timeout is a fallback, not a plan. If you keep hitting it, you're waiting on the wrong event.
- Every status you wait on costs a webhook delivery, which ran 1 to 2 seconds for us. Wait only after messages that can actually be overtaken.