Blog

WhatsApp Cloud API Integration: Receiving Attachments from WhatsApp to Salesforce

September 12, 2022
by
Cloud Peritus
WhatsApp Cloud API Integration: Receiving Attachments from WhatsApp to Salesforce

Introduction

Well hello there! Good to see you again (metaphorically speaking, I can’t see you…or…can..no, can’t see you at all). We bring you another thrilling installment of integrating Salesforce with WhatsApp. We thought it only fair to show you how to receive attachments after showing you how to send them. Our exceptional developers will guide you through Media Object IDs, HTTP requests, and webhooks to complete our final foray into WhatsApp integration. So take our proverbial hand (you had better be reaching out right now) and thus begin the final chapter.

Use Case

Previously we used the example of wanting to send your customer a survey. In this case, what if you wanted to receive something from them? Let’s say it was a screenshot of an issue they are facing on their phone. WhatsApp is a convenient way for your customer to send the picture to you, but you want to capture this file on Salesforce. What could you possibly do in such— oh that’s right, We are going to teach you exactly how to tackle such a scenario. Read on!

Prerequisite

To receive attachments or any type of messages sent from WhatsApp to Salesforce, firstly a Webhook must be set up. Follow this link for setting up and configuring the Webhook.

Attachments in WhatsApp

Attachments in WhatsApp are treated as media objects. Any attachment that is sent to WhatsApp will have a Media Object Id, using which we can retrieve/download the attachment to save it in Salesforce for business operations. Once you have successfully uploaded media files to the API, the media ID is included in the response to your call. In a similar way, when a business account receives a media message, it downloads the media and uploads it to the Cloud API automatically. That event triggers Webhooks and sends you a notification that includes the media ID. Below is the sequence of steps to do this:

  1. On successful upload of an attachment, the configured Webhook sends an HTTP request to Salesforce with the Media Object Id.
  2. With this Media Object Id, make an HTTP GET request to the Retrieve Media URL endpoint to get the media URL.
  3. We will get the binary data from the HTTP response which we can utilize to save the attachment in Salesforce. All the media URLs expire within 5 mins.

Let’s see this in action now.

Upload an Attachment to WhatsApp

Let’s upload an image to the WhatsApp Business Account for this example.

As stated above, this triggers a HTTP Request to Salesforce with the Media Object Id via Webhook. The request body holds the Attachment metadata in the messages object as shown in the screenshot below.

It states the Attachment type, which is image/jpeg here, and the Id which is nothing but the Media Object Id.  

Retrieve & Save the Attachment from WhatsApp in Salesforce

To download the image, we need to get the download URL by hitting the Retrieve Media URL endpoint using the Media Object Id. Before doing this, make sure you have the Remote Site Settings configured in Salesforce to access the Graph API endpoint which is used to exchange data with WhatsApp.  

In our case, the media object id is 1646785179038797. Hence the endpoint URL will be https://graph.facebook.com/v13.0/1646785179038797. Make an HTTP GET request to this URL. The Header must also contain the Bearer Access Token. Follow this link to know how to get the access token. Find the code snippet below for the request details.

This returns the image metadata on a successful response with the media download URL, mime type, file size, and Id as shown in the below screenshot. Note that this media download URL expires in 5 minutes.

Now, make another GET call to the Download Media Content endpoint with the media download URL and the access token to get the binary data which we will use to store the file in Salesforce. Before making this callout, make sure to add this media download URL domain in Remote Site Settings. This URL domain is constant for any media type such as document, video, or audio.

If you try to make a GET call to the media URL which is already expired, you will receive a 404 Not Found response code. In that case, you must retrieve a new media URL and download it again.

The returned response doesn’t have any payload. It directly returns the binary value as shown below.

We can now use this Blob to save the image to Salesforce Files using the ContentVersion Object, which stores the attachment information by setting the Blob value to the VersionData field. This creates the ContentDocument record automatically, which is the parent to ContentVersion. Suppose you want to attach this file to any Salesforce record or share it with Users, Groups, etc. In that case, you will need to create the ContentDocumentLink record by setting the ContentDocumentId and LinkedEntityId which holds the Id of the Salesforce record to which the attachment should be related. In this example, we are attaching the image to the Contact Record.

The ShareType V represents Viewer permission. The user can explicitly view but not edit the shared file. This uploads the media to Salesforce Files successfully as shown in the below message.

You can retrieve and save other types of attachments such as documents, audio, and video following the same process.

Conclusion

Due to Apex Governor Limits, we cannot download files larger than 6MB when using synchronous Apex or 12MB with asynchronous Apex. If we make an HTTP request to the Download Media API for attachments that exceed these limits, we will get a Callout Exception as shown below:

A potential workaround is to have an external service or Salesforce Functions to act as a middleware to read and import these files (up to 2GB) into Salesforce via the Salesforce REST API.

The End of the Road

Here we must end our voyage into WhatsApp integration with Salesforce. You have learned how to set up WhatsApp integration to send a message from Salesforce, how to receive a message, how to send an attachment, and finally, how to receive one. We have to commend your dedication to finishing every installment and hope you come back often. In fact, next time we will write about…well, I guess you will have to come back to find out.

If you are interested in learning more about how we can address your complex business needs through our innovation and thought leadership, please reach out to info@cloudperitus.com or visit our website www.cloudperitus.com.

Reference Links

WhatsApp Media Messages

Authors

Karthik Peddinti

Karthik Peddinti

Certified Salesforce Developer, and Senior Software Engineer at Cloud Peritus
Pavan Kumar Tavva

Pavan Kumar Tavva

Associate Software Engineer at Cloud Peritus
Tim Kern

Tim Kern

Business Analyst at Cloud Peritus with a focus on Hitech and Financial Services.