Noizu.WhatsApp (:noizu_whatsapp)

Copy Markdown View Source

Minimal Elixir client for the WhatsApp Business Cloud API (Meta Graph), HTTP-stubbed testable, part of the Noizu comms-channels family (see docs/arch/comms-channels.md).

Installation

{:noizu_whatsapp, "~> 0.1"}

Configuration

All values may be passed to Noizu.WhatsApp.Client.new/1 explicitly; without options the client falls back to application env (config :noizu_whatsapp, :client, ...) and then to environment variables:

Env varPurpose
WHATSAPP_ACCESS_TOKENpermanent system user access token (never a user token)
WHATSAPP_PHONE_NUMBER_IDid of the sending business phone number
WHATSAPP_BUSINESS_ACCOUNT_IDWABA id (template listing)
WHATSAPP_APP_SECRETwebhook X-Hub-Signature-256 HMAC secret
WHATSAPP_VERIFY_TOKENwebhook subscription challenge token

Graph API version is configurable and defaults to v21.0 (Client.new(graph_version: "v22.0")).

Meta app setup (one-time)

  1. Create an app at developers.facebook.com, add the WhatsApp product.
  2. Note the Phone number ID and WhatsApp Business Account ID.
  3. Create a system user in Business Settings, grant it whatsapp_business_messaging, generate a permanent token.
  4. Subscribe the app to the WABA's messages webhook field; set the verify token.

Usage

client = Noizu.WhatsApp.Client.new()

# Plain text — only inside the 24h customer-service window
Noizu.WhatsApp.Messages.send_text(client, "+14155551234", "hello", preview_url: true)

# Outside the window, restart it with an approved template
Noizu.WhatsApp.Messages.send_template(client, "+14155551234", "welcome", "en_US", components)

Noizu.WhatsApp.Messages.send_media(client, "+14155551234", :image, "https://…/cat.jpg")
Noizu.WhatsApp.Messages.mark_read(client, "wamid.in.1")
Noizu.WhatsApp.Messages.react(client, "+14155551234", "wamid.in.1", "👍")

Noizu.WhatsApp.Templates.list(client)          # approved templates (needs WABA id)
Noizu.WhatsApp.Media.upload(client, :image, "path/to/file.png")

# Webhooks
{:ok, challenge} = Webhook.verify_challenge(params)
Webhook.valid_signature?(raw_body, x_hub_signature_256)
{:ok, [%Webhook.Message{} | _]} = Webhook.parse(raw_body)

Every failure is {:error, %Noizu.WhatsApp.Error{}}. Graph error code 131047 maps to kind: :window_closed (the 24-hour customer service window closed — use send_template/4 to reopen). 429s are retried with backoff and surface as tag: :rate_limited when the budget is exhausted.

Phone numbers are normalized to digits-only E.164 and masked to their last four digits in errors and inspect output. Access tokens are never logged.

Testing your own code

No network is required: pass req_options: [plug: fun] to the client (see Req.Test) or copy test/support/testing.ex.

License

MIT.