Example App Scripts Google Form, Sending All response to Whatsapp

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);
  }
}

FORM ID from URL https://docs.google.com/forms/d/1mYTARCa3_WEQU2YqWVjtp5tAlvGv4KW2bimfqrdQCnQ/edit#responses













Leave a Reply

Your email address will not be published. Required fields are marked *