gpt4 book ai didi

javascript - 使用 Twilio 的 API 的 Google 脚本正确运行但未返回正确的值

转载 作者:行者123 更新时间:2023-12-01 00:10:18 24 4
gpt4 key购买 nike

我遇到了无法输出正确值的脚本问题。否则它完全按照预期工作。这是整个脚本:

var statusColumnPos = [2,5]; // Position of status column
var searchColumn = 3 // Column C to search for number of entries

var dataTab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Booking Tracker"); // Get sheet by Name

var accountSID = "accountSID"; // account SID
var token = "accessToken"; // access token
var authorizationEncoded = "Basic " + Utilities.base64Encode(accountSID+":"+token);
var twillioNumber = "xxxxxxxxxx" // twillio number
var twillioAPI = "https://api.twilio.com/2010-04-01/Accounts/"+accountSID+"/Messages.json"



function getSmsDataFromCell(pos) {
var cell = dataTab.getRange(pos, statusColumnPos[1]);
var smsNumber = cell.offset(0, 1).getDisplayValue();
var smsMessage = cell.offset(0,2).getDisplayValue();
return [smsNumber, smsMessage];
}

function statusChecker() {
var searching = true;
var count = 1;
while(searching) {
var value = dataTab.getRange(count,searchColumn);
if (value.getValue() == "") {
searching = false;
break;
} else {
count++;
}
}
var values = dataTab.getRange(statusColumnPos[0], statusColumnPos[1], count - 2);
var arr = values.getValues();
var newArr = [];
for(var i = 0; i < arr.length; i++)
{
if (arr[i][0] === "READY") {
newArr = newArr.concat(i+2);
}
}

for (var i = 0; i < newArr.length; i++) {
var data = getSmsDataFromCell(newArr[i]);
dataTab.getRange(newArr[i], statusColumnPos[1] + 3).setValue("SENDING");
var result = sendMessage(data[1], data[0], twillioNumber);
if (result) {
dataTab.getRange(newArr[i], statusColumnPos[1] + 3).setValue("SENT");
} else {
dataTab.getRange(newArr[i], statusColumnPos[1] + 3).setValue("FAILED")
}
}
}

function sendMessage(message, to, from) {
var formData = {
'Body': message,
'From': from,
'To': to,
};
var options = {
'method' : 'post',
'contentType': 'application/x-www-form-urlencoded',
'payload' : formData,
'headers' : {
'Authorization': authorizationEncoded
}
};
var response = UrlFetchApp.fetch(twillioAPI, options);
if (response.getResponseCode() === 200 ) {
return true
} else {
Logger.log(response.getContentText());
return false
};
}

我遇到的具体问题是此部分未返回 TRUE:

  var response = UrlFetchApp.fetch(twillioAPI, options);
if (response.getResponseCode() === 200 ) {
return true
} else {
Logger.log(response.getContentText());
return false
};

或者以下语句无法按预期工作:

    var result = sendMessage(data[1], data[0], twillioNumber);
if (result) {
dataTab.getRange(newArr[i], statusColumnPos[1] + 3).setValue("SENT");
} else {
dataTab.getRange(newArr[i], statusColumnPos[1] + 3).setValue("FAILED")
}
}
}

SMS 消息正在按预期发送,但发送后不是导致 SENT,而是导致 FAILED。该结构对我来说似乎很合理,所以我不想通过在脚本中交换 SENT/FAILED 来“快速修复”它。有人看到了我没有看到的东西吗?

最佳答案

根据 Twilio 的 API 文档,仅当资源更新时,POSTPUT 请求才会返回 HTTP 状态代码 200,而如果有新资源则返回 201创建:

Possible POST or PUT Response Status Codes

  • 200 OK: The request was successful, we updated the resource and the response body contains the representation.
  • 201 CREATED: The request was successful, we created a new resource and the response body contains the representation.

来源:Creating or Updating Resources with the HTTP POST and PUT Methods .

当我对此进行测试时,返回了状态代码 201 并且文本已成功发送。

关于javascript - 使用 Twilio 的 API 的 Google 脚本正确运行但未返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60121949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com