README: Email Digest with Gmail, OpenAI, and Slack

This is a great starter function that will ask you to set up a basic trigger, use Zapier actions, leverage packages, and secrets!

This function will summarize your important emails using OpenAI and send a daily digest to Slack. To set it up in your environment, follow the instructions below.

Prerequisites:

  • Gmail Account (can be updated to an alternative email client)
  • Slack Account (can be updated to an alternative chat/notification tool)
  • An OpenAI account and API key

TODOs:

  • Create a new function at functions.zapier.com

  • Create a Schedule by Zapier trigger that triggers daily at 5pm

  • Copy the main.py file contents below and paste it into your new function's main.py file

  • Create another code file and name it llm_config. Copy the contents of the llm_config below and paste it into the new file. 4.Add the openai package to the packages tab in the right side panel of your new function.

  • Add your OpenAI API key as a secret called OPENAI_API_KEY to the secrets tab in the right side panel of your new function.

  • Update the Zapier actions in your main.py file. Search for TODOs to identify where this is necessary. Note: When you add your account_id functions may duplicate the params. Delete the new params & fill out the existing params.

  • Update your location in your llm_config.py file.

  • Test by pushing 'Run Function' and when you are ready, deploy!

Code Files

main.py file

import os
from openai import OpenAI
from llm_config import system_prompt

client = OpenAI()
OpenAI.api_key = os.environ["OPENAI_API_KEY"]

# TODO: Type CNTRL + Space to select your account id 
emails = zapier.action.GoogleMailV2CLIAPI__at__1__1__2.message(
  account_id=, # Type CTRL + Space to select your account  
  params={
      "query":"is:unread newer_than:1d is:important",  # Search String
      # "_zap_search_success_on_miss":"False",  
  },
  type_of="search",
)
 
summaries = []

# We summarize all e-mails individually because GPT doesn't have enough
# tokens to do a day's worth of emails at once.
for email in emails:
  summarise_response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
      {"role": "system", "content": system_prompt},
      {"role": "user", "content": email["body_plain"]}
    ],
    temperature= 1
  )

  sender_name = email["from"]["name"].strip().replace('"', "")
  summary_text = summarise_response.choices[0].message.content.strip()
  
  summary = f"""*{sender_name}*:
  {summary_text}"""
  
  summaries.append(summary)

all_emails_summarised = "\n\n".join(summaries)  

result=f"""Hey!

Here's your daily email summary:

{all_emails_summarised}"""


# TODO: Type CNTRL + Space to select your account id and username (channel field)
zapier.action.SlackAPI.direct_message(
  account_id=, # Type CTRL + Space to select your account  
  params={
      # "send_multi":"no",  # Send Multi Message | Valid inputs: "no", "yes"
      "channel":, # To Username
      "text":result,  # Message Text
      # "as_bot":"yes",  # Send as a bot? | Valid inputs: "no", "yes"
      # "username":"",  # Bot Name
      # "icon":"",  # Bot Icon
      # "add_edit_link":"yes",  # Include a link to this Zap? | Valid inputs: "no", "yes"
      # "image_url":"",  # Attach Image by URL
      "unfurl":"no",  # Auto-Expand Links? | Valid inputs: "no", "yes"
      # "link_names":"yes",  # Link Usernames and Channel Names? | Valid inputs: "no", "yes"
      # "post_at":"",  # Schedule At
  },
  type_of="write",
)

llm_config.py file

my_location = "" # TODO: Add your locations (e.g. New York, New York USA)

system_prompt = f"""You are an AI assistant tasked with summarizing emails 
into clear, concise, and actionable bullet points. Your goal is to extract 
the most relevant information from each email and present it as well-defined 
actions or key takeaways. Ensure that:

	1.	Each bullet point is short, direct, and actionable.
	2.	Important dates, deadlines, tasks, and requests are emphasized.
	3.	Any follow-up actions or required next steps are clearly identified.
	4.	Unnecessary details and pleasantries are omitted.
	5.	Each bullet MUST use the • symbol as its list item!
	6.	Any indented bullets also use • and are indented using 4 spaces 
	relative to the parent bullet point.
	7.	Any dates and times MUST be reformatted to use the correct timezone 
	for {my_location}.
	8.	Messages MUST use Slack's syntax. Link example: <https://google.com|Google>, 
	bold text example: *this text is bold*. If the email has its own formatting, replace 
	it with Slack formatting. That means no **bold** but instead *bold*!

Example:

Email:
"Hi team,
Just a reminder that we need the final budget report by Friday, September 15th. 
Also, please review the latest project proposal and send your feedback by Wednesday. 
Let's plan a meeting next Monday to discuss.
Best,
Alex"

Summary:

	•	Submit the final budget report by Friday, September 15th.
	•	Review the project proposal and send feedback by Wednesday.
	•	Schedule a meeting for next Monday to discuss the report.

Make sure the AI focuses on clarity, brevity, and actionable items!"""