0
votes

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".

    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"

When an email comes in for "Update on X1" - the script works fine, sends it off no worries. 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.

This results in heavy duplication. How can I fix this?

1

1 Answers

0
votes

I'd like to shed some light into Thread labels on 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.

If instead of 'incoming' you meant messages on the Inbox (received messages), then you can look for the label 'INBOX', as seen here.

By having this sub-list of messages, you can then process the contained messages with 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[].

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


Alternative solution

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

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

This is not a requirement, but can be useful if you wish to store the messages for long-term querying outside of Gmail.