WhatsApp Cloud API

Whatsapp

 

Send and receive messages using a cloud-hosted version of the WhatsApp Business Platform. The Cloud API allows you to implement WhatsApp Business APIs without the cost of hosting of your own servers and also allows you to more easily scale your business messaging. The Cloud API supports up to 80 messages per second of combined sending and receiving (inclusive of text and media messages).

 

The WhatsApp Business API allows medium and large businesses to communicate with their customers at scale. Using the API, businesses can build systems that connect thousands of customers with agents or bots, enabling both programmatic and manual communication. Additionally, you can integrate the API with numerous backend systems, such as CRM and marketing platforms.

 

Features

Businesses will get all the new features faster on Cloud API. Right now, WhatsApp Business Cloud API comes with all the features that are available with WhatsApp Business API.

 

Useful features of WhatsApp Cloud API:

 

 

Most common uses

 

 

 

Get Started

To send and receive a first message using a test number, complete the following steps:

 

1. Set up Developer Assets and Platform Access

 

 

From the App Dashboard, click on the app you would like to connect to WhatsApp. Scroll down to find the "WhatsApp" product and click Set up.

 

Next, you will see the option to select an existing Business Manager (if you have one) or, if you would like, the onboarding process can create one automatically for you (you can customize your business later, if needed). Make a selection and click Continue.

 

When you click Continue, the onboarding process performs the following actions:

 

 

2. Send a Test Message

 

 

Now, you can open your IDE and create a new project. Drop a TsgcWhatsapp_Client component and fill the following properties:

 

 

Once those 2 properties have been property configured, call the method SendTest to send your First message to a phone number using the Whatsapp Business Platform.

 


oClient := TsgcWhatsapp_Client.Create(nil);
oClient.WhatsappOptions.PhoneNumberId := '107809351952205';
oClient.WhatsappOptions.Token := 'EAAO4OpgZAs98BAGj3nCFGr...ZB2t8mmLB2LRXJkte2Y5PMNh2';
oClient.SendTest('34605889421');

 

3. Configure a Webhook

 

To get alerted when you receive a message or when a message’s status has changed, you need to set up a Webhooks endpoint for your app. Setting up Webhooks doesn’t affect the status of your phone number and does interfere with you sending or receiving messages.

 

To get started, first you need to create the endpoint, so first configure the ServerOptions property of WhatsApp Client component and configure the following properties:

 

 

After configuring the server, you can use the method StartServer to start the server and accept the incoming requests.

 


oClient := TsgcWhatsapp_Client.Create(nil);
oClient.ServerOptions.WebhookOptions.Endpoint := '/webhook';
oClient.ServerOptions.WebhookOptions.Token := 'MySecretToken';
oClient.StartServer();

Once your endpoint is ready, go to your App Dashboard.

 

In your App Dashboard, find the WhatsApp product and click Configuration. Then, find the webhooks section and click Configure a webhook. After the click, a dialog appears on your screen and asks you for two items:

 

 

After adding the information, click Verify and Save.

 

Back in the App Dashboard, click WhatsApp > Configuration in the left-side panel. Under Webhooks, click Manage. A dialog box will open with all the objects you can get notified about. To receive messages from your users, click Subscribe for messages.

 

4. Receive a test message

 

Every time a new message is received, the client event OnMessageReceived will be called.

 


procedure OnMessageReceived(Sender: TObject; const aMessage: TsgcWhatsapp_Receive_Message; var aMarkAsRead: Boolean);
begin
  DoLog('Received: ' + aMessage.Messages._Message[0].Id);
end;

 

Now that your Webhook is set up, send a message to the test number you have used. You should immediately get a Webhooks notification with the content of your message!

 

 

WhatsApp API not allow to send free text messages to phones that never contact you before (in the latest 24 hours). The only way to send a text message to a phone that never text to your developer account number, is sending a Template (previously approved by Meta). To override this limitation, if you want to test free text messages, just sent first a whatsapp message from the destination number to your developer account number and then you will be able to send free text messages during 24 hours.

 

Events

OnBeforeSendMessage

 

The event is called before the message is sent to the WhatsApp servers, you can access to the internal message accessing to the RawMessage paramenter.

 

OnBeforeSubscribe

 

The event is called before the server subscribes to a topic, use the parameter Accept to subscribe or not, by default, the server will subscribe to all events requested.

 

OnRawMessage

 

This event is called when the server receives a new message and still is not parsed, so you get access to the raw message.

 

OnMessageReceived

 

This event is called after the server receives a new message and is parsed. If you set the parameter MarkAsRead to True, the sender will receive a double check.

 

OnMessageSent

 

This event is called every time the server receives a new status message about the message previously sent. Read the Status property to know if the message has been sent, delivered or read.