

# Responding to a message in AWS End User Messaging Social
<a name="whatsapp-receive-message"></a>

Before you can receive a text or media message, you must have set up your WhatsApp Business Account (WABA) and an event destination. When you receive an incoming message, an event is saved in the event destination Amazon SNS topic. To receive a notification, you must subscribe to the Amazon SNS topics endpoint.

For an example event of a received media message, see [Example WhatsApp JSON for receiving a media message](managing-event-destination-dlrs.md#managing-event-destination-dlrs-example-receive-media). For more information on configuring the AWS CLI, see [Configure the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) in the *[AWS Command Line Interface User Guide](https://docs.aws.amazon.com/cli/latest/userguide/)*. For a list of supported media file types, see [Supported media file types and sizes in WhatsAppSupported media file types](supported-media-types.md).

**Important**  
To receive incoming messages, you must have [event destinations](managing-event-destinations-add.md) enabled for the WABA. For more information, see [Add a message and event destination to AWS End User Messaging Social](managing-event-destinations-add.md).

# Example of changing a message's status to read in AWS End User Messaging Social
<a name="receive-message-status"></a>

You can set the [status of the message](managing-event-destinations-status.md) to `read` to show the end user two blue check marks on their screen. 

```
aws socialmessaging send-whatsapp-message --message '{"messaging_product":"whatsapp","message_id":"'{MESSAGE_ID}'","status":"read"}' --origination-phone-number-id {ORIGINATION_PHONE_NUMBER_ID} --meta-api-version v20.0    
```

In the preceding command, do the following:
+ Replace *\$1ORIGINATION\$1PHONE\$1NUMBER\$1ID\$1* with your phone number's ID.
+ Replace *\$1MESSAGE\$1ID\$1* with the unique identifier of the message. Use the value of the `id` field in the message object of the Amazon SNS topic.

# Example of responding to a message with a reaction in AWS End User Messaging Social
<a name="receive-message-emoji"></a>

You can add a reaction to the message, like a thumbs up. 

```
aws socialmessaging send-whatsapp-message --message '{"messaging_product":"whatsapp","recipient_type":"individual","to":"'{PHONE_NUMBER}'","type": "reaction","reaction": {"message_id": "'{MESSAGE_ID}'","emoji":"\uD83D\uDC4D"}}' --origination-phone-number-id {ORIGINATION_PHONE_NUMBER_ID} --meta-api-version v20.0
```

In the preceding command, do the following:
+ Replace *\$1PHONE\$1NUMBER\$1* with your customer's phone number.
+ Replace *\$1MESSAGE\$1ID\$1* with the unique identifier of the message. Use the value of the `id` field in the message object of the Amazon SNS topic.
+ Replace *\$1ORIGINATION\$1PHONE\$1NUMBER\$1ID\$1* with your phone number's ID.

# Download a media file from WhatsApp to Amazon S3
<a name="receive-message-image"></a>

To retrieve a media file and save it to an Amazon S3 bucket, use the [get-whatsapp-message-media](https://docs.aws.amazon.com/cli/latest/reference/socialmessaging/get-whatsapp-message-media.html) command.

```
aws socialmessaging get-whatsapp-message-media --media-id {MEDIA_ID} --origination-phone-number-id {ORIGINATION_PHONE_NUMBER_ID} --destination-s3-file bucketName={BUCKET},key=inbound_
{
    "mimeType": "image/jpeg",
    "fileSize": 78144
}
```

In the preceding command, do the following:
+ Replace *\$1BUCKET\$1* with the name of the Amazon S3 bucket.
+ Replace *\$1MEDIA\$1ID\$1* with the value of the `id` field from the received event. For an example incoming media event, see [Example WhatsApp JSON for receiving a media message](managing-event-destination-dlrs.md#managing-event-destination-dlrs-example-receive-media).
+ Replace *\$1ORIGINATION\$1PHONE\$1NUMBER\$1ID\$1* with your phone number's ID.

To retrieve the media from the Amazon S3 bucket, use the following command:

```
aws s3 cp s3://{BUCKET}/inbound_{MEDIA_ID}.jpeg
```

In the preceding command, do the following:
+ Replace *\$1BUCKET\$1* with the name of the Amazon S3 bucket.
+ Replace *\$1MEDIA\$1ID\$1* with the MEDIA\$1ID returned from the previous step. 

# Example of responding to a message with a read receipt and reaction
<a name="example-response"></a>

In this example, your customer, Diego, sent you a message saying "Hi" and you respond to him with a read receipt and hand wave emoji. 

## Prerequisites
<a name="example-response_prerequisite"></a>

To receive a notification that Diego sent a message, you must have set up an event destination Amazon SNS topic and subscribed to a topic endpoint.

## Responding
<a name="example-response_steps"></a>

1. When the message from Diego is received, an event is published to the endpoints of the topic. The following is a snippet of what the topic publishes.
**Note**  
Because Diego initiated the conversation, it doesn't count against the quota for your business initiated conversations.  
The `whatsAppWebhookEntry` in this example is shown in JSON notation. For an example of converting the `whatsAppWebhookEntry` from JSON sting to JSON, see [Example WhatsApp JSON for receiving a message](managing-event-destination-dlrs.md#managing-event-destination-dlrs-example-receive-text).

   ```
   {
     "context": {
       "MetaWabaIds": [
         {
           "wabaId": "1234567890abcde",
           "arn": "arn:aws:social-messaging:us-east-1:123456789012:waba/fb2594b8a7974770b128a409e2example"
         }
       ],
       "MetaPhoneNumberIds": [
         {
           "metaPhoneNumberId": "abcde1234567890",
           "arn": "arn:aws:social-messaging:us-east-1:123456789012:phone-number-id/976c72a700aac43eaf573ae050example"
         }
       ]
     },
     "whatsAppWebhookEntry": "{\"...JSON STRING....",
     "aws_account_id": "123456789012",
     "message_timestamp": "2025-01-08T23:30:43.271279391Z"
   }
   //Decoding the contents of whatsAppWebhookEntry
   {
     "id": "365731266123456",
     "changes": [
       {
         "value": {
           "messaging_product": "whatsapp",
           "metadata": {
             "display_phone_number": "12065550100",
             "phone_number_id": "321010217712345"
           },
           "contacts": [
             {
               "profile": {
                 "name": "Diego"
               },
               "wa_id": "12065550102"
             }
           ],
           "messages": [
             {
               "from": "14255550150",
               "id": "wamid.HBgLMTQyNTY5ODgzMDIVAgASGCBDNzBDRjM5MDU2ODEwMDkwREY4ODBDRDE0RjVGRkexample",
               "timestamp": "1723506035",
               "text": {
                 "body": "Hi"
               },
               "type": "text"
             }
           ]
         },
         "field": "messages"
       }
     ]
   }
   ```

1. To show Diego you received the message, set the status to `read`. Diego will see two blue check marks next to the message on his device.
**Note**  
You must specify base64 encoding when you use the AWS CLI version 2. This can be done by adding the AWS CLI paramater `--cli-binary-format raw-in-base64-out` or changing the AWS CLI global configuration file. For more information, see [https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-settings](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-settings) in the *AWS Command Line Interface User Guide for Version 2*.

   ```
   aws socialmessaging send-whatsapp-message --message '{"messaging_product":"whatsapp","message_id":"'{MESSAGE_ID}'","status":"read"}' --origination-phone-number-id {ORIGINATION_PHONE_NUMBER_ID} --meta-api-version v20.0    
   ```

   In the preceding command, do the following:
   + Replace *\$1ORIGINATION\$1PHONE\$1NUMBER\$1ID\$1* with the phone number ID that Diego sent his message to `phone-number-id-976c72a700aac43eaf573ae050example`.
   + Replace *\$1MESSAGE\$1ID\$1* with the unique identifier of the message. This is the same value of the `id` field in the received message `wamid.HBgLMTQyNTY5ODgzMDIVAgASGCBDNzBDRjM5MDU2ODEwMDkwREY4ODBDRDE0RjVGRkexample`.

1. You can send Diego a hand wave reaction.

   ```
   aws socialmessaging send-whatsapp-message --message '{"messaging_product":"whatsapp","recipient_type":"individual","to":"'{PHONE_NUMBER}'","type": "reaction","reaction": {"message_id": "'{MESSAGE_ID}'","emoji":"\uD83D\uDC4B"}}' --origination-phone-number-id {ORIGINATION_PHONE_NUMBER_ID} --meta-api-version v20.0
   ```

   In the preceding command, do the following:
   + Replace *\$1PHONE\$1NUMBER\$1* with Diego's phone number, `14255550150`.
   + Replace *\$1MESSAGE\$1ID\$1* with the unique identifier of the message. This is the same value of the `id` field in the received message `wamid.HBgLMTQyNTY5ODgzMDIVAgASGCBDNzBDRjM5MDU2ODEwMDkwREY4ODBDRDE0RjVGRkexample`.
   + Replace *\$1ORIGINATION\$1PHONE\$1NUMBER\$1ID\$1* with the phone number ID that Diego sent his message to: `phone-number-id-976c72a700aac43eaf573ae050example`.

## Additional resources
<a name="example-response_additional_resources"></a>
+ Enable [event destinations](managing-event-destinations.md) to log events and receive incoming messages.
+ For a list of WhatsApp message objects, see [Messages](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#message-object) in the *WhatsApp Business Platform Cloud API Reference*.