fix(whatsapp): answer 503 when the inbox cannot take an event, so Meta redelivers

The webhook answered 200 whatever happened downstream, and Meta only
redelivers on a non-2xx answer, so an event that reached a stopped service,
or a just-started one that had not resolved its companies yet, was lost.
mobile-api now answers 503 on an unreachable service or a 5xx (a 4xx
refusal still gets 200), and the service answers 503 for a configured
number whose company is not resolved yet. wamid uniqueness makes the
redelivery safe. Owner decision 2026-12-02; design D3 and the spec updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 11:49:17 +08:00
1 parent 8984c7c1ae
commit 3e610e0f88
8 files changed
+80 -7

No files matched your search

@@ -89,8 +89,13 @@ public class WhatsAppWebhookService {
rest.postForEntity(rest.getURLforApiService(RestApiWhatsappServiceConstants.API_REST_WHATSAPP_WEBHOOK_INGEST),
Void.class, new WebhookIngestRequest(Base64.getEncoder().encodeToString(raw), signature));
} catch (RestClientException e) {
// Still 200: Meta retries on anything else and the service is idempotent. Status code only, never the message.
// Status code only, never the message.
log.warn("WhatsApp webhook forward failed: {}", e instanceof RestClientResponseException r ? r.getStatusCode().value() : e.getClass().getSimpleName());
// A refusal (4xx) cannot succeed on a retry, so Meta gets 200. Anything else (service down, 5xx) answers 503:
// Meta redelivers for up to 7 days, and the service ignores a wamid it already holds.
if (!(e instanceof RestClientResponseException r && r.getStatusCode().is4xxClientError())) {
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE);
}
}
log.info("WhatsApp webhook forwarded for {} company(ies)", matched.size());
}
@@ -27,6 +27,8 @@ import org.springframework.http.MediaType;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import javax.crypto.Mac;
@@ -251,7 +253,7 @@ class WhatsAppWebhookTest {
}
@Test
void forwardFailureStillAnswers200AndLogsNoContent() throws Exception {
void forwardFailureAnswers503SoMetaRetriesAndLogsNoContent() throws Exception {
ListAppender<ILoggingEvent> logs = new ListAppender<>();
logs.start();
Logger logger = (Logger) LoggerFactory.getLogger(WhatsAppWebhookService.class);
@@ -261,7 +263,7 @@ class WhatsAppWebhookTest {
byte[] body = BODY.getBytes(StandardCharsets.UTF_8);
String sig = sign("ins-secret", body);
mvc().perform(post("/api/public/whatsapp/webhook").contentType(MediaType.APPLICATION_JSON).content(body)
.header("X-Hub-Signature-256", sig)).andExpect(status().isOk());
.header("X-Hub-Signature-256", sig)).andExpect(status().isServiceUnavailable());
assertThat(logs.list).isNotEmpty();
for (ILoggingEvent e : logs.list) {
assertThat(e.getFormattedMessage()).doesNotContain("hello").doesNotContain("secret").doesNotContain(sig).doesNotContain("verify-me");
@@ -272,6 +274,24 @@ class WhatsAppWebhookTest {
}
}
@Test
void serviceErrorAnswers503SoMetaRetries() throws Exception {
when(rest.postForEntity(eq(INGEST), eq(Void.class), any()))
.thenThrow(HttpServerErrorException.create(HttpStatus.SERVICE_UNAVAILABLE, "x", null, null, null));
byte[] body = BODY.getBytes(StandardCharsets.UTF_8);
mvc().perform(post("/api/public/whatsapp/webhook").contentType(MediaType.APPLICATION_JSON).content(body)
.header("X-Hub-Signature-256", sign("ins-secret", body))).andExpect(status().isServiceUnavailable());
}
@Test
void serviceRefusalStillAnswers200BecauseARetryCannotHelp() throws Exception {
when(rest.postForEntity(eq(INGEST), eq(Void.class), any()))
.thenThrow(HttpClientErrorException.create(HttpStatus.BAD_REQUEST, "x", null, null, null));
byte[] body = BODY.getBytes(StandardCharsets.UTF_8);
mvc().perform(post("/api/public/whatsapp/webhook").contentType(MediaType.APPLICATION_JSON).content(body)
.header("X-Hub-Signature-256", sign("ins-secret", body))).andExpect(status().isOk());
}
@Test
void rateLimitedByIp() throws Exception {
mvc().perform(get("/api/public/whatsapp/webhook").param("hub.mode", "subscribe").param("hub.verify_token", "verify-me").param("hub.challenge", "1"))