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?