Blog

WhatsApp Cloud API Integration: Sending Attachments From Salesforce To WhatsApp

August 29, 2022
by
Cloud Peritus
WhatsApp Cloud API Integration: Sending Attachments From Salesforce To WhatsApp

Welcome back for another installment of integrating Salesforce with WhatsApp! In this post, our amazing developers will show you how to send attachments from Salesforce to WhatsApp to further integrate your system and generally make your life easier. So dive into this post head first and bring an umbrella because I see a cloud…Cloud API!.. Oh come on, that was funny.  

Use case

In our previous post, we were sending a survey to the customer. In this situation, you could be sending an important document that needs to be filled out or you could just be sending a fun gif (pronounce that how you will) just to maintain a connection with your customer. Either way, an attachment needs to be sent through WhatsApp, and we have the solution to your conundrum.

Prerequisite

This is the fifth installment in the WhatsApp Cloud API Integration series. Before proceeding with this document, please follow the steps for the initial configuration in the first article.

Introduction

In this blog, we will show how we can send an attachment from Salesforce to WhatsApp using the Cloud API.

Imagine there is a requirement in Salesforce to send attachments to a recipient in WhatsApp from the related Files. In the below image, we’ve uploaded an image file on the Contact record.

It is known that the uploaded image will be available on Salesforce and stored as a Files record. In order to send this attachment from Salesforce to WhatsApp, we have to generate a public downloadable link as WhatsApp Cloud API doesn't support the base64 currently.

We can generate the public downloadable link either from the Salesforce Application UI or from the backend using the ContentDistribution object. Generating the public link from UI in turn creates the ContentDistribution record in the backend.

ContentDistribution object is used to create, update, delete, or query information about a document shared externally via a link or via Salesforce CRM Content delivery.

Creating Public Downloadable Link from UI

Click on the File (Google_Rainbow) record. To generate the public link from the UI, click on the Public Link option you see on the banner at the top.

Now, click on the Create Link button.

This creates the ContentDistribution record in the backend which holds the downloadable links.

Creating a Content Distribution Record from Backend

To generate the public downloadable link via code, create a ContentDistribution record for the related ContentDocument if it does not already exist. While creating a ContentDistribution record, make sure that PreferencesAllowOriginalDownload is set to true. This generates a downloadable link that can be used to send the attachment to WhatsApp.

If we want to send an attachment as a PDF to WhatsApp, set PreferencesAllowPDFDownload and PreferencesAllowOriginalDownload as true in the ContentDistribution record.

The screenshot below shows the ContentDownloadURL generated for the image that is uploaded to files.

Send Image by URL to WhatsApp

Make an HTTP POST request to Send Image Message by URL endpoint. Make sure the Remote Site Setting is configured for this endpoint.

This POST request should include the following details:

Header: Meta information about the request. All requests that contain a message body should set the Content-Type in the header (e.g., Content-Type: application/json). The Header must also contain the Bearer Access Token. Follow this article to know how to get the access token.

Body: It contains the payload data as shown in the below screenshot. Type attribute supports the attachment type that we are going to send. It supports image, document, audio, or video. Since we are sending an image in this example, we are setting the type as image and the URL for the image will be set to the link attribute with ContentDownloadURL generated from the ContentDistribution record.

This sends the image to your WhatsApp recipients from your Business Account as shown in the screenshot below.

The successful response returns the WhatsApp Message Id.

Sending Other Attachments by URL to WhatsApp

We can also send other types of attachments such as documents, audio, and videos following the same process by changing the “type” value

Send Document By URL

Send Audio By URL

Send Video By URL

WhatsApp Media Type Limitations

Following are the limitations we have to consider while sending an attachment from salesforce to WhatsApp

Refer to this documentation WhatsApp Limitations, to know more about WhatsApp media type limitations.

NOTE: Once sent, there is no need to keep the files in Salesforce. It has no effect on WhatsApp's media.

Conclusion

In no time flat (or as long as it took you; no pressure) you can now send documents, audio, or videos from Salesforce to WhatsApp. Don’t feel the need to show off your eidetic memory, please bookmark this page for future reference and share it with colleagues, friends, or anyone within earshot. Don’t forget to come back soon to learn how to, you guessed it, receive attachments from WhatsApp to Salesforce.

If you are interested in learning more about how we can address your complex business needs, please reach out to info@cloudperitus.com.

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.