WhatsApp API integration with Google drive using API chains
This link template will allow you to save files from WhatsApp messages to Google drive. To work with the service, you need to go through the steps of setting up your own "service" for saving a file, since Google drive does not have a method that would allow you to save files via a url.
In the initial setup, you need to specify the address of the function that will be deployed on the Google cloud server. More on this below:
1 - To start, create a new project in Google cloud or select an existing project;
2 - Billing must be enabled in the project for further correct operation. Google itself will also remind you about this when trying to connect the API;
3 - Next, you need to go to the project libraries section, find and connect the “Cloud Build API" - you will need it to deploy the function.
And also connect the "Google Drive API" - this is an API for directly connecting to Google Drive.
4 -After that, you need to add a service user to the project: go to service accounts and add a service account.
Specify any name.
In the Permissions tab, add the Owner rights to the user.
5 - Next, go to the Google Drive folder we are interested in and give it permission for a newly created service account. To do this, right-click on it, select the “Share" option.
After that, we add the address of the service account, as well as the right to edit and click send.
This address can be found by clicking on the link, selecting the desired project.
6 - Create a new Google function that will save a file to Google Drive. To do this, go to the address, select the project of interest at the top and create a new function.
7 - In the function settings, specify the region that is convenient for you, set the name of the function.
You can immediately copy the full URL of the link to your widget in the Chat API.
In the function settings below, we indicate that authentication is also allowed for unauthorized users. Click "save". Below, if you plan to save files over 256 megabytes, specify 512 megabytes.
In the part where the user is specified under which the code will be executed (runtime service account), you must specify your user, otherwise access to the Drive API will be unavailable, click "continue".
8 - Copy the index.js and package.json from the project https://github.com/chatapi/chainapi-google-drive in the window that opens.
9 -Next, you need to change the shared Folder Key parameter in the index.js.
Go to the folder to which you have granted rights in Google drive, the address contains the ID of this folder, for example, in the address "https://drive.google.com/drive/folders/0BxL2BKcYFzUQTDJYV1djOEtnYzg?something=0-WVKid8cJNA2-Nw " the ID will be 0BxL2BKcYFzUQTDJYV1djOEtnYzg.
If you don't want the files to be automatically available to all users in the ShareFile constant inside index.js must be set to "false".
In the upper right corner of the function settings, you must specify the download function at the entry point.
Click the "deploy" button and wait for the process to complete.
10 - After that, you can continue the initial configuration in the API chains. If you forgot to enter the address of the function - wait for the completion of its deployment, go to edit and copy the address.
After that, you will need to specify in the dynamic data where to get the file url from.
A successful response may contain the sharedLink parameter and/or the direct Download Link parameter, the former is necessary for direct access to the file in Google Drive, the latter for direct download.
If the file weighs more than 100 megabytes, Google will not allow a direct link to download the file because of the built-in antivirus. Circumvention of such restrictions is possible: https://bytesbin.com/skip-google-drive-virus-scan-warning-large-files / , but we do not recommend giving your API key to external resources.
In case of failure, you will get a description of the error in the err parameter and the status 500.