WhatsApp 認証テンプレートの送信
このコードスニペットでは、Messages API を使って WhatsApp 認証テンプレートを送信する方法を説明します。
WhatsAppは最近、認証テンプレートを使用する際のルールを更新しました。 COPY_CODE または ONE_TAP ボタンを含む。ボタンを含むため、認証テンプレートは対話型テンプレートと見なされます。対話型テンプレートを Vonage Messages API 経由で送信する場合は、テンプレートに message_type の custom よりも template.
テンプレートはメッセージカスタムオブジェクトを使って送信されます。カスタムオブジェクトは元のWhatsApp APIリクエストの一部を取得し、直接WhatsAppに送信します。
WhatsAppメディアメッセージテンプレートは本文、フッター、ボタンで構成されています。
注:ワンタップボタンを使って認証テンプレートを送信しようとしている場合、以下の操作を行わなければ、認証テンプレートは送信されません。 握手 メッセージを送信する前に、またはメッセージが適格性チェックに失敗した場合、配信されたメッセージにはワンタップボタンの代わりにコピーコードボタンが表示されます。
例
各コード・スニペットで使用されているすべての変数の説明を以下に示します:
| キー | 説明 |
|---|---|
VONAGE_APPLICATION_ID | The Vonage Application ID. |
VONAGE_APPLICATION_PRIVATE_KEY_PATH | Private key path. |
WHATSAPP_NUMBER | The WhatsApp number that has been allocated to you by Vonage. For sandbox testing the number is 14157386102. |
VONAGE_NUMBER | Refer to |
TO_NUMBER | Replace with the number you are sending to. E.g. |
WHATSAPP_AUTH_TEMPLATE_NAME | The name of the Authentication template created in your WhatsApp Business Account. |
OTP | A One-Time Password |
注: 先頭の + または 00 電話番号を入力する場合は、447700900000のように国番号から入力してください。
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Write the code
Add the following to send-authentication-template.sh:
curl -X POST "${MESSAGES_API_URL}" \
-H "Authorization: Bearer "$JWT\
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d $'{
"to": "'${MESSAGES_TO_NUMBER}'",
"from": "'${WHATSAPP_SENDER_ID}'",
"channel": "whatsapp",
"message_type": "custom",
"custom": {
"type": "template",
"template": {
"name": "'${WHATSAPP_TEMPLATE_NAME}'",
"language": {
"policy": "deterministic",
"code": "en"
},
"components": [
{
"type": "body",
"parameters": [
{
"type": "text",
"text": "'${WHATSAPP_OTP}'"
}
]
},
{
"type": "button",
"sub_type": "url",
"index": "0",
"parameters": [
{
"type": "text",
"text": "'${WHATSAPP_OTP}'"
}
]
}
]
}
}Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
npm install @vonage/server-sdk @vonage/messagesCreate a file named send-authentication-template.js and add the following code:
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Create a class named SendWhatsappAuthenticationTemplate and add the following code to the main method:
Write the code
Add the following to the main method of the SendWhatsappAuthenticationTemplate class:
Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.messages.whatsapp with the package containing SendWhatsappAuthenticationTemplate:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named SendWhatsappAuthenticationTemplate and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the SendWhatsappAuthenticationTemplate class:
var response = client.getMessagesClient().sendMessage(
WhatsappCustomRequest.builder()
.from(WHATSAPP_SENDER_ID).to(MESSAGES_TO_NUMBER)
.custom(Map.of(
"type", MessageType.TEMPLATE,
"template", Map.of(
"name", WHATSAPP_AUTH_TEMPLATE_NAME,
"language", Map.of(
"policy", Policy.DETERMINISTIC,
"code", Locale.ENGLISH
),
"components", List.of(
Map.of(
"type", "body",
"parameters", List.of(
Map.of(
"type", MessageType.TEXT,
"text", WHATSAPP_OTP
)
)
),
Map.of(
"type", MessageType.BUTTON,
"sub_type", "url",
"index", 0,
"parameters", List.of(
Map.of(
"type", MessageType.TEXT,
"text", WHATSAPP_OTP
)
)
)
)
)
)).build()
);
System.out.println("Message sent successfully. ID: "+response.getMessageUuid());Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.messages.whatsapp with the package containing SendWhatsappAuthenticationTemplate:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Install-Package VonagePrerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
composer require vonage/clientCreate a file named send-authentication-template.php and add the following code:
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
pip install vonage python-dotenvRun your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
gem install vonageCreate a file named send-authentication-template.rb and add the following code:
Run your code
Save this file to your machine and run it:
試してみる
コードを実行すると、WhatsApp 認証テンプレートが送信先の番号に送信されます。