Unable to make doPost(e) work - Cannot read properties of undefined

41 views
Skip to first unread message

Álvaro Castilla Vera

unread,
Oct 27, 2024, 7:31:20 AMOct 27
to Google Apps Script Community
Hello, I'm trying to send the whatsapp number from the sender of a message to my twilio account and I'm trying to extract it to a spreadsheet. (Additionally to compare the row that has it and write in another column the word 'CALL')

My problem lies in the fact that I'm getting this error each time I triy to send the HTTP POST request from my twilio flow to my google app script via webhook. The webhook is correctly set up and does connect.

Here is the error:

Error: Cannot read properties of undefined (reading 'channel') - This is the response I'm getting for the request in Twilio. I assume it isn't defining e as the json I'm sending to app script.

This is my code in google app script:

function doPost(e) {
  var sheet = SpreadsheetApp.openById('for confidentiality I cant share this').getSheetByName('HIPOTECAS'); // Obtener la hoja llamada "HIPOTECAS"
  var data = JSON.parse(e.postData.contents);
 
  var phoneNumber = data.num; // Asume que el número de teléfono viene en la propiedad 'phoneNumber'
 
  // Buscar la fila que contiene el número de teléfono
  var range = sheet.getDataRange();
  var values = range.getValues();

  for (var i = 0; i < values.length; i++) {
    if (values[i][P-1] === phoneNumber) { // Cambia P por el número de columna donde están los números (0-indexed)
      sheet.getRange(i + 1, 24).setValue('LLAMAR'); // Columna X es la número 24 (0-indexed)
      break; // Salir del bucle si se encuentra el número
    }
  }

  return ContentService.createTextOutput('Success');
}
Reply all
Reply to author
Forward
0 new messages