Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Google Script's Web App as Webhook to receive Push Notification directly

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);  
  }


....
like image 843
Pat Avatar asked Aug 01 '14 15:08

Pat


People also ask

Do push notifications use Webhooks?

Webhooks are a way to push notifications to your own servers, in real-time, as your data is created and modified, without the disadvantages of polling. We've written and maintain the code that pushes notifications to your servers as data changes.

How do I use Google push notifications?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.

Does Google Sheets support Webhooks?

Building up Google Sheets Webhooks Integration is a three step process that enables the users to send data from Google Sheets on any event change. Follow the steps given below to set up Google Sheets Webhooks Integration: Step 1: Create and Configure a new Document.


1 Answers

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).

like image 143
Martin Zeitler Avatar answered Sep 28 '22 02:09

Martin Zeitler