Blog

All Blog Posts  |  Next Post  |  Previous Post

Building your own WhatsApp application with Delphi

Bookmarks: 

Wednesday, November 2, 2022

With the release of the new TMS FNC Websocket pack and the newest iteration of the TMS FNC Cloud Pack we now offer full fledged support for receiving and sending WhatsApp messages using the WhatsApp Business API. The TMS FNC Cloud Pack enables the user to send various messages to people. but to receive messages, the WhatsApp Business API requires the user to setup a webhook (and obviously, it isn’t directly possible to setup a webhook for a native Delphi application).TMS Software Delphi  Components

Therefore we introduced the TMSFNCWhatsAppServer and the TMSFNCWhatsAppReceiver component. We can easily set-up a server and client without a whole lot of code, the heavy lifting is done by these components. In this blog we will do a quick walkthrough on how you can accomplish this. 


Server

TMS Software Delphi  Components

First of all, we will need to host a server somewhere. This server will act as the mediator between our WhatsApp client and the WhatsApp Business API. To do this, we can create a project and drop the TTMSFNCWhatsAppServer component on the form. Simply link the necessary certificate files in the properties(RootCertificateFileFile, CertificateFile, CertificateFileKeyFile), Set the required Port number and if needed a Pathname and you're good to go. No code is needed for this. The component will forward all communication between the WhatsApp Business API and the client.

You can find more information on how to set up the server here.


Client

TMS Software Delphi  Components

The client application will need both the TTMSFNCWhatsAppReceiver and the TTMSFNCCloudWhatsApp component. The former will handle all incoming messages whilst the latter will handle the sending of messages. 


Set-up

TTMSFNCWhatsAppReceiver

Simply set the host, path and port number and set the active property on true or call connect. you can then subscribe on the OnWhatsAppMessageReceived to start handling the messages. 

TTMSFNCCloudWhatsApp

Provide the APIKey and set the Phone Number ID and subscribe to the OnSendSuccess event. 


Implementation

As we use 2 stand-alone components in conjunction, there is a bit of code required to fully integrate both in a full-fledged WhatsApp project. But, we at TMS Software, have done some of the heavy lifting for you and wrote a wrapper unit which contains some logic to make integrating the components a bit easier. this wrapper unit can be found in the TMS FNC Cloud pack demo folder in the WhatsApp with WebSocket Demo client project. 

This wrapper unit has a TTMSFNCWhatsAppConversations class. This class contains all unique phone numbers and all messages that have been sent/received to this number. 

After instantiating the object you can simple add a new conversation by using the following code. 

FConversations.AddNewConversation(AMessage.SendTo);

When receiving/sending a new message you can then use the following code snippets to add these messages to this list. 

FConversations.Items[AMessage.From.Phone].AddIncomingMessage(AMessage)
FConversations.Items[AMessage.SendTo].AddOutgoingMessage(AMessage)
Using this class structure you can set-up a WhatsApp project fairly easy. Check out the demo that has been provided in the TMS FNC Cloud Pack: WhatsApp with WebSocket demo to see a simple implementation of both the client and server.


Availability

Get started with both TMS FNC Cloud Pack and TMS FNC Websocket and build your own WhatsApp projects now! These packs are also part of TMS ALL-ACCESS and TMS FNC Component Studio.




Bradley Velghe


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post