WhatsappElixir.Flows (whatsapp_elixir v0.1.9)

View Source

Module for managing WhatsApp Flows including creation, publishing, updating, and lifecycle management.

Key Functions

Flows Overview

WhatsApp Flows are interactive experiences that allow businesses to create structured conversations with customers. Flows can be used for various purposes including:

  • Sign up and sign in processes
  • Appointment booking
  • Lead generation
  • Customer support
  • Surveys and feedback collection

Flow Categories

  • SIGN_UP: User registration flows
  • SIGN_IN: User authentication flows
  • APPOINTMENT_BOOKING: Scheduling and booking flows
  • LEAD_GENERATION: Lead capture flows
  • CONTACT_US: Contact and inquiry flows
  • CUSTOMER_SUPPORT: Support and help flows
  • SURVEY: Survey and feedback flows
  • OTHER: General purpose flows

Flow Status

  • DRAFT: Flow is under development, can be tested with draft mode
  • PUBLISHED: Flow is live and can be sent to customers
  • DEPRECATED: Flow is retired and cannot be sent or opened
  • BLOCKED: Flow endpoint is unhealthy, cannot be sent or opened
  • THROTTLED: Flow endpoint has issues, limited to 10 messages per hour

Prerequisites

To use the Flows API, you need:

  • Message templates (view and manage) permissions
  • Phone Numbers (view and manage) permissions
  • Verified business with high message quality
  • Meta application connected to Flows with endpoints

For complete details, see the WhatsApp Flows API documentation.

Summary

Functions

Deletes a Flow. Only Flows in DRAFT status can be deleted.

Deprecates a published Flow, preventing it from being sent or opened.

Generates a web preview URL for visualizing and testing a Flow.

Lists all assets attached to a specific Flow.

Lists all Flows for a WhatsApp Business Account.

Publishes a Flow, making it available to send to customers.

Updates a Flow's JSON content by uploading a JSON file.

Updates a Flow's metadata (name, categories, endpoint_uri, application_id).

Functions

create_flow(flow_data, custom_config \\ [])

Creates a new Flow.

Parameters

  • flow_data: A map containing the following keys:
    • :name (string, required): Flow name
    • :categories (list, required): List of Flow categories (at least one required)
    • :flow_json (string, optional): Flow's JSON encoded as string
    • :publish (boolean, optional): Whether to publish the Flow immediately (requires flow_json)
    • :clone_flow_id (string, optional): ID of source Flow to clone
    • :endpoint_uri (string, optional): URL of the WhatsApp Flow Endpoint (for Flow JSON v3.0+)
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

# Create a simple draft Flow
iex> flow_data = %{
...>   name: "Customer Survey",
...>   categories: ["SURVEY", "CUSTOMER_SUPPORT"]
...> }
iex> WhatsappElixir.Flows.create_flow(flow_data)
{:ok, %{"id" => "flow123", "success" => true, "validation_errors" => []}}

# Create and publish a Flow with JSON content
iex> flow_data = %{
...>   name: "Lead Generation Form",
...>   categories: ["LEAD_GENERATION"],
...>   flow_json: "{"version":"5.0","screens":[...]}",
...>   publish: true,
...>   endpoint_uri: "https://example.com/webhook"
...> }
iex> WhatsappElixir.Flows.create_flow(flow_data)
{:ok, %{"id" => "flow456", "success" => true}}

Returns

  • {:ok, response} on success with Flow ID and validation errors if any
  • {:error, response} on failure

Validation

  • Raises ArgumentError if required fields (name, categories) are missing
  • Raises ArgumentError if categories contain invalid values
  • Raises ArgumentError if publish is true but flow_json is not provided

delete_flow(flow_id, custom_config \\ [])

Deletes a Flow. Only Flows in DRAFT status can be deleted.

Parameters

  • flow_id (string): ID of the Flow to delete
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

iex> WhatsappElixir.Flows.delete_flow("flow123")
{:ok, %{"success" => true}}

Returns

  • {:ok, response} on success
  • {:error, response} on failure

deprecate_flow(flow_id, custom_config \\ [])

Deprecates a published Flow, preventing it from being sent or opened.

Parameters

  • flow_id (string): ID of the Flow to deprecate
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

iex> WhatsappElixir.Flows.deprecate_flow("flow123")
{:ok, %{"success" => true}}

Returns

  • {:ok, response} on success
  • {:error, response} on failure

generate_preview(flow_id, invalidate_existing \\ false, custom_config \\ [])

Generates a web preview URL for visualizing and testing a Flow.

Parameters

  • flow_id (string): ID of the Flow
  • invalidate_existing (boolean, optional): Whether to invalidate existing preview URL
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

# Generate new preview URL
iex> WhatsappElixir.Flows.generate_preview("flow123")
{:ok, %{
  "preview" => %{
    "preview_url" => "https://business.facebook.com/wa/manage/flows/123/preview/?token=abc...",
    "expires_at" => "2023-05-21T11:18:09+0000"
  },
  "id" => "flow123"
}}

Returns

  • {:ok, response} containing preview URL and expiration time
  • {:error, response} on failure

get_flow(flow_id, fields \\ "", phone_number_id \\ nil, custom_config \\ [])

Retrieves details of a specific Flow.

Parameters

  • flow_id (string): ID of the Flow to retrieve
  • fields (string, optional): Comma-separated list of fields to include
  • phone_number_id (string, optional): Phone number ID for health status check
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

# Get Flow with default fields
iex> WhatsappElixir.Flows.get_flow("flow123")
{:ok, %{
  "id" => "flow123",
  "name" => "Customer Survey",
  "status" => "PUBLISHED",
  "categories" => ["SURVEY"],
  "validation_errors" => []
}}

Returns

  • {:ok, response} containing Flow details
  • {:error, response} on failure

get_flow_assets(flow_id, custom_config \\ [])

Lists all assets attached to a specific Flow.

Parameters

  • flow_id (string): ID of the Flow
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

iex> WhatsappElixir.Flows.get_flow_assets("flow123")
{:ok, %{
  "data" => [
    %{
      "name" => "flow.json",
      "asset_type" => "FLOW_JSON",
      "download_url" => "https://scontent.xx.fbcdn.net/..."
    }
  ]
}}

Returns

  • {:ok, response} containing assets data and paging information
  • {:error, response} on failure

get_flows(fields \\ "", custom_config \\ [])

Lists all Flows for a WhatsApp Business Account.

Parameters

  • fields (string, optional): Comma-separated list of fields to include in response
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

# List all Flows with default fields
iex> WhatsappElixir.Flows.get_flows()
{:ok, %{
  "data" => [
    %{"id" => "flow1", "name" => "Survey", "status" => "PUBLISHED", "categories" => ["SURVEY"]},
    %{"id" => "flow2", "name" => "Lead Gen", "status" => "DRAFT", "categories" => ["LEAD_GENERATION"]}
  ],
  "paging" => %{"cursors" => %{"before" => "...", "after" => "..."}}
}}

Returns

  • {:ok, response} containing data array and paging information
  • {:error, response} on failure

migrate_flows(destination_waba_id, source_waba_id, source_flow_names \\ nil, custom_config \\ [])

Migrates Flows from one WhatsApp Business Account to another.

Parameters

  • destination_waba_id (string): Destination WABA ID
  • source_waba_id (string): Source WABA ID
  • source_flow_names (list, optional): Specific Flow names to migrate (max 100)
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

# Migrate all Flows
iex> WhatsappElixir.Flows.migrate_flows("dest_waba", "source_waba")
{:ok, %{
  "migrated_flows" => [
    %{"source_name" => "survey", "source_id" => "123", "migrated_id" => "456"}
  ],
  "failed_flows" => []
}}

# Migrate specific Flows
iex> WhatsappElixir.Flows.migrate_flows("dest_waba", "source_waba", ["survey", "lead-gen"])
{:ok, %{"migrated_flows" => [...], "failed_flows" => [...]}}

Returns

  • {:ok, response} containing migrated and failed Flows
  • {:error, response} on failure

publish_flow(flow_id, custom_config \\ [])

Publishes a Flow, making it available to send to customers.

Parameters

  • flow_id (string): ID of the Flow to publish
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

iex> WhatsappElixir.Flows.publish_flow("flow123")
{:ok, %{"success" => true}}

Returns

  • {:ok, response} on success
  • {:error, response} on failure

update_flow_json(flow_id, json_file_path, custom_config \\ [])

Updates a Flow's JSON content by uploading a JSON file.

Parameters

  • flow_id (string): ID of the Flow to update
  • json_file_path (string): Path to the JSON file containing Flow content
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

iex> WhatsappElixir.Flows.update_flow_json("flow123", "/path/to/flow.json")
{:ok, %{"success" => true, "validation_errors" => []}}

Returns

  • {:ok, response} on success with validation errors if any
  • {:error, response} on failure

Notes

  • The JSON file must be valid Flow JSON format
  • File size is limited to 10 MB
  • The file is uploaded as multipart form data

update_flow_metadata(flow_id, update_data, custom_config \\ [])

Updates a Flow's metadata (name, categories, endpoint_uri, application_id).

Parameters

  • flow_id (string): ID of the Flow to update
  • update_data (map): Data to update containing:
    • :name (string, optional): New Flow name
    • :categories (list, optional): New Flow categories (at least one required if provided)
    • :endpoint_uri (string, optional): New endpoint URI (for Flow JSON v3.0+)
    • :application_id (string, optional): Meta application ID to connect to the Flow
  • custom_config (keyword, optional): Custom configuration for the HTTP request

Examples

# Update Flow name
iex> WhatsappElixir.Flows.update_flow_metadata("flow123", %{name: "Updated Survey"})
{:ok, %{"success" => true}}

Returns

  • {:ok, response} on success
  • {:error, response} on failure