Blog
All Blog Posts | Next Post | Previous PostBuilding your own WhatsApp application with Delphi
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 isnt directly possible to setup a webhook for a native Delphi application).
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
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
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)
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
This blog post has received 2 comments.
WhatsApp business templates are not supported in the TTMSFNCCloudWhatsapp component.
Bradley Velghe
All Blog Posts | Next Post | Previous Post
Following the instructions on your blog we have downloaded the trial version of TMS FNC Cloud Pack and TMS FNC Websocket to test how it works. We receive and send text messages without problems, but
Is it possible to send messages with a WhatsApp business template?
Thank you.
Anitúa Ferran