使用 Google Script 的 Web App 作为 Webhook 直接接收推送通知 [英] Use Google Script's Web App as Webhook to receive Push Notification directly

查看:24
本文介绍了使用 Google Script 的 Web App 作为 Webhook 直接接收推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:Google 云端硬盘中的更改 => 将通知推送到 https://script.google.com/a/macros/my-domain/... => 应用已被推送以采取行动.我不想设置中间 Webhook 代理来接收通知.相反,让 Web 应用程序(通过 Google Script)接收它并直接推送.

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.

由于相关功能没有记录(就在这里:https://developers.google.com/drive/web/push) ,下面是我尝试过但失败的代码.1.以上想法可行吗??2. 我的代码 doPost(R) 似乎无法正确接收通知(R 参数).无论如何,更改Google Drive后没有任何反应.任何问题?(我尝试记录输入参数R以查看其真实结构并确定OAuth的参数Obj是否与普通Drive App相同,但在记录之前发生错误)

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


....

推荐答案

Cloud Functions HTTP 触发器(s) 也可能是一个选项......

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

(在提出此问题时尚不存在).这只需要在 Google Drive 设置中将触发器 URL 设置为通知 URL - 并为触发器添加一些 NodeJS 代码;不管它做什么.一个可以例如.发送电子邮件和/或 FCM 类似的推送通知.该触发器也可以从 App Script 触发,使用 UrlFetchApp应用脚本 API.一个可以有多个触发器,它们执行不同的任务(App Script 只是一种可能性).

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

这篇关于使用 Google Script 的 Web App 作为 Webhook 直接接收推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆