Send a Quick Reply Button
In this code snippet you learn how to send a quick reply style button on WhatsApp. This uses Vonage's custom object facility. You can reference the WhatsApp developer docs for the specifics of the message format.
When the message recipient clicks on the quick reply button, Vonage will POST relevant data to your inbound message webhook URL.
Example
Find the description for all variables used in each code snippet below:
| Key | Description |
|---|---|
VONAGE_APPLICATION_ID | The Vonage Application ID. |
VONAGE_APPLICATION_PRIVATE_KEY_PATH | Private key path. |
BASE_URL | For production use the base URL is |
MESSAGES_API_URL | There are two versions of the API, each with their own endpoints. For production the previous Messages API endpoint was |
WHATSAPP_NUMBER | The WhatsApp number that has been allocated to you by Vonage. For sandbox testing the number is 14157386102. |
VONAGE_WHATSAPP_NUMBER | Refer to |
VONAGE_NUMBER | Refer to |
TO_NUMBER | Replace with the number you are sending to. E.g. |
WHATSAPP_TEMPLATE_NAMESPACE | The namespace ID found in your WhatsApp Business Account. Only templates created in your own namespace will work. Using an template with a namespace outside of your own results in an error code 1022 being returned. |
WHATSAPP_TEMPLATE_NAME | The name of the template created in your WhatsApp Business Account. |
NOTE: Don't use a leading + or 00 when entering a phone number, start with the country code, for example, 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-button-quick-reply.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": "header",
"parameters": [
{
"type": "image",
"image": {
"link": "'${MESSAGES_IMAGE_URL}'"
}
}
]
},
{
"type": "body",
"parameters": [
{
"type": "text",
"parameter_name": "customer_name",
"text": "Joe Bloggs"
},
{
"type": "text",
"parameter_name": "dentist_name",
"text": "Mr Smith"
},
{
"type": "text",
"parameter_name": "appointment_date",
"text": "2025-02-26"
},
{
"type": "text",
"parameter_name": "appointment_location",
"text": "ACME Dental Practice"
}
]
},
{
"type": "button",
"sub_type": "quick_reply",
"index": 0,
"parameters": [
{
"type": "payload",
"payload": "Yes-Button-Payload"
}
]
},
{
"type": "button",
"sub_type": "quick_reply",
"index": 1,
"parameters": [
{
"type": "payload",
"payload": "No-Button-Payload"
}
]
}
]
}
}
}'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-button-quick-reply.js and add the following code:
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} : {}),
},
);Write the code
Add the following to send-button-quick-reply.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: 'header',
parameters: [
{
type: 'image',
image: {
link: MESSAGES_IMAGE_URL,
},
},
],
},
{
type: 'body',
parameters: [
{
type: 'text',
parameter_name: 'customer_name',
text: 'Joe Bloggs',
},
{
type: 'text',
parameter_name: 'dentist_name',
text: 'Mr Smith',
},
{
type: 'text',
parameter_name: 'appointment_date',
text: '2025-02-26',
},
{
type: 'text',
parameter_name: 'appointment_location',
text: 'ACME Dental Practice',
},
],
},
{
type: 'button',
sub_type: 'quick_reply',
index: 0,
parameters: [
{
type: 'payload',
payload: 'Yes-Button-Payload',
},
],
},
{
type: 'button',
sub_type: 'quick_reply',
index: 1,
parameters: [
{
type: 'payload',
payload: 'No-Button-Payload',
},
],
},
],
},
},
})
.then((resp) => console.log(resp.messageUUID))
.catch((error) => console.error(error));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 file named SendWhatsappQuickReplyButton and add the following code to the main method:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Write the code
Add the following to the main method of the SendWhatsappQuickReplyButton file:
val messageId = client.messages.send(
whatsappCustom {
to(MESSAGES_TO_NUMBER)
from(WHATSAPP_SENDER_ID)
custom(mapOf(
"type" to MessageType.TEMPLATE,
"template" to mapOf(
"namespace" to WHATSAPP_TEMPLATE_NAMESPACE,
"name" to WHATSAPP_TEMPLATE_NAME,
"language" to mapOf(
"code" to Locale.ENGLISH,
"policy" to Policy.DETERMINISTIC
),
"components" to listOf(
mapOf(
"type" to "header",
"parameters" to listOf(
mapOf(
"type" to MessageType.TEXT,
"text" to "12/26"
)
)
),
mapOf(
"type" to "body",
"parameters" to listOf(
mapOf(
"type" to MessageType.TEXT,
"text" to "*Ski Trip*"
),
mapOf(
"type" to MessageType.TEXT,
"text" to "2019-12-26"
),
mapOf(
"type" to MessageType.TEXT,
"text" to "*Squaw Valley Ski Resort, Tahoe*"
)
)
),
mapOf(
"type" to MessageType.BUTTON,
"sub_type" to "quick_reply",
"index" to 0,
"parameters" to listOf(
mapOf(
"type" to "payload",
"payload" to "Yes-Button-Payload"
)
)
),
mapOf(
"type" to MessageType.BUTTON,
"sub_type" to "quick_reply",
"index" to 1,
"parameters" to listOf(
mapOf(
"type" to "payload",
"payload" to "No-Button-Payload"
)
)
)
)
)
))
}
)Run your code
We can use the application 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 SendWhatsappQuickReplyButton:
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 file named SendWhatsappQuickReplyButton 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 SendWhatsappQuickReplyButton file:
var response = client.getMessagesClient().sendMessage(
WhatsappCustomRequest.builder()
.from(WHATSAPP_SENDER_ID).to(MESSAGES_TO_NUMBER)
.custom(Map.of(
"type", MessageType.TEMPLATE,
"template", Map.of(
"namespace", WHATSAPP_TEMPLATE_NAMESPACE,
"name", WHATSAPP_TEMPLATE_NAME,
"language", Map.of(
"code", Locale.ENGLISH,
"policy", Policy.DETERMINISTIC
),
"components", List.of(
Map.of(
"type", "header",
"parameters", List.of(
Map.of(
"type", MessageType.TEXT,
"text", "12/26"
)
)
),
Map.of(
"type", "body",
"parameters", List.of(
Map.of(
"type", MessageType.TEXT,
"text", "*Ski Trip*"
),
Map.of(
"type", MessageType.TEXT,
"text", "2019-12-26"
),
Map.of(
"type", MessageType.TEXT,
"text", "*Squaw Valley Ski Resort, Tahoe*"
)
)
),
Map.of(
"type", MessageType.BUTTON,
"sub_type", "quick_reply",
"index", 0,
"parameters", List.of(
Map.of(
"type", "payload",
"payload", "Yes-Button-Payload"
)
)
),
Map.of(
"type", MessageType.BUTTON,
"sub_type", "quick_reply",
"index", 1,
"parameters", List.of(
Map.of(
"type", "payload",
"payload", "No-Button-Payload"
)
)
)
)
)
)).build()
);
System.out.println("Message sent successfully. ID: "+response.getMessageUuid());Run your code
We can use the application 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 SendWhatsappQuickReplyButton:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Install-Package VonageWrite the code
Add the following to SendWhatsAppQuickReplyButton.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 = "header",
parameters = new[]
{
new
{
type = "text",
text = "12/26"
}
}
},
new
{
type = "body",
parameters = new[]
{
new
{
type = "text",
text = "*Ski Trip*"
},
new
{
type = "text",
text = "2019-12-26"
},
new
{
type = "text",
text = "*Squaw Valley Ski Resort, Tahoe*"
}
}
},
new
{
type = "button",
sub_type = "quick_reply",
index = 0,
parameters = new[]
{
new
{
type = "payload",
text = "Yes-Button-Payload"
}
}
},
new
{
type = "button",
sub_type = "quick_reply",
index = 1,
parameters = new[]
{
new
{
type = "payload",
text = "No-Button-Payload"
}
}
}
}
}
}
};
var response = await vonageClient.MessagesClient.SendAsync(request);Prerequisites
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-button-quick-reply.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-dotenvWrite the code
Add the following to send-button-quick-reply.py:
from vonage import Auth, Vonage
from vonage_messages import WhatsappCustom
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY,
)
)
message = WhatsappCustom(
to=MESSAGES_TO_NUMBER,
from_=WHATSAPP_SENDER_ID,
custom={
"type": "template",
"template": {
"name": WHATSAPP_TEMPLATE_NAME,
"language": {"policy": "deterministic", "code": "en"},
"components": [
{
"type": "header",
"parameters": [
{
"type": "image",
"image": {
"link": MESSAGES_IMAGE_URL,
},
},
],
},
{
"type": "body",
"parameters": [
{
"type": "text",
"parameter_name": "customer_name",
"text": "Joe Bloggs",
},
{
"type": "text",
"parameter_name": "dentist_name",
"text": "Mr Smith",
},
{
"type": "text",
"parameter_name": "appointment_date",
"text": "2025-02-26",
},
{
"type": "text",
"parameter_name": "appointment_location",
"text": "ACME Dental Practice",
},
],
},
{
"type": "button",
"sub_type": "quick_reply",
"index": 0,
"parameters": [{"type": "payload", "payload": "Yes-Button-Payload"}],
},
{
"type": "button",
"sub_type": "quick_reply",
"index": 1,
"parameters": [{"type": "payload", "payload": "No-Button-Payload"}],
},
],
},
},
)
response = client.messages.send(message)
print(response)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.
gem install vonageCreate a file named send-button-quick-reply.rb and add the following code:
Run your code
Save this file to your machine and run it:
Try it out
When you run the code a WhatsApp reminder message is sent to the destination number. The message has two quick reply buttons which you can use to select whether you are going to the event or not. When a button is pressed data similar to the following is posted to your inbound webhook URL:
{
"message_uuid": "28ee5a1c-c4cc-48ec-922c-01520d4d396b",
"to": {
"number": "447700000000",
"type": "whatsapp"
},
"from": {
"number": "447700000001",
"type": "whatsapp"
},
"timestamp": "2019-12-03T12:45:57.929Z",
"direction": "inbound",
"message": {
"content": {
"type": "button",
"button": {
"payload": "Yes-Button-Payload",
"text": "Yes"
}
}
}
}
In this example the recipient clicked the yes button.