Last active
November 7, 2023 16:44
-
-
Save caruizdiaz/d105d09a983df792757c202c4e976dab to your computer and use it in GitHub Desktop.
Google form demo
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
// | |
// Part of 2Chat.co | |
// Learn more about how to use this code at: https://blog.2chat.co/send-a-whatsapp-message-for-each-google-form-response/ | |
// | |
// | |
// Paste the URL you copied from the flow you created on 2Chat | |
// | |
// ❗ Replace everything inside the quotation marks with the URL generated by 2Chat ❗ | |
// | |
const ENDPOINT_URL = "https://api.p.2chat.io/open/flows/FLW6ec87f3e-4b85-4371-91e6-cbd4891eedf3"; | |
// | |
// The following fields can be changed at your will and depending on | |
// the data you are collecting with your form. | |
// In our case, we use the first name and the phone number of the | |
// person submitting the form. | |
// | |
const WA_GROUP_UUID = "WAG648339f3-ecb7-4e63-9b83-546eb7ed9f5b"; | |
const REVIEWER_NAME_FIELD_TITLE = "Reviewer name"; // ❗ Change this value to the title you used in your form ❗ | |
const APARTMENT_NAME_FIELD_TITLE = "Apartment name"; // ❗ Change this value to the title you used in your form ❗ | |
const CLEANER_NAME_FIELD_TITLE = "Cleaner name"; // ❗ Change this value to the title you used in your form ❗ | |
const DATE_FIELD_TITLE = "Cleaning Date"; // ❗ Change this value to the title you used in your form ❗ | |
function onSubmit(e) { | |
var form = FormApp.getActiveForm(); | |
var allResponses = form.getResponses(); | |
var latestResponse = allResponses[allResponses.length - 1]; | |
var response = latestResponse.getItemResponses(); | |
var responses = {}; | |
var formattedResponses = ""; | |
for (var i = 0; i < response.length; i++) { | |
var question = response[i].getItem().getTitle(); | |
var answer = response[i].getResponse(); | |
responses[question] = answer; | |
// | |
// We group all questions and answers into a single variable, and use WhatsApp formatting | |
// to make it more readable to the recipient | |
// | |
// 📙 Learn more about WhatsApp formatting: https://2chat.co/tools/whatsapp-text-formatter | |
// | |
formattedResponses += `*${question}*:\n_${answer}_\n\n`; | |
} | |
// | |
// We build the payload to send the form submission to 2Chat | |
// | |
// The only mandatory field here is `to_number`, which is the intended recipient of our message | |
// | |
// 📙 Learn more about flows here: https://help.2chat.io/en/collections/4808619-flows | |
// | |
const payload = { | |
"to_group_uuid": WA_GROUP_UUID, | |
"variables": { | |
"reviewerName": responses[REVIEWER_NAME_FIELD_TITLE], | |
"cleanerName": responses[CLEANER_NAME_FIELD_TITLE], | |
"cleaningDate": responses[DATE_FIELD_TITLE], | |
"apartmentName": responses[APARTMENT_NAME_FIELD_TITLE], | |
"responses": formattedResponses | |
} | |
} | |
console.log("Sending message to group:", WA_GROUP_UUID); | |
var options = { | |
"method": "POST", | |
"contentType": "application/json", | |
"payload": JSON.stringify(payload) | |
}; | |
UrlFetchApp.fetch(ENDPOINT_URL, options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment