Delphi WhatsApp Cloud API

From sgcWebSockets 2022.5.0 WhatsApp Cloud API is supported.

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:

  • Integrate WhatsApp messaging with tools like CRM, analytics, and third-party apps
  • Green Tick, verified WhatsApp Business profile
  • WhatsApp Broadcast & Bulk Messaging
  • No app or interface, use via BSPs or CRM
  • WhatsApp Chatbot & chat automation using third-party apps
  • Schedule WhatsApp messages at a large scale
  • Interactive messaging features include List messages, reply buttons, CTA messages

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:

  • Your App is associated with the Business Manager that you chose, or that was created automatically.
  • A WhatsApp test phone number is added to your business. You can use this test phone number to explore the WhatsApp Business Platform without registering or migrating a real phone number. Test phone numbers can send unlimited messages to up to 5 recipients (which can be anywhere in the world).

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:

  • WhatsappOptions.PhoneNumberId: is the ID of the Phone Number used to send messages.
  • WhatsappOptions.Token: is the Temporary Access Token valid for 24 hours.

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:

  • ServerOptions: here you can configure the IP Address to bind, the Listening Port, if it's using SSL (the WebHook must run in a secure server, you can configure your server to use SSL or Proxy the WebHook requests to a none HTTPs server). The server is based on TsgcWebSocketHTTPServer.
    • WebhookOptions: this property allows to set the Webhook properties that later will be configured in your developer facebook account.
      • Endpoint: it's the name of the endpoint, by default is /webhook. Example: if your server is listening on https://www.esegece.com, the endpoint will be "https://www.esegece.com/webhook"
      • Token: it's a security string that can contain any value defined by you. It's used to verify the Webhook registration is correct.

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:

  • Callback URL: This is the URL Meta will be sending the events to.
  • Verify Token: This string is set up by you, when you create your webhook endpoint.

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!

Find below a public online WhatsApp Bot Demo, just send any text message to start.

WhatsApp Bot

Built with sgcWebSockets library, send any text message and the bot will show a list of options or echo your text message.

Find below the compiled WhatsApp Demo for Windows.

File Name: sgcWhatsApp
File Size: 2.7 mb
Download File
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

sgcWebSockets 2022.5.0
Delphi Microsoft Sign-In Login

Related Posts