# How to Automatically Save Customer Documents to Google Drive using DoubleTick

When customers send documents (like PDFs) on WhatsApp, they are stored in DoubleTick’s data storage.

Without automation, your team has to:

* Open each chat
* Download the file
* Upload it manually to Drive

This process is repetitive and error-prone.

#### <mark style="color:$primary;">**What this setup does**</mark>

Once configured, every time a customer sends a document:

* The file is automatically fetched from DoubleTick
* It is uploaded to Google Drive
* A shareable link is generated
* The link is logged into a Google Sheet

No manual work. Everything is tracked automatically.

***

#### <mark style="color:$primary;">**How the Automation Works**</mark>

Here’s the complete flow:

1. Customer sends a document on WhatsApp
2. DoubleTick webhook triggers (**On Message Received**)
3. Bot sends document URL to your API
4. Google Apps Script fetches the file securely
5. File is uploaded to Google Drive
6. Drive link is saved in Google Sheets

***

#### <mark style="color:$primary;">**Before You Begin**</mark>

Make sure you have:

* DoubleTick account with WhatsApp API active
* DoubleTick **Developer API Key**
* Google account (Drive + Sheets + Apps Script access)
* Google Drive Folder ID (from folder URL)
* Google Sheet ID (from sheet URL)

***

#### <mark style="color:$primary;">**Part 1: Create Google Apps Script**</mark>

***

#### <mark style="color:$primary;">**Step 1: Open Apps Script**</mark>

* Go to: script.google.com
* Click **New Project**

***

#### <mark style="color:$primary;">**Step 2: Add Script**</mark>

Paste the following script:

```
function doPost(e) {
  var params = JSON.parse(e.postData.contents);
  var messageUrl = params.messageUrl;
  var folderId = params.folderId;

  var authKey = "YOUR_DOUBLETICK_API_KEY";

  var response = UrlFetchApp.fetch(messageUrl, {
    method: "get",
    headers: { "Authorization": authKey }
  });

  var blob = response.getBlob().setName("Document_" + new Date().getTime() + ".pdf");

  var folder = DriveApp.getFolderById(folderId);
  var file = folder.createFile(blob);
  var fileUrl = file.getUrl();

  var spreadsheet = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID");
  var sheet = spreadsheet.getSheets()[0];
  sheet.appendRow([new Date(), fileUrl]);

  return ContentService.createTextOutput(JSON.stringify({ driveUrl: fileUrl }))
    .setMimeType(ContentService.MimeType.JSON);
}
```

***

#### <mark style="color:$primary;">**Step 3: Update Required Values**</mark>

Replace:

* `YOUR_DOUBLETICK_API_KEY` → Your API key
* `YOUR_SPREADSHEET_ID` → Your Google Sheet ID

***

#### <mark style="color:$primary;">**Step 4: Deploy Script**</mark>

* Click **Deploy → New Deployment**
* Select **Web App**
* Set access to: **Anyone**
* Click **Deploy**
* Copy the **Web App URL**

***

#### <mark style="color:$primary;">**Part 2: Setup Bot in DoubleTick**</mark>

***

#### <mark style="color:$primary;">**Step 1: Create Bot**</mark>

* Go to **Bots**
* Click **Create New Bot**

***

#### <mark style="color:$primary;">**Step 2: Add Webhook Trigger**</mark>

* Add **Webhook Node**
* Event: **On Message Received**
* Copy webhook URL

***

#### <mark style="color:$primary;">**Step 3: Register Webhook**</mark>

* Go to **Settings → Webhooks**
* Click **Create Webhook**
* Paste URL
* Select event: **On Message Received**
* Save

***

#### <mark style="color:$primary;">**Step 4: Add Call API Node**</mark>

Configure:

* Method: POST
* URL: (Apps Script Web App URL)
* Body Type: JSON

```
{
  "messageUrl": "{{message.url}}",
  "folderId": "YOUR_GOOGLE_DRIVE_FOLDER_ID"
}
```

***

#### <mark style="color:$primary;">**Step 5: Activate Bot**</mark>

* Save the bot
* Make sure it is **active**

***

#### <mark style="color:$primary;">**Part 3: Test the Setup**</mark>

1. Send a PDF to your WhatsApp number
2. Wait a few seconds

#### Expected Result:

* File appears in Google Drive
* A new row is added in Google Sheet
* Sheet contains:
  * Timestamp
  * File link

***

#### <mark style="color:$primary;">**Tips & Best Practices**</mark>

* Add timestamp to file names to avoid duplicates
* Use a dedicated Drive folder for organization
* Extend the sheet with more fields (customer number, etc.)
* You can test using Postman by sending a file URL manually

***

#### <mark style="color:$primary;">**Summary**</mark>

With this setup, you’ve built a fully automated system that:

* Captures customer documents instantly
* Stores them securely in Google Drive
* Maintains a real-time log in Google Sheets

This reduces manual effort, improves organization, and ensures no document is ever missed.
