Subscribe to Webhooks to get notifications about messages your business receives and customer profile updates.
Whenever a trigger event occurs, the WhatsApp Business Platform sees the event and sends a notification to a Webhook URL you have previously specified. You can get two types of notifications:
Every time a new message is received the event OnMessageReceived is called, where you can access to the content of the Message and mark the message as read.
Find below an example, when a new text message is received, it's echoed to user who sent it.
procedure OnWhatsAppMessageReceived(Sender: TObject; const aMessage: TsgcWhatsApp_Receive_Message; var aMarkAsRead: Boolean);
var
vText: string;
vTo: string;
begin
if aMessage.Contacts.Count > 0 then
begin
vTo := aMessage.Contacts.Contact[0].WaID;
if aMessage.Messages.Count > 0 then
begin
if aMessage.Messages._Message[0]._Type = wapmrtText then
begin
vText := 'ECHO ==> ' + aMessage.Messages._Message[0].Text.Body;
WhatsApp.SendMessageText(vTo, vText);
aMarkAsRead := True;
end;
end;
end;
end;
The WhatsApp Business Platform sends notifications to inform you of the status of the messages between you and users. When a message is sent successfully, you receive a notification when the message is sent, delivered, and read. The order of these notifications in your app may not reflect the actual timing of the message status. View the timestamp to determine the timing, if necessary.
Every time a new status is received, the event OnMessageSent is called.
procedure OnWhatsAppMessageSent(Sender: TObject; const aMessage: TsgcWhatsApp_Receive_Message; aStatus: TsgcWhatsAppSendMessageStatusType);
begin
vPhone := aMessage.MetaData.DisplayPhoneNumber;
case aStatus of
wapsmstSent: DoLog('Message to ' + vPhone + ' sent.');
wapsmstDelivered: DoLog('Message to ' + vPhone + ' delivered.');
wapsmstRead: DoLog('Message to ' + vPhone + ' read.');
else
DoLog('Message to ' + vPhone + ' unknown status.')
end;
end;