Skip to content
Last updated

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/

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 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.

  1. Search for your Integration in the Catalog

  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

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.

FieldRequiredDescription

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 IDWhen Send By = Phone Number IDThe ID of phone number act as sender.
Phone NumberWhen Send By = Display Phone NumberThe phone number displayed in the WhatsApp Manager > Phone numbers.
Account IDWhen Send By = Display Phone NumberThe account ID own the phone number.
Send ModeSelect either: - Template - Text Message. In this use case, select Text Message.
Text ContentWhen Send Mode = Text MessageThe text content of the message. Maximum 4096 characters.
Preview URLWhen Send Mode = Text MessageEnable the URL preview in the message sent.

Sending predefined template messages

Sample query

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.

FieldRequiredDescription
Send BySelect 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 IDWhen Send By = Phone Number IDThe ID of phone number act as sender.
Phone NumberWhen Send By = Display Phone NumberThe phone number displayed in the WhatsApp Manager > Phone numbers.
Account IDWhen Send By = Display Phone NumberThe account ID own the phone number.
Send ModeSelect either: - Template - Text Message. In this use case, select Template.
Template NameYesThe name of the template. Look for a template in WhatsApp Manager > Manage template.
Header variableYesThe 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 variablesYesList 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 codeYesSelect 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.

  4. In the drop-down, select one of the following schedule options:

    Drop-down ValueDescription
    Custom cron...Review 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.
    NoneNo schedule.

Custom cron... Details

Cron ValueDescription
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 ValueExampleExample 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.

  • 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 Columnsto 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.

  • 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.

(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.

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 theText Message Mode

{  "accessToken": "__SAVED__",  "sendBy": "phone_number_id",  "phoneNumberId": "phone_number_Id",  "sendMode": "textMessage",  "textContent": "Your_text_content",  "previewUrl": true,  "languageCode": "en_US"}

Parameters

NameDescriptionValueDefault ValueRequired
typeDescribe the name of the service as the destination of export.whats_app_businessN/AYes
access_tokenWhatsapp Business API access tokenWhatsapp Business API access tokenN/AYes
send_bySupport Phone Number ID or Display Phone NumberPhone Number ID or Display Phone NumberPhone Number IDYes
account_idWhatsapp Business Account IDWhatsapp Business Account IDN/AYes if send_by is Display Phone Number
phone_numberWhatsapp Business Phone NumberWhatsapp Business Phone NumberN/AYes if send_by is Display Phone Number
phone_number_idWhatsapp Business Phone Number IDWhatsapp Business Phone Number IDN/AYes if send_by is Phone Number ID
send_modeSending messages mode, support template or text messageSending messages mode, support template or text messagetext messageYes
text_contentThe text content of the messageThe text content of the messageN/AYes if send_mode is text message
preview_urlThe Preview URL of the messageThe Preview URL of the messageN/AYes if send_mode is text message
template_nameTemplate nameTemplate nameN/AYes if send_mode is the template
header_variableThe header variable of the templateThe header variable of the templateN/ANo
body_variablesList of body variables, separated by commaList of body variables, separated by commaN/ANo
language_codeLanguage code of the messageLanguage code of the messageusYes 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/