WhatsApp クライアント
TsgcWhatsApp_Client は、WhatsApp 向けの Meta Graph API である WhatsApp Business Cloud API と通信します。テキスト、メディア、位置情報、連絡先、リアクション、インタラクティブなリストやボタン、承認済みテンプレートを送信し、組み込みの Webhook サーバーを通じて返信と配信ステータスを受信します。Delphi と C++ Builder で同じコンポーネント、同じ API が使えます。
TsgcWhatsApp_Client は、WhatsApp 向けの Meta Graph API である WhatsApp Business Cloud API と通信します。テキスト、メディア、位置情報、連絡先、リアクション、インタラクティブなリストやボタン、承認済みテンプレートを送信し、組み込みの Webhook サーバーを通じて返信と配信ステータスを受信します。Delphi と C++ Builder で同じコンポーネント、同じ API が使えます。
Meta アプリケーションから取得した Phone Number ID とアクセストークンを WhatsAppOptions にコピーし、SendMessage... 系のメソッドを呼び出したうえで、返信を受信する Webhook をホストするために StartServer を呼び出します。
TsgcWhatsApp_Client
WhatsApp Business Cloud API(Meta Graph API)
純粋な HTTPS、すべての Delphi プラットフォーム
Delphi、C++ Builder
sgcWebSockets にも含まれます。WhatsApp クライアントは sgcWebSockets の Professional エディション以上にも含まれています。sgcSocial は単体のパッケージで、sgcWebSockets Core ランタイムを同梱しています。
WhatsAppOptions.PhoneNumberId と Token を設定してテキストを送信し、Meta が受信メッセージを配信できるように Webhook サーバーを起動します。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 が、ServerOptions で設定した Meta の Webhook をホストします。OnBeforeSubscribe が検証リクエストに応答し、OnMessageReceived が var aMarkAsRead 引数とともに受信メッセージを配信し、OnMessageSent が配信ステータスを報告し、OnRawMessage が生の JSON を公開します。
TLSOptions が HTTPS クライアントを設定し、OnBeforeSendMessage が送信前のメッセージを検査し、ファイルログが通信内容をトラブルシューティング用に記録します。