Google Apps 脚本在手动执行时有效,但在由触发器启动时无效且不生成日志
Google Apps Script works when executed manually but not when launched by a trigger & no logs produced
我正在使用部署为 Web 应用程序的 Google Apps 脚本 (GAS),以便接收来自 Nexmo 的 JSON-POST API 电话(短信)并使 REST API 调用 PubNub 传递此 SMS 的文本以供进一步分发。
当我手动执行脚本时,一切正常。然而,当脚本被 post API 调用 GAS webhook 触发时,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 中什么也没有出现,即使我尝试在对它做任何其他事情之前记录原始 post 数据。同时,模拟 post 调用的 curl 请求显示没有错误,HTTP 200,并确认已进行 post 调用。
感谢@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
}
我正在使用部署为 Web 应用程序的 Google Apps 脚本 (GAS),以便接收来自 Nexmo 的 JSON-POST API 电话(短信)并使 REST API 调用 PubNub 传递此 SMS 的文本以供进一步分发。
当我手动执行脚本时,一切正常。然而,当脚本被 post API 调用 GAS webhook 触发时,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 中什么也没有出现,即使我尝试在对它做任何其他事情之前记录原始 post 数据。同时,模拟 post 调用的 curl 请求显示没有错误,HTTP 200,并确认已进行 post 调用。
感谢@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
}