App作为Webhook直接接收推送通知

App作为Webhook直接接收推送通知

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

问题描述

我的目标:Google Drive变更=>将通知推送至 ... =>应用程序被迫采取行动。
我不想设置中间Webhook代理来接收通知。相反,让Web应用程序(通过Google脚本)接收它并直接推送。

由于相关函数没有记录(只是在这里:),下面是我试过但代码失败的代码。
1.上述想法是否可行?
2.我的代码doPost(R)似乎无法正确接收通知(R参数)。无论如何,我更改Google云端硬盘后没有回应。任何问题? (我曾尝试记录输入参数R以查看其实际结构并确定OAuth的参数Obj是否与普通Drive App相同,但在日志之前发生错误)

 函数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);
...
}

函数doPost(R){
var SysEmail =我的电子邮件;
MailApp.sendEmail(SysEmail,'测试','成功接收推送通知');

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


函数doGet(e){
var HTMLToOutput;
var SysEmail =我的电子邮件;

if(e.parameters.kind){
//我认为这部分不需要,因为推送通知是通过Post,而不是Get。我应该使用onPost()来接收它。对?

} else if(e.parameters.code){
getAndStoreAccessToken(e.parameters.code);
HTMLToOutput ='< html>< h1> App已成功安装。< / h1>< / html>';
$ b $} else {//我们从头开始或重新设置
HTMLToOutput =< html>< h1>立即安装此应用程序...!< / h1>< a href ='+ getURLForAuthorization()+'>点击此处开始< / a>< / html>;
}

返回HtmlService.createHtmlOutput(HTMLToOutput);
}


....


解决方案

Cicada,



我们已经完成了类似的功能来多次接收webhooks / API调用。注:获得R,您需要: var Response = R.parameters


  1. $ c>然后你可以做 Response.kind Response.id 等等。


  2. 记录器不能与doGet()和doPost()一起使用。我在任何严肃的代码之前设置了对电子表格的写入。那样我知道它是否被触发。



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


....
解决方案

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.

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

08-27 14:03