手动执行时,Google Apps脚本有效,但由触发器启动且没有产生日志时,它们不起作用

时间:2019-01-15 10:36:42

标签: google-apps-script pubnub

我正在使用部署为网络应用程序的Google Apps脚本(GAS),以便接收来自Nexmo(SMS)的JSON-POST API调用,并通过传递此SMS文本向PubNub进行REST API调用,以进一步分发。< / p>

当我手动执行脚本时,一切正常。但是,如果脚本是通过对GAS Webhook的post API调用触发的,则SMS数据将传递给GAS,但似乎没有对PubNub API的调用。

我尝试将Logger,Stackdriver和自定义日志记录功能用于电子表格,但是doPost(e)触发器执行脚本时没有生成日志。手动执行后,日志将按预期显示(无错误)。对于任何建议,我将不胜感激,因为我完全不知道什么地方出了问题以及如何开始对其进行跟踪。下面是一个简化的代码:

//this is a function that fires when the webapp receives a GET request
function doGet(e) {
  return HtmlService.createHtmlOutput("request received");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {
//  var myData = JSON.parse(e.postData.contents); // below sample data analogous to what I would get from JSON-POST
  var myData = { "msisdn": "447700900001", "to": "447700900000", "messageId": "0A0000000123ABCD1", "text": "B", "type": "text", "keyword": "B", "message-timestamp": "2019-01-01T12:00:00.000+00:00" }
  var responseText = myData.text;
  return HtmlService.createHtmlOutput("post request received");

// This is the PubNub API call
  var PUB_KEY = 'demo';
  var SUB_KEY = 'demo';
  var CHANNEL = 'poll_demo';
  var url = 'http://pubsub.pubnub.com/publish/' + PUB_KEY + '/' + SUB_KEY + '/0/' + CHANNEL + '/0/' + escape('"' + responseText + '"');
  var response = UrlFetchApp.fetch(url);
}

编辑: 在这一点上,我的主要障碍是console.log或Logger.log中什么也没有出现,即使我尝试在处理任何其他内容之前记录原始的发布数据。同时,curl请求模拟post调用未显示错误,HTTP 200,并确认已进行了post调用。

1 个答案:

答案 0 :(得分:1)

感谢@tehhowch指出我对return的工作原理缺乏基本了解。将return放在其余代码执行之前,使其余代码对解释器不可见,并呈现与以下代码等效的代码:

//this is a function that fires when the webapp receives a GET request
function doGet(e) {
  return HtmlService.createHtmlOutput("request received");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {
//  var myData = JSON.parse(e.postData.contents); // below sample data analogous to what I would get from JSON-POST
  var myData = { "msisdn": "447700900001", "to": "447700900000", "messageId": "0A0000000123ABCD1", "text": "B", "type": "text", "keyword": "B", "message-timestamp": "2019-01-01T12:00:00.000+00:00" }
  var responseText = myData.text;
  return HtmlService.createHtmlOutput("post request received");
// PubNub inaccessible code is here
}