简体   繁体   中英

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.

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.

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. 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. At the same time curl request emulating post call shows no errors, HTTP 200, and acknowledges that a post call was made.

Thank you to @tehhowch for pointing out my fundamental lack of understanding how return works. Putting return before rest of the code was executed made remaining code invisible to the interpreter and rendered code equivalent to the following:

//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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM