Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - Google Apps脚本-如何阻止新邮件进入旧线程?(Google Apps Script - How to stop new messages from going into old threads?)

I have a Google Apps script that parses through threads with a label "incomming", fires them off to a webhook, and then marks them as "sent".

(我有一个Google Apps脚本,该脚本通过带有“ incomming”标签的线程进行解析,将它们触发到Webhook,然后将其标记为“ sent”。)

    function send() {
    var label = GmailApp.getUserLabelByName('incomming');
    var sentlabel = GmailApp.getUserLabelByName('sent');
    var messages = [];
    var threads = label.getThreads();

    for (var i = 0; i < threads.length; i++) {
        messages = messages.concat(threads[i].getMessages())
    }

    for (var i = 0; i < messages.length; i++) {
        var message = messages[i];
        Logger.log(message);

        var payload = {
            "from": message.getFrom(),
            "to": message.getTo(),
            "cc": message.getCc(),
            "date": message.getDate(),
            "subject": message.getSubject(),
            "txtbody": message.getPlainBody()
        };

        var options = {
            'method': 'post',
            'contentType': 'application/json',
            'payload': JSON.stringify(payload),
        };

        var webhookUrl = '<redacted>';
        UrlFetchApp.fetch(webhookUrl, options);
    }

    // remove the label from these threads so we don't send them to
    // slack again next time the script is run
    label.removeFromThreads(threads);
    Utilities.sleep(200);
    sentlabel.addToThreads(threads);
    for (var i = 0; i < threads.length; i++) {
        threads[i].moveToArchive();
    }
}

The problem is, because these emails are generated by a service, they have similar subjects - sometimes the same subject (I can't change this in the service)

(问题是,因为这些电子邮件是由服务生成的,所以它们具有相似的主题-有时是相同的主题(我无法在服务中更改此主题))

Lets say the subject is "Update on X1"

(可以说主题是“在X1上更新”)

When an email comes in for "Update on X1" - the script works fine, sends it off no worries.

(当收到一封有关“在X1上更新”的电子邮件时-该脚本可以正常工作,不用担心,将其发送出去。)

But, if a new "Update on X1" comes in, it sends both the new one and the old one since Gmail labels the whole thread.

(但是,如果有新的“ X1更新”出现,则它将发送新邮件和旧邮件,因为Gmail会标记整个线程。)

This results in heavy duplication.

(这导致大量重复。)

How can I fix this?

(我怎样才能解决这个问题?)

  ask by NictraSavios translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'd like to shed some light into Thread labels on Gmail.

(我想对Gmail上的“线程”标签有所了解。)

As of now (and for the perceivable future), a thread will have all the labels from it's messages.

(截至目前(以及可预见的未来),线程将具有其消息中的所有标签。)

This is still helpful to you, as you can quickly isolate which Threads contain messages with the "incoming" label.

(这仍然对您有帮助,因为您可以快速隔离哪些线程包含带有“传入”标签的消息。)

By having this sub-list of messages, you can then process the contained messages with thread.getMessages() .

(通过具有消息的子列表,您可以使用thread.getMessages()处理包含的消息。)

Then you can use the Advanced Google services for Gmail in order to call the Users.messages.get to retrieve the Message representation, including it's list of labels labelIds[] .

(然后,您可以使用Gmail的高级Google服务 ,以便调用Users.messages.get来检索Message表示形式,包括其标签labelIds[]的列表。)

You can then manipulate each message and add the labels to them when your messages are sent.

(然后,您可以操纵每条消息,并在发送消息时为它们添加标签。)


Another solution would be to have some form of storage where you store the already processed messageId s, then check for those before sending them to you webhook.

(另一种解决方案是采用某种存储形式,在其中存储已处理的messageId ,然后在将其发送到您的webhook之前检查它们。)

You can do this with a JDBC connection on Apps Script, Firebase Realtime Database or even your own DIY Google Docs solution.

(您可以通过Apps脚本, Firebase Realtime Database甚至您自己的DIY Google Docs解决方案上的JDBC连接进行此操作。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...