Skip to main content
Table of Contents

Getting started with TMS FNC WhatsApp Server

Prerequisites

  • TMS FNC Core and TMS FNC WebSocket installed (see Installation).
  • A Meta developer app with the WhatsApp product and a WhatsApp Business account.
  • A public domain name for the server and a certificate for it from a trusted certificate authority, plus the OpenSSL libraries next to the executable.

Add the component

  1. Drop TTMSFNCWhatsAppServer from the TMS FNC WebSocket palette page onto a form, or create one at runtime.
  2. Set Port (typically 443) and VerifyToken to a string of your choice.
  3. Set CertificateFile, CertificateKeyFile, and RootCertificateFile to your PEM certificate files. TLS is always enabled.
  4. Set Active := True to start the server.
  5. Register https://<your-domain>/ as the callback URL with the same verify token in the Meta dashboard, and subscribe to the messages field.

Inspect incoming payloads

Every payload WhatsApp posts is passed to OnRawMessage before it is forwarded to the connected receivers:

Start the webhook server and inspect incoming payloadsPascalC++
procedure TForm1.FormCreate(Sender: TObject);
begin
  // Callback URL in the Meta dashboard: https://<your-domain>/
  TMSFNCWhatsAppServer1.Port := 443;
  TMSFNCWhatsAppServer1.VerifyToken := 'my-verify-token';   // same value as in the dashboard
  TMSFNCWhatsAppServer1.CertificateFile := 'certs\cert.pem';
  TMSFNCWhatsAppServer1.CertificateKeyFile := 'certs\key.pem';
  TMSFNCWhatsAppServer1.RootCertificateFile := 'certs\chain.pem';
  TMSFNCWhatsAppServer1.Active := True;
end;

procedure TForm1.TMSFNCWhatsAppServer1RawMessage(Sender: TObject;
  AMessage: string; var ABroadcast: Boolean);
begin
  // AMessage is the JSON body WhatsApp posted. This event runs on the
  // server's connection thread, so queue user interface updates.
  TThread.Queue(nil,
    procedure
    begin
      Memo1.Lines.Add(AMessage);
    end);
  // Leave ABroadcast True to forward the payload to every connected receiver.
end;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Callback URL in the Meta dashboard: https://<your-domain>/
  TMSFNCWhatsAppServer1->Port = 443;
  TMSFNCWhatsAppServer1->VerifyToken = "my-verify-token";   // same value as in the dashboard
  TMSFNCWhatsAppServer1->CertificateFile = "certs\\cert.pem";
  TMSFNCWhatsAppServer1->CertificateKeyFile = "certs\\key.pem";
  TMSFNCWhatsAppServer1->RootCertificateFile = "certs\\chain.pem";
  TMSFNCWhatsAppServer1->Active = true;
}

void __fastcall TForm1::ShowRaw()
{
  Memo1->Lines->Add(FRaw);
}

void __fastcall TForm1::TMSFNCWhatsAppServer1RawMessage(TObject *Sender,
  System::UnicodeString AMessage, bool &ABroadcast)
{
  // AMessage is the JSON body WhatsApp posted. This event runs on the
  // server's connection thread, so queue user interface updates.
  FRaw = AMessage;
  TThread::Queue(nullptr, ShowRaw);
  // Leave ABroadcast True to forward the payload to every connected receiver.
}

Next steps