需要帮助为SpreadsheetApp(Google Appscript)创建GMAIL Pub / Sub Notification服务 [英] Need Help creating GMAIL Pub/Sub Notification service to SpreadsheetApp (Google Appscript)

查看:110
本文介绍了需要帮助为SpreadsheetApp(Google Appscript)创建GMAIL Pub / Sub Notification服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我不必重新发布这个问题,但我的老板把这个问题推到了最高优先级,我需要帮助来解决这个问题。我正尝试使用GAS脚本从我的GSuite域上的地址(目前,我正在测试我的地址)中提取发布/订阅通知。基本上,我试图完成所有这些材料中描述的内容:



1)来自Spencer Easton的Great Github项目 (Instructional Video)

2)pubsub API for GMAIL



3)通知帮助



4)实时通知

5)端点文档



我在Google Drive中的工作域中创建了参考文献1中记录的GAS项目, Chrome商店,添加了PUB / SUB API和库,连接到带有Gmail服务帐户权限的Pub / Sub Cloud服务,并创建了必要的主题/订阅。我运行了enrollEmail并取回了一个有效的history_Id,我可以使用API​​浏览器查找并确认该文件可以写入Drive上指定的电子表格。但doPost方法决不会像我希望的那样触发事件在电子表格上书写。有人可以帮我找出为什么这段代码不适用于我的域名吗?我没有明确的错误信息,因为我试图运行邮政服务。



这是我的代码:



 

 

 

 ).getSheets()[0]; ss.appendRow(['Push was received'+ new Date()]);尝试{var message = JSON.parse(e.postData.getDataAsString())。message; var data = Utilities.newBlob(Utilities.base64Decode(message.data))。getDataAsString(); ss.appendRow([new Date(),message.message_id,data]); } catch(e){ss.appendRow(['failure',e]); } return 200; } function enrollEmail(){var EMAIL = Session.getActiveUser()。toString(); var watchRes = Gmail.newWatchRequest(); watchRes.labelIds = [INBOX]; watchRes.labelFilterAction ='include'; watchRes.topicName ='projects / project-id-3596301251382091354 / topics / eWarning'; var response = Gmail.Users.watch(watchRes,EMAIL); Logger.log(响应); var ss = SpreadsheetApp.open(DriveApp.getFilesByName('Episode_Log')。next())。getSheets()[0]; ss.appendRow(['Manual Test']); // This This works} function checkHistory(){var EMAIL = Session.getActiveUser()。toString(); Logger.log(Gmail.Users.History.list(EMAIL,{startHistoryId:'****'})); //使用有效的ID}  


$ b

谢谢家伙非常多!



* PS我没有在Google Cloud帐户中明确启用结算功能。有人知道我是否需要为此功能付费吗?

解决方案

对于像我这样需要在Google中创建监控服务的人应用程序和需要快速的东西,我想出了一个快速的脚本来检查特定标签的电子邮件。通过GmailApp,您也可以按主题查看消息。以下代码如下:

var EMAILID = Session.getActiveUser()。 getEmail();函数getMessages(){var expLabel = GmailApp.getUserLabelByName('Episode Expiration'); var threads = expLabel.getThreads(); for(var t = 0; t

PubSub再次运行,但如果开发人员想要记录对原始问题的响应,我会让此线程开放。谢谢,伙计们!

I wish I didn't have to repost this question, but my boss pushed this one up to high priority, and I need help to sort this out. I'm trying to use a GAS script to pull pub/sub notifications from an address on my GSuite Domain (currently, I'm testing on mine). Basically, I'm trying to accomplish what is described in all this material:

1)Great Github Project from Spencer Easton (Instructional Video)

2) pubsub API for GMAIL

3) Notification Help

4) Real-time notifications

5)Endpoint documentation

I have created the GAS project documented in reference 1 on my Work domain in Google Drive, published as draft to chrome store, added PUB/SUB API's and libraries, connected to Pub/Sub Cloud service with permissions to Gmail service account, and created the necessary topic/subscription. I've run enrollEmail and got back a valid history_Id that I can look up using the API explorer and confirmed the file can write to the specified Spreadsheet on Drive. But the doPost method never triggers an event to write on the spreadsheet like I hoped. Could someone help me find out why this code is not working on my domain? I don't have an explicit error message, because I am trying to run the post service.

here is my code:

function doPost(e) {
  var ss = SpreadsheetApp.open(DriveApp.getFilesByName('Episode_Log').next()).getSheets()[0];
  ss.appendRow(['Push was recieved'+ new Date()]); 
  try{
 var message = JSON.parse(e.postData.getDataAsString()).message;
    var data = Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString();
    ss.appendRow([new Date(), message.message_id, data]);
  }
  catch(e){ss.appendRow(['failure', e]); }
  
  return 200;
 }



function enrollEmail() {
  var EMAIL = Session.getActiveUser().toString();
  var watchRes = Gmail.newWatchRequest();
  watchRes.labelIds = ["INBOX"];
  watchRes.labelFilterAction = 'include';
  watchRes.topicName = 'projects/project-id-3596301251382091354/topics/eWarning';
  
  var response = Gmail.Users.watch(watchRes, EMAIL);
  
  Logger.log(response);
  
    var ss = SpreadsheetApp.open(DriveApp.getFilesByName('Episode_Log').next()).getSheets()[0];
 ss.appendRow(['Manual Test']); //This works 
}

function checkHistory(){
 var EMAIL = Session.getActiveUser().toString();
 Logger.log(Gmail.Users.History.list(EMAIL, {startHistoryId: '****'})); //works with a valid ID
}

Thank you guys so Much!

*PS I have not explicitly enabled billing on the google Cloud account. Does anyone know if I have to bill for this functionality?

解决方案

For anyone like me who needed to create a monitoring service in Google Appscript and needs something quick, I came up with a quick script to just check emails by a specific label. With the GmailApp, you can check messages by subject, too. Here is the code below:

var EMAILID = Session.getActiveUser().getEmail();
function getMessages() {
  var expLabel = GmailApp.getUserLabelByName('Episode Expiration');
  var threads = expLabel.getThreads();
  
  for(var t = 0; t < threads.length; t++)
  {
    var messages = threads[t].getMessages();
    for(var m = 0; m < messages.length; m++)
    {
      var email = {from: messages[m].getFrom().toString(),
                   body: messages[m].getPlainBody().toString(),
                   subject: messages[m].getSubject().toString()};
      
      Logger.log(email.subject.indexOf('Expiration'));
      if(/*(email.from.toString().includes('info@mydomain.com'))&&(*/email.subject.indexOf('Expiration') > 0)//)
      {
        var startInd = email.body.indexOf('Start')+12;
        var endInd = email.body.indexOf('End')+12;
        var pasInd = email.body.indexOf('Number')+8;
        var qualInd = email.subject.indexOf('Q');
        
        var qualco = email.subject.substring(qualInd, qualInd+17);
        var warning = email.body.substring(email.body.indexOf('Episode Expires in'),email.body.indexOf('days')+4);
        var startDate = email.body.substring(startInd, startInd+12);
        var endDate = email.body.substring(endInd , endInd+11);
        var priorAuthNumber = email.body.substring(pasInd , pasInd+13);
        
        var ss = SpreadsheetApp.openById('mysheetID').getSheets()[0];
        ss.appendRow([qualco, priorAuthNumber, startDate, endDate, warning]);  
      }
      messages[m].moveToTrash();
    }
  }
}

At this point, I don't actually need the PubSub to run anymore, but I'll leave this thread open if the developers want to document a response to the original problem. Thanks, guys!

这篇关于需要帮助为SpreadsheetApp(Google Appscript)创建GMAIL Pub / Sub Notification服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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