Need Help creating GMAIL Pub/Sub Notification serv

2019-04-13 08:54发布

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?

1条回答
你好瞎i
2楼-- · 2019-04-13 09:20

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!

查看更多
登录 后发表回答