我希望我不必重新发布这个问题,但我的老板将这个问题推到了高优先级,我需要帮助来解决这个问题。我正在尝试使用 GAS 脚本从我的 GSuite 域上的地址提取发布/订阅通知(目前,我正在我的域上进行测试)。基本上,我正在尝试完成所有这些材料中描述的内容:
1)来自 Spencer Easton 的伟大的 Github 项目 https://github.com/Spencer-Easton/Apps-Script-Gmail-Push-Notifications-v2
2) GMAIL 的 pubsub API https://stackoverflow.com/questions/40099785/generic3-push-into-an-endpoint-url-pubsub-api-for-gmail
3) 通知帮助 https://stackoverflow.com/questions/42017690/i-want-to-get-new-gmail-notify-by-google-cloud-pub-sub-api
4) 实时通知 https://stackoverflow.com/questions/30733806/how-to-real-time-monitor-the-emails/30735808#30735808
5)端点文档 https://stackoverflow.com/questions/30584533/unable-to-create-subscription-with-a-push-end-point-on-app-script-script-google/30598935#30598935
我已经在 Google Drive 中的工作域上创建了参考 1 中记录的 GAS 项目,作为草稿发布到 chrome 商店,添加了 PUB/SUB API 和库,连接到具有 Gmail 服务帐户权限的 Pub/Sub 云服务,并创建了必要的主题/订阅。我已经运行 enrollEmail 并返回了一个有效的 History_Id,我可以使用 API 资源管理器查找该 ID,并确认该文件可以写入云端硬盘上的指定电子表格。但 doPost 方法永远不会像我希望的那样触发在电子表格上写入的事件。有人可以帮我找出为什么此代码在我的域上不起作用吗?我没有明确的错误消息,因为我正在尝试运行邮政服务。
这是我的代码:
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
}
非常感谢你们!
*PS 我还没有明确在谷歌云帐户上启用计费。有谁知道我是否需要为此功能付费?
对于像我这样需要在 Google Appscript 中创建监控服务并且需要快速操作的人,我想出了一个快速脚本来仅按特定标签检查电子邮件。使用 GmailApp,您还可以按主题检查邮件。下面是代码:
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('[email protected] /cdn-cgi/l/email-protection'))&&(*/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();
}
}
}
此时,我实际上不再需要 PubSub 运行,但如果开发人员想要记录对原始问题的响应,我将保持此线程打开。多谢你们!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)