WhatsApp

For arlanix/notification 1.2.0

WhatsApp

The module arlanix/module-notification-whatsapp adds WhatsApp to a subscription's Delivered by field. It is optional, and it needs arlanix/module-notification 1.1 or later, which ships Arlanix\Notification\Model\Protocol\Http\RequestClientInterface.

Each subscription stores its own Phone number ID, access token, recipient, template name and language. The manual shows how to get each at Meta, with a screenshot to each step.

The store's server must be able to reach graph.facebook.com:443. Meta must have verified the business that owns the number: until then, every send fails with (#133010) Account not registered, even from Meta's test number.

Request and template

Arlanix\NotificationWhatsapp\Model\WhatsappProtocol sends a JSON POST through RequestClientInterface to https://graph.facebook.com/v26.0/PHONE_NUMBER_ID/messages, with Authorization: Bearer ACCESS_TOKEN and Content-Type: application/json. Graph API v26.0 is set by the class's graphVersion argument in etc/di.xml. Override that argument to change the version; its format must be vN.N, otherwise the protocol falls back to v26.0.

The token is the subscription secret endpoint/whatsapp_access_token, stored encrypted and never returned to the form. The settings are endpoint/whatsapp_phone_number_id, endpoint/whatsapp_recipient, endpoint/whatsapp_template and endpoint/whatsapp_language. The recipient normaliser removes spaces, hyphens, parentheses and the leading plus. The address is digits; the grid and Message Log show +digits.

TemplateMessage builds this body shape:

{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "442079460958",
  "type": "template",
  "template": {
    "name": "store_alert",
    "language": {"code": "en_US"},
    "components": [{
      "type": "body",
      "parameters": [
        {"type": "text", "text": "SUBJECT"},
        {"type": "text", "text": "MESSAGE"}
      ]
    }]
  }
}

The first body parameter fills {{1}} with the plain text subject, cut to 200 characters; the second fills {{2}} with the plain text message, cut to 900. Each parameter is flattened to one line: line breaks become · and tabs become spaces. An empty parameter becomes -. Each delivery is one template message, including a digest of up to 25 different messages.

The protocol accepts any successful HTTP status. For failures, it maps the JSON error.code as follows:

HTTP status or Meta code Result
HTTP 401 or code 190 The access token expired or was revoked; replace it.
132000, 132001 Check the template name, language, approval and exactly two body variables.
131026, 131030, 131047 The recipient is not on WhatsApp, is not allowed for a test number, or cannot receive the message now.
HTTP 429, or 130429, 131048, 131056 Rate limit: wait and retry. This takes precedence over the other mappings.
Other failures The HTTP status and sanitised error.message, if any. Tags are stripped, whitespace collapsed, the token redacted, and the returned text cut to 200 characters.

See When WhatsApp refuses for the exact messages and what to fix.

Meta refuses a template body that starts or ends with a variable, so the template puts text around {{1}} and {{2}}. See Make the template.

When WhatsApp asks to wait

WhatsApp answers HTTP 429, or Meta code 130429, 131048 or 131056, when too many messages are sent at once. The delivery waits for the Retry-After header, or retry_after in the JSON answer, at most one hour, and one minute when WhatsApp gives no time. During the first day after the message was published, a wait does not use up one of the delivery's attempts. See When a service asks to wait, which works the same way.

Check it

Use Send test message on a subscription. It shows what WhatsApp answered. Meta's test number delivers only to the recipients added to it in the app's Step 1. Try it out, five at most.