![](/img/trans.png)
[英]Google Apps Script only works when manually triggered, not by any other trigger
[英]Google Apps Script works when executed manually but not when launched by a trigger & no logs produced
我正在使用部署为网络应用程序的Google Apps脚本(GAS),以接收来自Nexmo(SMS)的JSON-POST API调用,并通过将此SMS文本传递给PubNub进行REST API调用,以进行进一步分发。
当我手动执行脚本时,一切都会按预期进行。 但是,当脚本由对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请求未显示HTTP 200错误,并确认进行了调用。
感谢@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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.