Example App Scripts Google Form: Sending All Response to Whatsapp

June 3, 2024

Script Implementation

function onFormSubmit(e) {
  record_array = []

  var form = FormApp.openById('1mYTARCa3_WEQU2YqWVjtp5tAlvGv4KW2bixxx'); // Form ID
  var formResponses = form.getResponses();
  var formCount = formResponses.length;

  var formResponse = formResponses[formCount - 1];
  var itemResponses = formResponse.getItemResponses();

  var resultString = '';

  for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
    var title = itemResponse.getItem().getTitle();
    var answer = itemResponse.getResponse();

    record_array.push(answer);
    resultString += title + ': ' + answer + '\n';
  }  

  Logger.log(resultString)

  // Send the resultString to an external API
  var apiUrl = 'https://apiservice.com/v1/wa/send'; // Replace with your API endpoint
  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify({ message: resultString, phone: "628XX250XXXX" })
  };

  try {
    var response = UrlFetchApp.fetch(apiUrl, options);
    Logger.log('Response Code: ' + response.getResponseCode());
    Logger.log('Response Body: ' + response.getContentText());
  } catch (error) {
    Logger.log('Error: ' + error.message);
  }
}

Setup Instructions

  1. Open your Google Form
  2. Click Tools > Script editor
  3. Copy the script above
  4. Replace Form ID from your form URL
  5. Update WhatsApp API endpoint and phone number
  6. Save and deploy

Key Components

Component Purpose
onFormSubmit Trigger function when form is submitted
FormApp.openById Access specific Google Form
getResponses Retrieve form submissions
UrlFetchApp Send data to WhatsApp API

Features

  • Automatic response collection
  • Formatted message creation
  • WhatsApp integration
  • Error handling
  • Response logging
#GoogleAppsScript #Forms #WhatsApp #Automation