Mark an Inbound Message as Read

In this code snippet you learn how to mark an inbound WhatsApp message as read. Marking a message as read will mean that blue check-marks will show against that message in the WhatsApp UI instead of grey check-marks.

Marking an inbound message as read is done by sending a PATCH request to the message object stored on Vonage's servers, identifying the specific message by its UUID. Since message objects are geo-located, a geo-specific URL must be used for the request; this will be present in the body of the inbound message to be marked as read.

IMPORTANT: If a customer has not messaged you first, then the first time you send a message to a user, WhatsApp requires that the message contains a template. This is explained in more detail in the Understanding WhatsApp topic.

Example

Find the description for all variables used in each code snippet below:

KeyDescription
JWT

Used to authenticate your request. See Authentication for more information, including how to generate a JWT.

VONAGE_APPLICATION_ID

The Vonage Application ID.

VONAGE_PRIVATE_KEY_PATH

Private key path.

GEOSPECIFIC_MESSAGES_API_URL

The URL for the Geo-specific Messages API endpoint. One of https://api-eu.nexmo.com/v1/messages, https://api-us.nexmo.com/v1/messages, https://api-ap.nexmo.com/v1/messages.

GEOSPECIFIC_VONAGE_API_HOST

The hostname for the Geo-specific API endpoint. One of api-eu.nexmo.com, api-us.nexmo.com, api-ap.nexmo.com.

MESSAGE_UUID

The UUID of the specific message.

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 mark-as-read.sh:

curl -X PATCH "${GEOSPECIFIC_MESSAGES_API_URL}/${MESSAGES_MESSAGE_ID}" \
  -H "Authorization: Bearer "$JWT\
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d $'{
    "status": "read",
  }'

View full source

Run your code

Save this file to your machine and run it:

bash mark-as-read.sh

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 MarkAsRead and add the following code to the main method:

val client = Vonage {
    applicationId(VONAGE_APPLICATION_ID)
    privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}

View full source

Write the code

Add the following to the main method of the MarkAsRead file:

client.messages.existingMessage(MESSAGES_MESSAGE_ID, MESSAGES_GEOSPECIFIC_API_HOST).markAsRead()

View full source

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 MarkAsRead:

gradle run -Pmain=com.vonage.quickstart.kt.messages.whatsapp.MarkAsRead

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 MarkAsRead and add the following code to the main method:

VonageClient client = VonageClient.builder()
		.applicationId(VONAGE_APPLICATION_ID)
		.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
		.build();

View full source

Write the code

Add the following to the main method of the MarkAsRead file:

client.getMessagesClient().ackInboundMessage(MESSAGES_MESSAGE_ID, ApiRegion.API_EU);

View full source

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 MarkAsRead:

gradle run -Pmain=com.vonage.quickstart.messages.whatsapp.MarkAsRead

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

Install-Package Vonage

Write the code

Add the following to MarkAsRead.cs:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var vonageClient = new VonageClient(credentials);
await vonageClient.MessagesClient.UpdateAsync(WhatsAppUpdateMessageRequest.Build(MESSAGES_MESSAGE_ID));

View full source

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

composer require vonage/client

Create a file named send-file.php and add the following code:

$keypair = new \Vonage\Client\Credentials\Keypair(
    file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
    VONAGE_APPLICATION_ID
);

$client = new \Vonage\Client($keypair);

View full source

Write the code

Add the following to send-file.php:


$fileObject = new \Vonage\Messages\MessageObjects\FileObject(
    'https://example.com/file.pdf',

View full source

Run your code

Save this file to your machine and run it:

php send-file.php

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

pip install vonage python-dotenv

Write the code

Add the following to mark-as-read.py:

client.messages.mark_whatsapp_message_read("MESSAGES_MESSAGE_ID")

View full source

Run your code

Save this file to your machine and run it:

python messages/whatsapp/mark-as-read.py

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

gem install vonage

Create a file named mark-as-read.rb and add the following code:

client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: VONAGE_PRIVATE_KEY,
  api_host: GEOSPECIFIC_VONAGE_API_HOST
)

View full source

Write the code

Add the following to mark-as-read.rb:

client.messaging.update(
  message_uuid: MESSAGES_MESSAGE_ID,
  status: 'read'
)

View full source

Run your code

Save this file to your machine and run it:

ruby mark-as-read.rb

Try it out

When you run the code a request is made to mark the specified inbound message as read.