简体   繁体   English

手动执行时,Google Apps脚本有效,但由触发器启动时,则无效,并且不生成日志

[英]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.

相关问题 "Google Apps 脚本仅在手动触发时有效,其他任何触发器均无效" - Google Apps Script only works when manually triggered, not by any other trigger Google Apps-当responseStatus更改时触发脚本 - Google apps - Trigger script when responseStatus change 创建新文件夹时,如何使Google脚本触发器自动触发(手动工作) - How to get Google script trigger to fire automatically (works manually) when creating a new folder 为什么我的脚本在由触发器执行时无法运行,但在手动执行时成功运行? - How comes my script fails to run while executed by a trigger but is successful when executed manually? 例外:无效参数:Google Apps 脚本触发器中的 http://[object%20Object]。 (当我手动运行它时它正在工作) - Exception: Invalid argument: http://[object%20Object] in Google Apps Script trigger. (It's working when I run it manually) Google Apps脚本:当联系人更改时触发脚本? - Google Apps Script: Trigger a script when Contacts change? 添加新日历事件时的 Google Apps 脚本和触发器 - Google Apps Script and Trigger when new Calendar Event is added 电子表格和Google Apps脚本:当单元格中的值匹配时触发 - Spreadsheets and Google Apps Script: trigger when value in cell matches 当 Zapier 添加一行时,Google Apps 脚本 onEdit 触发器不会触发 - Google Apps Script onEdit Trigger Not Firing When Zapier Adds A Row 用户制作副本时如何触发 Google Apps 脚本 - How to trigger Google Apps script when user makes a copy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM