JSON integration has many practices; one of the most straightforward integrations and most used integration you can make is integrating with google sheets.
You can use this integration to:
-
Save user attributes.
-
Save reservations.
-
Save orders.
-
Use a sheet as CRM.
How to Integrate Your Bot With JSON API and Google Sheets
Prepare your sheet
-
Create a new or open an existing Google Spreadsheet
-
Prepare your sheet to accept the data as suggested below:
-
Correctly name the columns.
-
Add a Timestamp column to be auto-filled with the date and time of each record.
Prepare your AppScript
function doGet(e){
return handleResponse(e);
}
// Enter sheet name where data is to be written below
var SHEET_NAME = "Sheet1";
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
function handleResponse(e) {
// shortly after my original solution Google announced the LockService[1]
// this prevents concurrent access overwritting data
// [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
// we want a public lock, one that locks for all invocations
var lock = LockService.getPublicLock();
lock.waitLock(30000); // wait 30 seconds before conceding defeat.
try {
// next set where we write the data - you could write to multiple/alternate destinations
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
//var doc = SpreadsheetApp.openById("1BzKVTSZrhh6t9cuATPFNqENlJW5DAiBLbMlo8y3zrWY");
var sheet = doc.getSheetByName(SHEET_NAME);
// we'll assume header is in row 1 but you can override with header_row in GET/POST data
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [];
// loop through the header columns
for (i in headers){
if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
row.push(new Date());
} else { // else use header name to get data
row.push(e.parameter[headers[i]]);
}
}
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
// return json success results
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e){
// if error return this
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally { //release lock
lock.releaseLock();
}
}
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
SCRIPT_PROP.setProperty("key", doc.getId());
}
-
Open your Google Spreadsheet and go to "Extensions" in the navigation bar. Select "Apps Script."
-
Copy and paste the provided code into the Apps Script editor.
-
Run the "setup()" function:
- Save the script.
- Choose "Setup Function" from the dropdown menu.
- Click "Run" to execute the setup function.
-
Publish the script as a web app:
- In the script editor, go to "Deploy."
- Select "Deploy as a Web App" from the options.
- Click "Deploy."
-
Copy the URL of the published web app. This URL allows you to use the script's features.
In Deployment, Make sure:
-
"Execute the app as" is me (yourself).
-
“Who Has Access To The App” that anyone can access.
Prepare your Flow
-
In the flow where you wish to set your integration, add a JSON API message.
-
Select “Get Method.”
-
Paste the deployed web app URL (AKA the deployed app script). e.g, "https://script.google.com/macros/s/..."
-
Customize the parameters' names to match the column’s name in the sheet.
-
Add attributes in the URL to assign parameters. eg: "https://script.google.com/macros/s/...?first_name={{first_name}}&last_name={{last_name}}"
-
Finally, test the integration!