WhatsApp Client 组件:sgcSocial | eSeGeCe

WhatsApp Client

TsgcWhatsApp_Client 对接 WhatsApp Business Cloud API,即面向 WhatsApp 的 Meta Graph API。发送文本、媒体、位置、联系人、表情反应、交互式列表和按钮,以及已审核的模板消息,然后通过内置的 webhook 服务器接收回复和送达状态。在 Delphi 和 C++ Builder 中是同一个组件、同一套 API。

TsgcWhatsApp_Client

将 Meta 应用中的 Phone Number ID 和访问令牌复制到 WhatsAppOptions 中,调用 SendMessage... 方法,然后调用 StartServer 来托管接收回复的 webhook。

组件类

TsgcWhatsApp_Client

API

WhatsApp Business Cloud API(Meta Graph API)

传输

纯 HTTPS,覆盖所有 Delphi 平台

语言

Delphi、C++ Builder

也内置于 sgcWebSockets 中。WhatsApp 客户端同样内置于 sgcWebSockets 的 Professional 及以上版本中。sgcSocial 是独立包,已内置 sgcWebSockets Core 运行时。

发送一条文本,接收回复

设置 WhatsAppOptions.PhoneNumberIdToken,发送一条文本,然后启动 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;
}

关键属性与方法

您最常使用的成员。

Cloud API 凭据

WhatsAppOptions.PhoneNumberIdWhatsAppOptions.Token,均来自您的 Meta 应用。每次调用都是对 Graph API 的纯 HTTPS 请求。

文本、表情反应、回执

SendMessageText 发送文本,SendMessageReaction 对某条消息 id 作出表情反应,MarkMessageRead 发送已读回执。

公开链接媒体

SendMessageImageSendMessageDocumentSendMessageAudioSendMessageVideoSendMessageSticker 引用托管在公开 URL 上的媒体。

本地文件媒体

SendFileImageSendFileDocumentSendFileAudioSendFileVideoSendFileSticker 先上传本地文件,再发送它。

位置与联系人

SendMessageLocation 发送带名称和地址的坐标。SendMessageContact 发送联系人卡片。

交互式消息

SendMessageInteractiveList 用于选择列表,SendMessageInteractiveButtons 用于回复按钮。

模板

SendMessageTemplate 接受模板名称、语言代码和参数。

媒体存储

UploadMediaDownloadMediaDeleteMedia 直接操作 Cloud API 的媒体存储。

Webhook 服务器

StartServerStopServer 托管 Meta 的 webhook,通过 ServerOptions 配置。OnBeforeSubscribe 应答验证请求,OnMessageReceivedvar aMarkAsRead 参数送达入站消息,OnMessageSent 报告送达状态,OnRawMessage 暴露原始 JSON。

TLS 与日志

TLSOptions 配置 HTTPS 客户端,OnBeforeSendMessage 检查出站消息,文件日志记录流量以便排查问题。

继续探索

sgcSocial 概览两个组件、版本和授权方式一览。
功能矩阵:WhatsApp该组件的每一项能力,以表格形式呈现。
WhatsApp API 深度解析API 参考页面,附在线帮助和 PDF 链接。
博客:WhatsApp Delphi 组件Cloud API 客户端的完整演示。
博客:用 WhatsApp API 发送本地文件用 SendFile 方法上传本地媒体。
超值之选:All-AccesseSeGeCe 全部产品,含高级支持,每年 €1,059 起。
查看 All-Access 价格

准备好发送 WhatsApp 消息了吗?

下载免费试用版,将您的 Delphi 应用程序连接到 WhatsApp Business Cloud API。