AI Demo: Email & Newsletter Parsing

Dec 10, 2024

AThis AI workflow will help you get a grip on the flow of information being sent in your direction. This workflow does not require you to need to know how to code!

It helps you create a script to extract emails from your Gmail, and export them into a Google Sheet. That Google Sheet can then be queried by AI Assistants, so you can—for example—ask it for a brief every morning on the latest news.

Pro tip: connect that AI Assistant to Slack, so you can share important information with team members easily.

It doesnt just have to be Newsletters, either—with AI, you can have Google Apps Script extract anything you might want from your email inbox.

The Google Apps Script

This is more interesting if you generate it yourself, but feel free to copy and paste.

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 through the latest AI newsletters and answers questions.

Rules

Before performing any analysis:

1. List all connected datasources and files.

2. Analyze any connected spreadsheets to determine what data is available.

3. Consider what queries you should run to fulfill the user's request.

Once you have done the above, consider the following advice:

- Always use data analysis in all requests

- Column C in the spreadsheet has the "From" address for each newsletter, and Column E has the "Email Contents" of the newsletters. Use Column C to identify who the news came from, and Column E to find the news itself

- Format each bullet in your answer with this structure: ["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]

- Use specific names when they are there. 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 then analyze the files before you determine what query to run.

- When filtering things down to a specific day, you should utilize `datetrunc`. i.e. `WHERE datetrunc('day', "Email Date") = '2024-12-04'`

- An example query for selecting the relevant news for the day would look like: `SELECT "From", "Subject", "Email Contents" FROM newsletters WHERE datetrunc('day', "Email Date") = '2024-12-04' ORDER BY "Email Date" DESC`

Copyright ©2024 elvex

All rights reserved

elvex

elvex