Skip to content

TTMSFNCWhatsAppServer

The TTMSFNCWhatsAppServer is a non-visual component that can be used to host a server for the callback URL in the WhatsApp integration API. You can parse the incoming messages directly before the component automatically forwards them to the connected TTMSFNCWhatsAppReceiver clients. Under the hood it uses TTMSFNCWebSocketServer.

Setup

  1. For initial setup of the account please follow the guide from here.

  2. After setting up the application and adding the WhatsApp integration, you can set the callback URL. To do this, select WhatsApp > Configuration from the side menu in your application's dashboard.
    Configuration page

  3. Edit the Callback URL under Webhook. Set this value to a secure (HTTPS) URL, the same URL where your server is running. Fill in the Verify token during development with a string of your choice. The same verify token will need to be used in the TTMSFNCWhatsAppServer. Later you can opt for a permanent token.
    Webhook window

  4. After the callback URL is successfully added, register for the fields of your choice. In the Webhook fields click on Manage and subscibe to messages to get notifications on incoming replies to the WhatsApp account.
    Fields window
    You can subscribe to multiple fields, all of the messages will be received by the server. Click Done.

Forward messages to clients

All messages are forwarded to the connected TTMSFNCWhatsAppReceiver clients. However, before broadcasting the incoming messages, you can parse their value on server side and/or disable the broadcasting by implementing the OnRawMessage event.

procedure TForm1.TMSFNCWhatsAppServer1RawMessage(Sender: TObject;
  AMessage: string; var ABroadcast: Boolean);
begin
  //AMessage is a JSON string. Parse here if necessary.
  //ABroadcast := False will cause the message not to be sent to the connected clients
end;

Secure connection

This server must always run from a secure context according to WhatsApp requirements. Because of this UseSSL is set to True and cannot be modified. See the Secure connection part of TTMSFNCWebSocketServer.

Properties

Property name Description
Active: Boolean Start or stop the server.
CertificateFile: string Path to the certificate file.
CertificateKeyFile: string Path to the certificate key file.
PathName: string Name of the path.
For example: https://localhost:8888/path
Port: Integer Port number.
RootCertificateFile: string Path to the root certificate file.
VerifyToken: string It should be the same value as used during the setup for the Verify token.

Methods

Method name Description
BroadcastMessage(AMessage: string) Broadcasts a string message to all connected clients.
BroadcastData(AData: TBytes) Broadcasts a binary message to all connected clients.
SendMessageTo(AMessage: string; ASelector: TTMSFNCWebSocketSendToCallback) Sends a string message to connected clients. The ASelector callback determines to which clients the message should be sent.
SendDataTo(AData: TBytes; ASelector: TTMSFNCWebSocketSendToCallback) Sends a binary message to connected clients. The ASelector callback determines to which clients the message should be sent.

Events

Event name Description
OnAllow Event triggered when a new client tries to connect.
OnBinaryDataReceived Event triggered when binary message arrives from the connected clients.
OnClose Event triggered when a close frame is sent by the client.
OnCommandGet Event triggered when an HTTP request arrives.
OnConnect Event triggered when a clients connects.
OnDisconnect Event triggered when a client disconnects.
OnGetSSLPassword Event triggered when the pass phrase is needed that was mentioned while creating the SLL certificate.
OnMessageReceived Event triggered when a string message arrives from the connected clients.
OnPing Event triggered when a ping message arrives from a client.
OnPong Event triggered when a pong message arrives from a client.
OnRawMessage Event triggered when a message arrives from WhatsApp. The AMessage string parameter is in JSON format.