Use Google Script's Web App as Webhook to rece

2020-02-23 04:01发布

My Goal: Changes in Google Drive => Push Notification to https://script.google.com/a/macros/my-domain/... => App is pushed to take action. I don't want to setup an middle Webhook agent for receiving notification. Instead, let the Web App (by Google Script) to receive it and be pushed directly.

Since the relevant function is quite undocumented (just here: https://developers.google.com/drive/web/push) , below is the code I tried but failure. 1. Is above idea feasible?? 2. My code doPost(R) seems cannot receive notification (R parameter) properly. Anyway, no response after I change the Google Drive. Any problem? (I have tried to log the input parameter R so as to see its real structure and decide if the parameter Obj for OAuth is the same as normal Drive App, but error occur before log)

function SetWatchByOnce(){
  var Channel = {
    'address': 'https://script.google.com/a/macros/my-domain/.../exec',
    'type': 'web_hook',
    'id': 'my-UUID'
  };

  var Result = Drive.Changes.watch(Channel); 
  ...
}    

function doPost(R) {
  var SysEmail = "My Email";
  MailApp.sendEmail(SysEmail, 'Testing ', 'Successfully to received Push Notification');

  var Response = JSON.parse(R.parameters);
  if (Response.kind == "drive#add") {
    var FileId = Response.fileId;
    MyFile = DriveApp.getFolderById(FileId);
    ...
  }
}


function doGet(e) {
  var HTMLToOutput;  
  var SysEmail = "My Email";

  if (e.parameters.kind) { 
      //I think this part is not needed, since Push Notification by Drive is via Post, not Get. I should use onPost() to receive it. Right?

    } else if (e.parameters.code) { 
      getAndStoreAccessToken(e.parameters.code);
      HTMLToOutput = '<html><h1>App is successfully installed.</h1></html>';

    } else { //we are starting from scratch or resetting
      HTMLToOutput = "<html><h1>Install this App now...!</h1><a href='" + getURLForAuthorization() + "'>click here to start</a></html>";
    }  

    return HtmlService.createHtmlOutput(HTMLToOutput);  
  }


....

2条回答
霸刀☆藐视天下
2楼-- · 2020-02-23 04:50

Cicada,

We have done similar functions to receive webhooks/API calls many times. Notes:

  1. to get R, you need: var Response = R.parameters and then you can do Response.kind, Response.id, etc.

  2. Logger will not work with doGet() and doPost(). I set it up a write to spreadsheet -- before any serious code. That way I know if it is getting triggered.

查看更多
▲ chillily
3楼-- · 2020-02-23 04:50

Cloud Functions HTTP trigger(s) might also be an option ...

(which not yet existed at time of this question). this just requires setting the trigger URL as the notification URL, in the Google Drive settings - and adding some NodeJS code for the trigger; whatever it shall do. one can eg. send emails and/or FCM push notifications alike that. that trigger could also be triggered from App Script, with UrlFetchApp and there is the App Script API. one can have several triggers, which are performing different tasks (App Script is only one possibilty).

查看更多
登录 后发表回答