Build an AI Newsletter Digest System Without Coding
If more newsletters land in your inbox than you can read, this workflow helps you keep up. You don’t need to know how to code to set it up.
It walks you through a script that pulls emails out of Gmail and copies them into a Google Sheet. An AI assistant can then read that sheet, so you can ask it for things like a brief on the latest news every morning.
Pro tip: connect the assistant to Slack and you can pass the important bits on to your team without copying and pasting.
Newsletters are only one example. With AI’s help, you can have Google Apps Script pull almost anything you want out of your inbox.
The Google Apps Script
You’ll learn more if you have AI write the script for you, but you’re welcome to copy and paste this one.
function exportNewsletters() {
// Open the spreadsheet and sheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("Newsletter Exports");
if (!sheet) {
sheet = spreadsheet.insertSheet("Newsletter Exports");
sheet.appendRow(["Email ID", "Email Date", "From", "Subject", "Email Contents"]);
// Format the header row
sheet.getRange(1, 1, 1, 5).setFontWeight("bold");
// Set column widths (adjust as needed)
sheet.setColumnWidths(1, 5, 150);
// Format the date column
sheet.getRange("B:B").setNumberFormat("yyyy-mm-dd hh:mm:ss");
}
// Get existing email IDs to avoid duplicates
var existingIds = [];
var lastRow = sheet.getLastRow();
if (lastRow > 1) { // Check if there's data beyond the header row
existingIds = sheet.getRange(2, 1, lastRow - 1, 1).getValues().flat();
}
// Search for emails with the "newsletter" label
var threads = GmailApp.search('label:newsletter', 0, 30);
var newData = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var message = messages[messages.length - 1]; // Get the last message in the thread
var emailId = message.getId();
if (!existingIds.includes(emailId)) {
var date = message.getDate();
var from = message.getFrom();
var subject = message.getSubject();
var content = message.getPlainBody();
newData.push([emailId, date, from, subject, content]);
}
}
// If we have new data, add it to the top of the sheet
if (newData.length > 0) {
// Sort new data by date, most recent first
newData.sort((a, b) => b[1] - a[1]);
// Insert new rows at the top and set values
sheet.insertRowsAfter(1, newData.length);
var newRange = sheet.getRange(2, 1, newData.length, 5);
newRange.setValues(newData);
// Set text wrapping to CLIP for all cells
newRange.setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);
// Set a standard row height (adjust the value as needed)
sheet.setRowHeights(2, newData.length, 21); // 21 is the default row height
}
}
function createHourlyTrigger() {
// Delete existing triggers
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() === "exportNewsletters") {
ScriptApp.deleteTrigger(triggers[i]);
}
}
// Create a new trigger to run every hour
ScriptApp.newTrigger('exportNewsletters')
.timeBased()
.everyHours(1)
.create();
}
The elvex Assistant Configuration
Name
AI News
Description
This assistant reads the latest AI newsletters and answers questions about them.
Rules
Before you do any analysis:
- List every connected data source and file.
- Look at any connected spreadsheets to see what data they hold.
- Work out which queries you need to run to answer the user.
After that, follow this advice:
-
Use data analysis on every request
-
In the spreadsheet, Column C holds the “From” address of each newsletter and Column E holds the “Email Contents”. Use Column C to tell who sent the news and Column E to find the news itself
-
Format each bullet in your answer like this: [“name” Newsletter it’s from], [“summary” the specific information you found], [“link” the word Link with the hyperlink connected to the specific information, if there is one. If there isn’t, ignore linking]
-
Name names when the newsletter does. For example, instead of “An AI company released a new model,” say “OpenAI released the new model ChatGPT 4o”
-
Always list the attached files and look through them before you decide which query to run.
-
To filter down to a single day, use
datetrunc. i.e.WHERE datetrunc('day', "Email Date") = '2024-12-04' -
A query that selects the day’s relevant news looks like this:
SELECT "From", "Subject", "Email Contents" FROM newsletters WHERE datetrunc('day', "Email Date") = '2024-12-04' ORDER BY "Email Date" DESC