Created
January 26, 2023 10:02
-
-
Save standuply/5753ecd4f7499ed7b2f80176446e4429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saveMessage(webhookMessageResult) { | |
if (webhookMessageResult.type === 'user-answers') { | |
const dialogs = webhookMessageResult.value.data; | |
let sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); | |
let sheet = sheets[0]; | |
for (let i = 0; i < dialogs.length; i++) { | |
const dialog = dialogs[i]; | |
if (i === 0 && dialog.type === 'survey') { | |
let activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
let newSheet = activeSpreadsheet.getSheetByName(dialog.answer); | |
if (newSheet == null) { | |
newSheet = activeSpreadsheet.insertSheet(); | |
newSheet.setName(dialog.answer); | |
} | |
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(dialog.answer); | |
continue; | |
} | |
let lastRow = Math.max(sheet.getLastRow(), 1); | |
let nowDate = new Date(); | |
sheet.insertRowAfter(lastRow); | |
sheet.getRange(lastRow + 1, 1).setValue(nowDate); | |
sheet.getRange(lastRow + 1, 2).setValue(webhookMessageResult.value.userName); | |
sheet.getRange(lastRow + 1, 3).setValue(dialog.question); | |
sheet.getRange(lastRow + 1, 4).setValue(dialog.answer); | |
SpreadsheetApp.flush(); | |
} | |
} | |
} | |
//this is a function that fires when the webapp receives a GET request | |
function doGet(e) { | |
return HtmlService.createHtmlOutput("request received"); | |
} | |
//this is a function that fires when the webapp receives a POST request | |
function doPost(data) { | |
if ( | |
data && | |
data.postData && | |
data.postData.contents | |
) { | |
let webhookMessage = JSON.stringify(data.postData.contents); | |
while (typeof webhookMessage === 'string') { | |
webhookMessage = JSON.parse(webhookMessage); | |
} | |
if (Array.isArray(webhookMessage.result)) { | |
for (const result of webhookMessage.result) { | |
saveMessage(result); | |
} | |
} else { | |
saveMessage(webhookMessage.result); | |
} | |
} | |
return HtmlService.createHtmlOutput("post request received"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment