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のように国番号から入力してください。
コードを書く
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}'"
}
]
}
]
}
}コードを実行する
このファイルをあなたのマシンに保存し、実行する:
前提条件
npm install @vonage/server-sdk @vonage/messagessend-authentication-template.js という名前のファイルを作成し、以下のコードを追加する:
const { Vonage } = require('@vonage/server-sdk');
const { Channels } = require('@vonage/messages');
/**
* It is best to send messages using JWT instead of basic auth. If you leave out
* apiKey and apiSecret, the messages SDK will send requests using JWT tokens
*
* @link https://developer.vonage.com/en/messages/technical-details#authentication
*/
const vonage = new Vonage(
{
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
},
{
...(MESSAGES_API_URL ? {apiHost: MESSAGES_API_URL} : {}),
},
);コードを書く
send-authentication-template.js に以下を追加する:
vonage.messages.send({
to: MESSAGES_TO_NUMBER,
from: WHATSAPP_SENDER_ID,
channel: Channels.WHATSAPP,
messageType: '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,
},
],
},
],
},
},
})
.then(({ messageUUID }) => console.log(messageUUID))
.catch((error) => console.error(error));コードを実行する
このファイルをあなたのマシンに保存し、実行する:
前提条件
コードを書く
SendWhatsappAuthenticationTemplate ファイルのmain メソッドに以下を追加する:
val messageId = client.messages.send(
whatsappCustom {
to(MESSAGES_TO_NUMBER)
from(WHATSAPP_SENDER_ID)
custom(mapOf(
"type" to MessageType.TEMPLATE,
"template" to mapOf(
"name" to WHATSAPP_AUTH_TEMPLATE_NAME,
"language" to mapOf(
"policy" to Policy.DETERMINISTIC,
"code" to Locale.ENGLISH
),
"components" to listOf(
mapOf(
"type" to "body",
"parameters" to listOf(
mapOf(
"type" to MessageType.TEXT,
"text" to WHATSAPP_OTP
)
)
),
mapOf(
"type" to MessageType.BUTTON,
"sub_type" to "url",
"index" to 0,
"parameters" to listOf(
mapOf(
"type" to MessageType.TEXT,
"text" to WHATSAPP_OTP
)
)
)
)
)
))
}
)コードを実行する
Gradle用のアプリケーション プラグインを使うことで、アプリケーションの実行を簡単にすることができます。build.gradle を以下のように更新する:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''以下のgradle コマンドを実行し、com.vonage.quickstart.kt.messages.whatsapp をSendWhatsappAuthenticationTemplate を含むパッケージに置き換えてアプリケーションを実行する:
前提条件
build.gradle に以下を追加する:
implementation 'com.vonage:server-sdk:9.3.1'SendWhatsappAuthenticationTemplate という名前のファイルを作成し、main メソッドに以下のコードを追加する:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();コードを書く
SendWhatsappAuthenticationTemplate ファイルのmain メソッドに以下を追加する:
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());コードを実行する
Gradle用のアプリケーション プラグインを使うことで、アプリケーションの実行を簡単にすることができます。build.gradle を以下のように更新する:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''以下のgradle コマンドを実行し、com.vonage.quickstart.messages.whatsapp をSendWhatsappAuthenticationTemplate を含むパッケージに置き換えてアプリケーションを実行する:
コードを書く
SendWhatsAppAuthenticationTemplate.cs に以下を追加する:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var vonageClient = new VonageClient(credentials);
var request = new WhatsAppCustomRequest
{
To = MESSAGES_TO_NUMBER,
From = WHATSAPP_SENDER_ID,
Custom = new
{
type = "template",
template = new
{
name = WHATSAPP_TEMPLATE_NAME,
language = new
{
code = "en",
policy = "deterministic"
},
components = new object[]
{
new
{
type = "body",
parameters = new[]
{
new
{
type = "text",
text = WHATSAPP_OTP
}
}
},
new
{
type = "button",
sub_type = "url",
index = "0",
parameters = new[]
{
new
{
type = "text",
text = WHATSAPP_OTP
}
}
}
}
}
}
};
var response = await vonageClient.MessagesClient.SendAsync(request);前提条件
composer require vonage/clientsend-authentication-template.php という名前のファイルを作成し、以下のコードを追加する:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);コードを書く
send-authentication-template.php に以下を追加する:
$custom = [
"to" => TO_NUMBER,
"from" => WHATSAPP_NUMBER,
"channel" => "whatsapp",
"message_type" => "custom",
"custom" => [
"type" => "template",
"template" => [
"name" => WHATSAPP_AUTH_TEMPLATE_NAME,
"language" => [
"policy" => "deterministic",
"code" => "en"
],
"components" => [
[
"type" => "body",
"parameters" => [
[
"type" => "text",
"text" => '$OTP'
]
]
],
[
"type" => "button",
"sub_type" => "url",
"index" => "0",
"parameters" => [
[
"type" => "text",
"text" => '$OTP'
]
]
]
]
]
]
];
$whatsApp = new \Vonage\Messages\Channel\WhatsApp\WhatsAppCustom(
TO_NUMBER,
FROM_NUMBER,
$custom
);
$client->messages()->send($whatsApp);コードを実行する
このファイルをあなたのマシンに保存し、実行する:
前提条件
コードを書く
send-authentication-template.rb に以下を追加する:
message = client.messaging.whatsapp(
type: 'custom',
message: {
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
}
]
}
]
}
}
)
client.messaging.send(
from: WHATSAPP_SENDER_ID,
to: MESSAGES_TO_NUMBER,
**message
)コードを実行する
このファイルをあなたのマシンに保存し、実行する:
試してみる
コードを実行すると、WhatsApp 認証テンプレートが送信先の番号に送信されます。