Reads every document that arrives by email, logs a short summary of each in a Google Doc and emails you what came in.
Uses
Gmail
Google Docs
OpenAI
Summarise the documents people email you
documentSearch to blank.Set documentSearch to blank.logDocumentUrl to blank.Set logDocumentUrl to blank.notifyEmail to blank.Set notifyEmail to blank.processedLabel to Document summaries.Set processedLabel to Document summaries.maxDocumentsPerRun to 5.Set maxDocumentsPerRun to 5.documentCap to maxDocumentsPerRun multiplied by 1.Set documentCap to maxDocumentsPerRun multiplied by 1.documentCap is greater than 25If documentCap is greater than 25Yes
No
No steps
documentCap is less than 1If documentCap is less than 1Yes
No
No steps
logDocumentId to blank.Set logDocumentId to blank.logDocumentUrl contains /d/ (ignoring case)If logDocumentUrl contains /d/ (ignoring case)Yes
logDocumentUrl split by /d/ split by / and save it as logDocumentId.Trim the first item of the last item of logDocumentUrl split by /d/ split by / and save it as logDocumentId.No
No steps
logDocumentId is emptyIf logDocumentId is emptyYes
No
No steps
@processedLabel'. Save it as found.List emails matching '@documentSearch has:attachment -label:@processedLabel'. Save it as found.summarisedCount to 0.Set summarisedCount to 0.digestLines to an empty list.Set digestLines to an empty list.message in found.emails:For each message in found.emails:summarisedCount is greater than or equal to documentCapIf summarisedCount is greater than or equal to documentCapYes
No
No steps
messageErrorIf a step below fails, save the error as messageErrorTry
messageId to blank.Set messageId to blank.idErrorIf a step below fails, save the error as idErrorTry
message.id and save it as messageId.Trim message.id and save it as messageId.On error
No steps
messageId is emptyIf messageId is emptyYes
Skipped a message Gmail returned without an id..Show in the output: Skipped a message Gmail returned without an id..No
No steps
rawFrom to blank.Set rawFrom to blank.fromErrorIf a step below fails, save the error as fromErrorTry
message.from and save it as rawFrom.Trim message.from and save it as rawFrom.On error
No steps
senderAddress to rawFrom.Set senderAddress to rawFrom.rawFrom contains < (ignoring case)If rawFrom contains < (ignoring case)Yes
rawFrom split by < split by > and save it as senderAddress.Trim the first item of the last item of rawFrom split by < split by > and save it as senderAddress.No
No steps
senderAddress doesn't contain @ (ignoring case)If senderAddress doesn't contain @ (ignoring case)Yes
rawFrom.Show in the output: Skipped a message whose sender could not be read: rawFrom.No
No steps
subject to (no subject).Set subject to (no subject).subjectErrorIf a step below fails, save the error as subjectErrorTry
message.subject and save it as subjectText.Trim message.subject and save it as subjectText.subjectText isn't emptyIf subjectText isn't emptyYes
subject to subjectText.Change subject to subjectText.No
No steps
On error
No steps
latestId to blank.Set latestId to blank.latestErrorIf a step below fails, save the error as latestErrorTry
senderAddress'. Save it as latestFromSender.Find email matching 'from:senderAddress'. Save it as latestFromSender.latestFromSender.email.id and save it as latestId.Trim latestFromSender.email.id and save it as latestId.On error
No steps
latestId is not messageIdIf latestId is not messageIdYes
senderAddress. Only a sender's newest message in the inbox can be opened, and a newer one has arrived - forward this message to yourself to have it read.Show in the output: Left alone: '@subject' from senderAddress. Only a sender's newest message in the inbox can be opened, and a newer one has arrived - forward this message to yourself to have it read.No
No steps
senderAddress. Save it as downloaded.Download the attachments on the latest email from senderAddress. Save it as downloaded.messageDocuments to 0.Set messageDocuments to 0.attachment in downloaded.documents:For each attachment in downloaded.documents:summarisedCount is greater than or equal to documentCap If summarisedCount is greater than or equal to documentCapYes
No
No steps
fileName to blank. Set fileName to blank.nameError If a step below fails, save the error as nameErrorTry
attachment.filename and save it as fileName.Trim attachment.filename and save it as fileName.On error
No steps
attachmentLabel to (unnamed attachment). Set attachmentLabel to (unnamed attachment).fileName isn't empty If fileName isn't emptyYes
attachmentLabel to fileName.Change attachmentLabel to fileName.No
No steps
fileName in lowercase doesn't end with .pdf (ignoring case) If fileName in lowercase doesn't end with .pdf (ignoring case)Yes
attachmentLabel: only PDF attachments are treated as documents.Show in the output: Skipped attachmentLabel: only PDF attachments are treated as documents.No
No steps
documentRef to blank. Set documentRef to blank.refError If a step below fails, save the error as refErrorTry
attachment.document_ref and save it as documentRef.Trim attachment.document_ref and save it as documentRef.On error
No steps
documentRef is empty If documentRef is emptyYes
attachmentLabel; it was left alone.Show in the output: Could not open attachmentLabel; it was left alone.No
No steps
detailLines to an empty list. Set detailLines to an empty list.documentKind to not stated in the document. Set documentKind to not stated in the document.readError If a step below fails, save the error as readErrorTry
reading.Run an AI agent: Read the attached document and pull out each of these fields, in this order: - What kind… and save it as reading.answer in reading.output.fields:For each answer in reading.output.fields:answerName to blank. Set answerName to blank.answerNameError If a step below fails, save the error as answerNameErrorTry
answer.name and save it as answerName.Trim answer.name and save it as answerName.On error
No steps
answerValue to blank. Set answerValue to blank.answerValueError If a step below fails, save the error as answerValueErrorTry
answer.value and save it as answerValue.Trim answer.value and save it as answerValue.On error
answerListErrorIf a step below fails, save the error as answerListErrorTry
answer.value with , and save it as answerValue.Join answer.value with , and save it as answerValue.On error
No steps
answerValue is empty If answerValue is emptyYes
answerValue to not stated in the document.Change answerValue to not stated in the document.Else if @answerName is "What kind of document this is"
documentKind to answerValue.Change documentKind to answerValue.Otherwise
No steps
answerName: answerValue truncated to 300 characters using … as the ellipsis to the end of detailLines and save it as detailLines. Add answerName: answerValue truncated to 300 characters using … as the ellipsis to the end of detailLines and save it as detailLines.On error
No steps
detailLines is empty If detailLines is emptyYes
attachmentLabel; nothing was written to the log and the mail was left alone.Show in the output: Could not read attachmentLabel; nothing was written to the log and the mail was left alone.No
No steps
summaryBrief to: Set summaryBrief to:Write a short plain summary of a document for the person who received it.
These are the only details that were read out of the document. Use nothing but these. Do not add anything, do not work anything out, and do not guess. Where a detail says it is not stated in the document, either say so plainly or leave it out - never fill it in.summaryRules to: Set summaryRules to:Write at most three sentences, describing only what the document says. Do not give advice, do not say what should be done about it, and do not comment on whether it is good, bad, risky or urgent.summaryText to blank. Set summaryText to blank.summaryError If a step below fails, save the error as summaryErrorTry
detailLines joined by a new line plus summaryRules'. Save it as written.Ask OpenAI: '@summaryBrief plus detailLines joined by a new line plus summaryRules'. Save it as written.written.response_text and save it as summaryText.Trim written.response_text and save it as summaryText.On error
No steps
summaryText is empty If summaryText is emptyYes
detailLines with a new line and save it as summaryText.Join detailLines with a new line and save it as summaryText.attachmentLabel; the log has the document's own answers instead.Show in the output: No summary could be written for attachmentLabel; the log has the document's own answers instead.No
No steps
entryHeading to attachmentLabel - from senderAddress - the current date and time. Set entryHeading to attachmentLabel - from senderAddress - the current date and time.entryText to Set entryText to
entryHeading
summaryText
.logDocumentId. Add text to logDocumentId.entryTextsummarisedCount. Add 1 to summarisedCount.messageDocuments. Add 1 to messageDocuments.attachmentLabel from senderAddress - documentKind to the end of digestLines and save it as digestLines. Add attachmentLabel from senderAddress - documentKind to the end of digestLines and save it as digestLines.processedLabel to email messageId.Add label processedLabel to email messageId.messageDocuments is 0If messageDocuments is 0Yes
No
No steps
On error
messageError as text.Show in the output: Could not read one document email: messageError as text.summarisedCount is greater than 0If summarisedCount is greater than 0Yes
digestBody to digestLines joined by a new lineSet digestBody to digestLines joined by a new line
The summaries are in your log: https://docs.google.com/document/d/@logDocumentId/edit.notifyEmail.Send an email to notifyEmail.
Subject: Documents read from your mail: summarisedCountdigestBodyNo
No new documents arrived to summarise..Show in the output: No new documents arrived to summarise..