【问题标题】:Google Apps Script works when executed manually but not when launched by a trigger & no logs producedGoogle Apps 脚本在手动执行时有效,但在由触发器启动且不生成日志时无效
【发布时间】:2019-01-15 10:36:41
【问题描述】:

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

当我手动执行脚本时,一切都按预期工作。但是,当脚本由对 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 调用。

【问题讨论】:

  • 你为什么不返回任何东西? webapp 触发器必须返回内容或 HTML(通过相应的服务):developers.google.com/apps-script/guides/…
  • 感谢@tehhowch 指出这一点。我取消了 return HtmlService.createHtmlOutput 行的注释。在这一点上,我的主要障碍是 console.log 或 Logger.log 中没有任何内容出现,即使我尝试在对其进行任何其他操作之前记录原始帖子数据。同时 curl 请求模拟 post 调用没有显示错误,HTTP 200,并确认进行了 post 调用。
  • 您是否尝试过使用console.log("my logging info");?
  • 请注意,return HtmlService.... 之后的任何内容都不会执行 - 因为您调用了 return。可能你想要function doPost(e) { var props = getPropsForApi('pubnub'); useApi('pubnub', props, e.postData); return ContentService....; } 之类的东西(即doPost 中的return 是你做的最后一件事)
  • 来自 PubNub 在您引用的端点的 200 响应意味着您的 GET 成功地在 PubNub 网络上发布了数据。为了在另一台机器上查看此数据,您必须在该机器上初始化订阅,并等待新的发布。如果您希望查看过去发布的数据,您可以使用 Storage & Playback history API 以编程方式获取数据。您可以在 PubNub 调试控制台 pubnub.com/docs/console 中探索此功能

标签: google-apps-script pubnub


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2023-02-18
    相关资源
    最近更新 更多