# Whatsapp Business Messaging Export Integration The WhatsApp Business Platform gives medium to large businesses the ability to connect with customers at scale. You can start WhatsApp conversations with your customers in minutes, send them care notifications or purchase updates, offer personalized services, and provide support in the channel that your customers prefer. This connector use Cloud API from Meta to enable Treasure Data users to initiate WhatsApp messages directly from TD. This integration can be used in activations to send messages in free text or pre-defined templates to the WhatsApp users. This topic includes: ## Prerequisites - Basic Knowledge of Treasure Data™. - Basic knowledge of Meta Business Manager, WhatsApp manager. ## Requirements and Limitations - Access token from system admin user is required. It is recommended to generate a never-expiring token. - Sending messages in pre-defined template is supported. However, the activations should limited to text-based templates. ## Static IP Address of Treasure Data Integration If your security policy requires IP whitelisting, you must add Treasure Data's IP addresses to your allowlist to ensure a successful connection. Please find the complete list of static IP addresses, organized by region, at the following link: [https://api-docs.treasuredata.com/en/overview/ip-addresses-integrations-result-workers/](https://api-docs.treasuredata.com/en/overview/ip-addresses-integrations-result-workers/) ## Use the TD Console to Create a Connection In Treasure Data, you must create and configure the data connection before running your query. As part of the data connection, you provide authentication to access the integration. ### Obtain the access token Follow this [blog post](https://developers.facebook.com/blog/post/2022/12/05/auth-tokens/) from Meta to obtain an access token. A permanent access token is recommended. ### Create Authentication Your first step is to create a new authentication with a set of credentials. 1. Select **Integrations Hub**. 2. Select **Catalog**. ![](/assets/26617500.ca92fa4ab9277dca95973c6bd413fc662a3f0d04b57d58f7a8c952a29f28bbec.25ec5a77.png) 1. Search for your Integration in the Catalog ![](/assets/screenshot-2024-11-14-at-13.33.01.8c496cc20fd3d4e0a8c589addc88ca62143df0364cb17d396d75169489c94954.13f2ff0a.png) 1. Hover your mouse over the icon and select **Create Authentication**. 2. Ensure that the **Credentials** tab is selected and then enter credential information for the integration. 3. Select **Continue**. 4. Enter a name for your authentication, and select **Done**. ### Define your Query 1. Navigate to Data Workbench > Queries. 2. Select **New Query**. 3. Enter the query to get list of phone numbers, see sample query. 4. Run query to validate expected result. ### Specify the Result Export Target and Configure the Activation - Sending free text messages Sample query ```SQL SELECT phone_number FROM users ``` Follow below configurations to set up the activation using free text messages. In this use case, same WhatsApp messages to all the valid phone numbers from the query result is expected to be triggered. ![](/assets/text.e6407d53dc3c8953f09c918087d42242a18e4e7d186ae48c3b168c1602dddc31.13f2ff0a.png) | Field | Required | Description | | --- | --- | --- | | Send By | | Select either: - Phone Number ID: this ID can be found from Meta Developer App Dashboard. - Display Phone Number: the number of the WhatsApp Business account from WhatsApp Manager, if this option is selected, WhatsApp Business Account ID is required to retrieve the Phone Number ID. For non-developer users, it is easier to use Display Phone Number together with Business Account ID. Those can be found from WhatsApp Manager > Phone numbers. | | Phone Number ID | When Send By = Phone Number ID | The ID of phone number act as sender. | | Phone Number | When Send By = Display Phone Number | The phone number displayed in the WhatsApp Manager > Phone numbers. | | Account ID | When Send By = Display Phone Number | The account ID own the phone number. | | Send Mode | | Select either: - Template - Text Message. In this use case, select **Text Message**. | | Text Content | When Send Mode = Text Message | The text content of the message. Maximum 4096 characters. | | Preview URL | When Send Mode = Text Message | Enable the URL preview in the message sent. | #### Sending predefined template messages Sample query ```sql SELECT buyer_phone AS phone_number, 'Order Confirmation' AS header, id AS order_id, tracking_number AS track_id, CURRENT_DATE AS today_varFROM orders ``` Follow below configurations to set up the activation using free text messages. In this use case, same WhatsApp message to all the valid phone numbers from the query result is expected to be triggered. ![](/assets/template.612ef6937c096547279f7d5f2c35ed56d576cb28b4d78f2b5d4c4a8ab5267854.13f2ff0a.png) | Field | Required | Description | | --- | --- | --- | | Send By | | Select either:- Phone Number ID: this ID can be found from Meta Developer App Dashboard. - Display Phone Number: the number of the WhatsApp Business account from WhatsApp Manager, if this option is selected, WhatsApp Business Account ID is required to retrieve the Phone Number ID. For non-developer users, it is easier to use Display Phone Number together with Business Account ID. Those can be found from WhatsApp Manager > Phone numbers. | | Phone Number ID | When Send By = Phone Number ID | The ID of phone number act as sender. | | Phone Number | When Send By = Display Phone Number | The phone number displayed in the WhatsApp Manager > Phone numbers. | | Account ID | When Send By = Display Phone Number | The account ID own the phone number. | | Send Mode | | Select either: - Template - Text Message. In this use case, select **Template**. | | Template Name | Yes | The name of the template. Look for a template in WhatsApp Manager > Manage template. | | Header variable | Yes | The column name or alias from query result to a variable in header. Maximum only one variable is supported by WhatsApp.If header variable is not found from the query or there is no variable defined in the header from the template, it will cause error. | | Body variables | Yes | List of column names or aliases from query result to variables in body. If body variables are not found from the query or does not align with the total number of variables defined by the template, it will cause error. Mapping between Treasure Data and WhatsApp is done using order, not name. | | Language code | Yes | Select proper language code. It is a search criteria for the target template. | ### (Optional) Schedule Query Export Jobs You can use Scheduled Jobs with Result Export to periodically write the output result to a target destination that you specify. Treasure Data's scheduler feature supports periodic query execution to achieve high availability. When two specifications provide conflicting schedule specifications, the specification requesting to execute more often is followed while the other schedule specification is ignored. For example, if the cron schedule is `'0 0 1 * 1'`, then the 'day of month' specification and 'day of week' are discordant because the former specification requires it to run every first day of each month at midnight (00:00), while the latter specification requires it to run every Monday at midnight (00:00). The latter specification is followed. #### Scheduling your Job Using TD Console 1. Navigate to **Data Workbench > Queries** 2. Create a new query or select an existing query. 3. Next to **Schedule**, select None. ![](/assets/image2021-1-15_17-28-51.f1b242f6ecc7666a0097fdf37edd1682786ec11ef80eff68c66f091bc405c371.0f87d8d4.png) 4. In the drop-down, select one of the following schedule options: ![](/assets/image2021-1-15_17-29-47.45289a1c99256f125f4d887e501e204ed61f02223fde0927af5f425a89ace0c0.0f87d8d4.png) | Drop-down Value | Description | | --- | --- | | Custom cron... | Review [Custom cron... details](#custom-cron-details). | | @daily (midnight) | Run once a day at midnight (00:00 am) in the specified time zone. | | @hourly (:00) | Run every hour at 00 minutes. | | None | No schedule. | #### Custom cron... Details ![](/assets/image2021-1-15_17-30-23.0f94a8aa5f75ea03e3fec0c25b0640cd59ee48d1804a83701e5f2372deae466c.0f87d8d4.png) | **Cron Value** | **Description** | | --- | --- | | `0 * * * *` | Run once an hour. | | `0 0 * * *` | Run once a day at midnight. | | `0 0 1 * *` | Run once a month at midnight on the morning of the first day of the month. | | "" | Create a job that has no scheduled run time. | ``` * * * * * - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +---------- month (1 - 12) | | +--------------- day of month (1 - 31) | +-------------------- hour (0 - 23) +------------------------- min (0 - 59) ``` The following named entries can be used: - Day of Week: sun, mon, tue, wed, thu, fri, sat. - Month: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec. A single space is required between each field. The values for each field can be composed of: | Field Value | Example | Example Description | | --- | --- | --- | | A single value, within the limits displayed above for each field. | | | | A wildcard `'*'` to indicate no restriction based on the field. | `'0 0 1 * *'` | Configures the schedule to run at midnight (00:00) on the first day of each month. | | A range `'2-5'`, indicating the range of accepted values for the field. | `'0 0 1-10 * *'` | Configures the schedule to run at midnight (00:00) on the first 10 days of each month. | | A list of comma-separated values `'2,3,4,5'`, indicating the list of accepted values for the field. | `0 0 1,11,21 * *'` | Configures the schedule to run at midnight (00:00) every 1st, 11th, and 21st day of each month. | | A periodicity indicator `'*/5'` to express how often based on the field's valid range of values a schedule is allowed to run. | `'30 */2 1 * *'` | Configures the schedule to run on the 1st of every month, every 2 hours starting at 00:30. `'0 0 */5 * *'` configures the schedule to run at midnight (00:00) every 5 days starting on the 5th of each month. | | A comma-separated list of any of the above except the `'*'` wildcard is also supported `'2,*/5,8-10'`. | `'0 0 5,*/10,25 * *'` | Configures the schedule to run at midnight (00:00) every 5th, 10th, 20th, and 25th day of each month. | 1. (Optional) You can delay the start time of a query by enabling the Delay execution. ### Execute the Query Save the query with a name and run, or just run the query. Upon successful completion of the query, the query result is automatically exported to the specified destination. Scheduled jobs that continuously fail due to configuration errors may be disabled on the system side after several notifications. (Optional) You can delay the start time of a query by enabling the Delay execution. ## Activate a Segment in Audience Studio You can also send segment data to the target platform by creating an activation in the Audience Studio. 1. Navigate to **Audience Studio**. 2. Select a parent segment. 3. Open the target segment, right-mouse click, and then select **Create Activation.** 4. In the **Details** panel, enter an Activation name and configure the activation according to the previous section on Configuration Parameters. 5. Customize the activation output in the **Output Mapping** panel. ![](/assets/ouput.b2c7f1d909c4f98ed10f5300df858a4b19f71a3b0834df952f5fb24018a5ea78.8ebdf569.png) - Attribute Columns - Select **Export All Columns** to export all columns without making any changes. - Select **+ Add Columns** to add specific columns for the export. The Output Column Name pre-populates with the same Source column name. You can update the Output Column Name. Continue to select **+ Add Columns**to add new columns for your activation output. - String Builder - **+ Add string** to create strings for export. Select from the following values: - String: Choose any value; use text to create a custom value. - Timestamp: The date and time of the export. - Segment Id: The segment ID number. - Segment Name: The segment name. - Audience Id: The parent segment number. 1. Set a **Schedule**. ![](/assets/snippet-output-connector-on-audience-studio-2024-08-28.a99525173709da1eb537f839019fa7876ffae95045154c8f2941b030022f792c.8ebdf569.png) - Select the values to define your schedule and optionally include email notifications. 1. Select **Create**. If you need to create an activation for a batch journey, review [Creating a Batch Journey Activation](/products/customer-data-platform/journey-orchestration/batch/creating-a-batch-journey-activation). ## (Optional) Configure Export Results in Workflow Within Treasure Workflow, you can specify the use of this integration to export data. With the **Send By** method, TD supports two types: - display_phone_number: You will need to enter your WhatsApp Business Account ID and phone number. - phone_number_id: You will be required to input your Phone Number ID. You can configure this connector to run using the following example: ``` out: type: whats_app_business access_token: your access token send_by: display_phone_number or phone_number_id account_id: your account id phone_number: your whatsapp business phone number phone_number_id: your phone number id # If the send_by is phone_number_id ``` With the **Send Mode** method, TD supports two types: - text_message: You will send the text messages to other users. - template: You will send the template messages that you can set up to other users. You can configure this connector to run using the following example: ``` out: type: whats_app_business access_token: your access token send_by: display_phone_number or phone_number_id account_id: your account id phone_number: your whatsapp business phone number phone_number_id: your phone number id # If the send_by is phone_number_id send_mode: text_message or template text_content: your text message preview_url: true out: type: whats_app_business access_token: your access token send_by: display_phone_number or phone_number_id account_id: your account id send_mode: template phone_number: your whatsapp business phone number template_name: your template header_variable: your header variable in the above template body_variables: your body variables in the above template language_code: en # default : en ``` ## (Optional) Export Integration Using the CLI You can also use CLI(Toolbelt) to export results to WhatsApp Business. You need to specify the information for export to your server as `--result` option of `td query` command. About `td query` command, you can see [this article](https://docs.treasuredata.com/display/PD/TD+Toolbelt+Job+and+Query+Command+Reference). The format of the option is JSON and the general structure is as follows. With the **Template Mode** ``` { "accessToken": "__SAVED__", "sendBy": "phone_number_id", "phoneNumberId": "phone_number_Id", "sendMode": "template", "templateName": "your_template", "headerVariable": "your_header_variable", "bodyVariables": "your_body_variables", "languageCode": "en_US"} ``` With the**Text Message Mode** ``` { "accessToken": "__SAVED__", "sendBy": "phone_number_id", "phoneNumberId": "phone_number_Id", "sendMode": "textMessage", "textContent": "Your_text_content", "previewUrl": true, "languageCode": "en_US"} ``` #### Parameters | Name | Description | Value | Default Value | Required | | --- | --- | --- | --- | --- | | type | Describe the name of the service as the destination of export. | whats_app_business | N/A | Yes | | access_token | Whatsapp Business API access token | Whatsapp Business API access token | N/A | Yes | | send_by | Support `Phone Number ID` or `Display Phone Number` | `Phone Number ID or Display Phone Number` | Phone Number ID | Yes | | account_id | Whatsapp Business Account ID | Whatsapp Business Account ID | N/A | Yes if send_by is Display Phone Number | | phone_number | Whatsapp Business Phone Number | Whatsapp Business Phone Number | N/A | Yes if send_by is Display Phone Number | | phone_number_id | Whatsapp Business Phone Number ID | Whatsapp Business Phone Number ID | N/A | Yes if send_by is Phone Number ID | | send_mode | Sending messages mode, support `template` or `text message` | Sending messages mode, support `template` or `text message` | text message | Yes | | text_content | The text content of the message | The text content of the message | N/A | Yes if send_mode is text message | | preview_url | The Preview URL of the message | The Preview URL of the message | N/A | Yes if send_mode is text message | | template_name | Template name | Template name | N/A | Yes if send_mode is the template | | header_variable | The header variable of the template | The header variable of the template | N/A | No | | body_variables | List of body variables, separated by comma | List of body variables, separated by comma | N/A | No | | language_code | Language code of the message | Language code of the message | us | Yes if send_mode is the template | #### Example for Usage ``` $ td query -d hiep_whatsapp -w "select first_name,last_name,phone_number,action_source,order_id from user_actions" -T presto --result '{ "type": "whats_app_business","access_token": "xxxxx","send_by": "phone_number_id","send_mode": "text_message", "phone_number_id": "yyyyyy","text_content": "Virtual reality (VR) technology has been evolving rapidly,transforming how we interact with digital environments and expanding possibilities across various sectors.From its early conceptualization to the sophisticated systems available today,VR has taken a path marked by both significant achievements and formidable challenges. https://en.wikipedia.org/wiki/Virtual_reality"}' ``` ## References - API Overview: https://developers.facebook.com/docs/whatsapp/cloud-api - OAuth 2.0: https://developers.facebook.com/blog/post/2022/12/05/auth-tokens/