TWhatsApp

Source: source/classes/twhatsapp.prg

Hierarchy: TWhatsApp (standalone class, no inheritance)

TWhatsApp is a WhatsApp Cloud API client that communicates with Meta's Business Platform via HTTPS requests using the cURL library. It requires a Meta Business Account, a registered phone number ID, and a permanent system access token generated from the Meta App Dashboard. All message types are sent as JSON payloads through the Graph API.

Key DATA Members

DATATypeDefaultDescription
cTokenCharacterBearer access token for API authentication
cPhoneIdCharacterSender phone number ID (Meta numeric ID, not the phone number)
cVersionCharacter"v18.0"Graph API version string
cBaseUrlCharacter"https://graph.facebook.com"Base URL for all API calls
cResponseCharacterRaw JSON response from the last API call
nErrorNumeric0cURL error code from the last operation
nHttpCodeNumeric0HTTP status code from the last response

Methods

MethodDescription
New( cToken, cPhoneId, cVersion )Constructor. Initializes cURL handle and stores credentials. Optionally sets API version.
End()Clean up the cURL handle. Call when done with the object.
SendText( cTo, cMsg, lPreviewUrl )Send a plain text message. lPreviewUrl enables link preview generation.
SendTemplate( cTo, cName, cLang, aBodyParams )Send a pre-approved message template with optional body parameter values.
SendImage( cTo, cLinkOrId, cCaption )Send an image by URL or previously uploaded media ID.
SendDocument( cTo, cLinkOrId, cFilename, cCaption )Send a document by URL or media ID with optional filename and caption.
SendAudio( cTo, cLinkOrId )Send an audio file by URL or media ID.
SendVideo( cTo, cLinkOrId, cCaption )Send a video by URL or media ID with optional caption.
SendLocation( cTo, nLat, nLong, cName, cAddress )Send a location pin with latitude, longitude, and optional name/address.
UploadMedia( cFileName, cMimeType )Upload a media file to WhatsApp servers. Returns the media ID string, or nil on failure.
DownloadMedia( cMediaId, cTargetFile )Download a media file from WhatsApp servers and save to disk. Returns .T. on success.
GetMessageId()Extract the message ID from the last API response. Returns empty string on failure.
GetError()Extract the error message from the last API response. Returns empty string if no error.

Example: Send Text Message

#include "FiveWin.ch"

function Main()

   local oWA := TWhatsApp():New( "EAAx...token", "123456789", "v18.0" )
   local cResp, cMsgId

   // Send a simple text message
   cResp := oWA:SendText( "5215512345678", "Hello from FiveWin!" )

   cMsgId := oWA:GetMessageId()
   if ! Empty( cMsgId )
      MsgInfo( "Message sent. ID: " + cMsgId )
   else
      MsgInfo( "Error: " + oWA:GetError() )
   endif

   oWA:End()

return nil

Example: Template Message

#include "FiveWin.ch"

function SendTemplate()

   local oWA  := TWhatsApp():New( "EAAx...token", "123456789" )
   local aParams := { "John", "FWH 26.05", "Monday" }

   // "welcome_message" must be pre-approved by Meta
   oWA:SendTemplate( "5215512345678", "welcome_message", "en_US", aParams )
   oWA:End()

return nil

Notes

See Also