[英]Google Apps Script works when executed manually but not when launched by a trigger & no logs produced
I am using Google Apps Script (GAS) deployed as a web app in order to receive an JSON-POST API call from Nexmo (SMS) and to make REST API call to PubNub passing text of this SMS for further distribution. 我正在使用部署为网络应用程序的Google Apps脚本(GAS),以接收来自Nexmo(SMS)的JSON-POST API调用,并通过将此SMS文本传递给PubNub进行REST API调用,以进行进一步分发。
When I manually execute the script everything works as expected. 当我手动执行脚本时,一切都会按预期进行。 However, when the script is triggered by the post API call to GAS webhook, SMS data is passed to GAS but there does not seem to be a call to PubNub API.
但是,当脚本由对GAS webhook的post API调用触发时,SMS数据将传递给GAS,但似乎没有对PubNub API的调用。
I tried using Logger, Stackdriver, and custom logging function to a spreadsheet but there are no logs produced when script is executed by doPost(e) trigger. 我尝试将Logger,Stackdriver和自定义日志记录功能用于电子表格,但是doPost(e)触发器执行脚本时没有日志生成。 When executed manually logs appear as expected (no errors).
手动执行后,日志将按预期显示(无错误)。 I would greatly appreciate any suggestions as I am completely at a loss as to what is wrong and how to start tracking it down.
对于任何建议,我将不胜感激,因为我完全不知道什么地方出了问题以及如何开始对其进行跟踪。 Below is a simplified code:
下面是一个简化的代码:
//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);
}
EDIT: At this point my main obstacle is that nothing appears in console.log or Logger.log even if I try to log raw post data before doing anything else with it. 编辑:在这一点上,我的主要障碍是,即使我尝试在处理任何其他内容之前记录原始的发布数据,也不会在console.log或Logger.log中显示任何内容。 At the same time curl request emulating post call shows no errors, HTTP 200, and acknowledges that a post call was made.
同时,模拟请求调用的curl请求未显示HTTP 200错误,并确认进行了调用。
Thank you to @tehhowch for pointing out my fundamental lack of understanding how return
works. 感谢@tehhowch指出我根本不了解
return
工作原理。 Putting return before rest of the code was executed made remaining code invisible to the interpreter and rendered code equivalent to the following: 将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.