WhatsApp Client
TsgcWhatsApp_Client 对接 WhatsApp Business Cloud API,即面向 WhatsApp 的 Meta Graph API。发送文本、媒体、位置、联系人、表情反应、交互式列表和按钮,以及已审核的模板消息,然后通过内置的 webhook 服务器接收回复和送达状态。在 Delphi 和 C++ Builder 中是同一个组件、同一套 API。
TsgcWhatsApp_Client 对接 WhatsApp Business Cloud API,即面向 WhatsApp 的 Meta Graph API。发送文本、媒体、位置、联系人、表情反应、交互式列表和按钮,以及已审核的模板消息,然后通过内置的 webhook 服务器接收回复和送达状态。在 Delphi 和 C++ Builder 中是同一个组件、同一套 API。
将 Meta 应用中的 Phone Number ID 和访问令牌复制到 WhatsAppOptions 中,调用 SendMessage... 方法,然后调用 StartServer 来托管接收回复的 webhook。
TsgcWhatsApp_Client
WhatsApp Business Cloud API(Meta Graph API)
纯 HTTPS,覆盖所有 Delphi 平台
Delphi、C++ Builder
也内置于 sgcWebSockets 中。WhatsApp 客户端同样内置于 sgcWebSockets 的 Professional 及以上版本中。sgcSocial 是独立包,已内置 sgcWebSockets Core 运行时。
设置 WhatsAppOptions.PhoneNumberId 和 Token,发送一条文本,然后启动 webhook 服务器,使 Meta 可以送达入站消息。TsgcWhatsApp_Client 声明在 sgcLibs 单元中。
uses
sgcLibs;
var
WhatsApp: TsgcWhatsApp_Client;
begin
WhatsApp := TsgcWhatsApp_Client.Create(nil);
WhatsApp.WhatsAppOptions.PhoneNumberId := '123456789012345';
WhatsApp.WhatsAppOptions.Token := 'EAAG...';
WhatsApp.SendMessageText('+34666555444', 'hello from Delphi');
// webhook server: receive messages and status updates
WhatsApp.ServerOptions.Port := 443;
WhatsApp.ServerOptions.SSL := True;
WhatsApp.ServerOptions.SSLOptions.CertFile := 'cert.pem';
WhatsApp.ServerOptions.SSLOptions.KeyFile := 'key.pem';
WhatsApp.ServerOptions.SSLOptions.Port := 443;
WhatsApp.OnMessageReceived := WhatsAppMessageReceived;
WhatsApp.StartServer;
end;
procedure TForm1.WhatsAppMessageReceived(Sender: TObject;
const aMessage: TsgcWhatsApp_Receive_Message;
var aMarkAsRead: Boolean);
begin
Memo1.Lines.Add(aMessage.RawMessage);
aMarkAsRead := True;
end;
// include: sgcLibs.hpp
TsgcWhatsApp_Client *WhatsApp = new TsgcWhatsApp_Client(this);
WhatsApp->WhatsAppOptions->PhoneNumberId = "123456789012345";
WhatsApp->WhatsAppOptions->Token = "EAAG...";
WhatsApp->SendMessageText("+34666555444", "hello from Delphi");
// webhook server: receive messages and status updates
WhatsApp->ServerOptions->Port = 443;
WhatsApp->ServerOptions->SSL = true;
WhatsApp->ServerOptions->SSLOptions->CertFile = "cert.pem";
WhatsApp->ServerOptions->SSLOptions->KeyFile = "key.pem";
WhatsApp->ServerOptions->SSLOptions->Port = 443;
WhatsApp->OnMessageReceived = WhatsAppMessageReceived;
WhatsApp->StartServer();
void __fastcall TForm1::WhatsAppMessageReceived(TObject *Sender,
TsgcWhatsApp_Receive_Message * const aMessage, bool &aMarkAsRead)
{
Memo1->Lines->Add(aMessage->RawMessage);
aMarkAsRead = true;
}
您最常使用的成员。
WhatsAppOptions.PhoneNumberId 和 WhatsAppOptions.Token,均来自您的 Meta 应用。每次调用都是对 Graph API 的纯 HTTPS 请求。
SendMessageText 发送文本,SendMessageReaction 对某条消息 id 作出表情反应,MarkMessageRead 发送已读回执。
SendMessageImage、SendMessageDocument、SendMessageAudio、SendMessageVideo 和 SendMessageSticker 引用托管在公开 URL 上的媒体。
SendFileImage、SendFileDocument、SendFileAudio、SendFileVideo 和 SendFileSticker 先上传本地文件,再发送它。
SendMessageLocation 发送带名称和地址的坐标。SendMessageContact 发送联系人卡片。
SendMessageInteractiveList 用于选择列表,SendMessageInteractiveButtons 用于回复按钮。
SendMessageTemplate 接受模板名称、语言代码和参数。
UploadMedia、DownloadMedia 和 DeleteMedia 直接操作 Cloud API 的媒体存储。
StartServer 和 StopServer 托管 Meta 的 webhook,通过 ServerOptions 配置。OnBeforeSubscribe 应答验证请求,OnMessageReceived 以 var aMarkAsRead 参数送达入站消息,OnMessageSent 报告送达状态,OnRawMessage 暴露原始 JSON。
TLSOptions 配置 HTTPS 客户端,OnBeforeSendMessage 检查出站消息,文件日志记录流量以便排查问题。