Estoy tratando de enviar mensajes de texto en un disparador de tiempo en una hoja de Google usando Twilio para enviar los textos. Cuando hago clic en el botón Ejecutar, funciona bien, pero cuando configuro un disparador de tiempo, dice que está en estado, está completado pero no funciona. Además, cuando hago clic en Ejecuciones, aparece un error que dice "Excepción: la solicitud falló para https://api.twilio.com devolvió el código 400. Respuesta del servidor truncada: {"código": 21211, "mensaje": "El ' El número de destino no es un número de teléfono válido.", "more_info": "https://www.twilio.com/docs/errors/21211", "status": 400} (use la opción muteHttpExceptions para examinar la respuesta completa)"
Soy 100% nuevo en Java y estoy aprendiendo sobre él, cualquier ayuda sería genial.
Aquí está mi código:
function RunThisToSendTexts() { function sendSms(to, body) { var messagesUrl = "https://api.twilio.com/2010-04-01/Accounts/AC9837381a431941024fc87xxxxxx/Messages.json"; var payload = { "To": to, "Body" : body, "From" : "+197131999xx" }; var options = { "method" : "post", "payload" : payload }; options.headers = { "Authorization" : "Basic " + Utilities.base64Encode("AC9837381a431941024fcxxxxxxx:22c7dec97dee0cef39exxxxxxx") }; UrlFetchApp.fetch(messagesUrl, options); } function sendAll() { var sheet = SpreadsheetApp.getActiveSheet(); var startRow = 2; // First row of data to process var numRows = sheet.getLastRow() - 1; var dataRange = sheet.getRange(startRow, 1, numRows, 2) var data = dataRange.getValues(); for (i in data) { var row = data[i]; try { response_data = sendSms(row[0], row[1]); status = "sent"; } catch(err) { Logger.log(err); status = "error"; } sheet.getRange(startRow + Number(i), 3).setValue(status); } } sendAll(); }Intenta escribirlo así:
function sendSms(to, body) { var messagesUrl = "https://api.twilio.com/2010-04-01/Accounts/AC9837381a431941024fc87xxxxxx/Messages.json"; var payload = { "To": to, "Body" : body, "From" : "+197131999xx" }; var options = { "method" : "post", "payload" : payload }; options.headers = { "Authorization" : "Basic " + Utilities.base64Encode("AC9837381a431941024fcxxxxxxx:22c7dec97dee0cef39exxxxxxx") }; UrlFetchApp.fetch(messagesUrl, options); } function sendAll() { var sheet = SpreadsheetApp.getActiveSheet(); var startRow = 2; // First row of data to process var numRows = sheet.getLastRow() - 1; var dataRange = sheet.getRange(startRow, 1, numRows, 2) var data = dataRange.getValues(); for (i in data) { var row = data[i]; try { response_data = sendSms(row[0], row[1]); status = "sent"; } catch(err) { Logger.log(err); status = "error"; } sheet.getRange(startRow + Number(i), 3).setValue(status); } }Y simplemente ejecute sendAll();
Prueba sendAll() de esta manera:
function sendAll() { const ss = SpreadsheetApp.getActive(); const sh = ss.getActiveSheet(); const sr = 2; const data = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues(); var status = 'Error' data.forEach((r, i) => { sendSms(r[0], r[1]); status = 'sent'; }); sh.getRange(i + sr, 3).setValue(status); }