🚀 Apps Script + Gemini Mastery — Issue #5
https://github.com/lsvekis/Build-an-AI-Email-Assistant-for-Gmail
Build an AI Email Assistant for Gmail
Draft replies, summarize long threads, extract tasks, detect tone, and create a Gmail sidebar — all powered by Gemini.
⭐ What You Will Build in Issue #5
A complete AI Email Assistant for Gmail that can:
📩 Summarize long email threads
📝 Draft professional replies
📌 Extract tasks, deadlines, and follow-ups
💬 Identify tone (friendly, urgent, formal, etc.)
⚡ Rewrite messages for clarity or politeness
🎯 Suggest subject lines
🧠 Provide quick “reply templates” based on context
You’ll do this using:
- GmailApp service (safe, built-in)
- A sidebar UI added via a Gmail Add-on card
- A central Gemini analysis engine
- Extracted email metadata (from, to, subject, body)
- Context-aware prompting
🧠 Learning Objectives
After completing this issue, you will understand:
✔ How to build a Gmail Add-on with Apps Script
✔ How to access email threads + metadata
✔ How to send email context to Gemini safely
✔ How to create AI-powered drafts & summaries
✔ How to generate inline UI cards inside Gmail
✔ How to build reusable AI helpers
This is the core skillset behind AI-powered email assistants like Superhuman, Gemini for Gmail, or Microsoft Copilot — but built by you, inside Apps Script, using your own workflow.
🧩 EXERCISE — Build the Gmail AI Assistant
We will implement:
- Gmail Add-on manifest
- Entry point for opening the Add-on
- Functions to get thread + message content
- Gemini-powered actions
- UI cards inside Gmail
1️⃣ Update the Manifest for a Gmail Add-on
appsscript.json
{
"timeZone": "America/Toronto",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/script.external_request"
],
"addOns": {
"gmail": {
"name": "AI Email Assistant",
"logoUrl": "https://www.gstatic.com/images/icons/material/system/2x/mail_outline_black_48dp.png",
"contextualTriggers": [{
"unconditional": {},
"onTriggerFunction": "onGmailMessageOpen"
}],
"primaryColor": "#3F51B5",
"secondaryColor": "#FFFFFF"
}
}
}
2️⃣ Create the Add-on Trigger
Code.gs
function onGmailMessageOpen(e) {
return buildMainCard(e);
}
3️⃣ Build the Gmail Add-on Card UI
UI.gs
function buildMainCard(e) {
const message = getEmailContent(e);
const card = CardService.newCardBuilder();
card.setHeader(
CardService.newCardHeader()
.setTitle("AI Email Assistant")
.setSubtitle("Powered by Gemini")
);
// Summarize button
const summarizeAction = CardService.newAction()
.setFunctionName("actionSummarize")
.setParameters({ emailBody: message.body });
card.addSection(
CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText("<b>What would you like to do?</b>"))
.addWidget(CardService.newTextButton()
.setText("Summarize Email")
.setOnClickAction(summarizeAction)
)
);
// Draft reply button
const replyAction = CardService.newAction()
.setFunctionName("actionDraftReply")
.setParameters({ emailBody: message.body });
card.addSection(
CardService.newCardSection()
.addWidget(CardService.newTextButton()
.setText("Draft a Reply")
.setOnClickAction(replyAction)
)
);
// Extract tasks
const tasksAction = CardService.newAction()
.setFunctionName("actionExtractTasks")
.setParameters({ emailBody: message.body });
card.addSection(
CardService.newCardSection()
.addWidget(CardService.newTextButton()
.setText("Extract Tasks & Deadlines")
.setOnClickAction(tasksAction)
)
);
return card.build();
}
4️⃣ Email Extraction Helper
EmailHelpers.gs
function getEmailContent(e) {
const messageId = e.gmail.messageId;
const message = GmailApp.getMessageById(messageId);
return {
from: message.getFrom(),
to: message.getTo(),
subject: message.getSubject(),
body: message.getPlainBody()
};
}
5️⃣ Gemini-Powered Actions
Actions.gs
function actionSummarize(e) {
const body = e.emailBody;
const prompt = `
Summarize the following email. Provide:
- A short summary
- Key points
- Action items (if any)
`;
return createResultCard(callGemini(prompt, body));
}
function actionDraftReply(e) {
const body = e.emailBody;
const prompt = `
Draft a professional email reply to the following message.
Maintain clarity, politeness, and a helpful tone.
`;
return createResultCard(callGemini(prompt, body));
}
function actionExtractTasks(e) {
const body = e.emailBody;
const prompt = `
Extract all tasks, deadlines, commitments, and follow-up actions
from the email below. Output as a task list.
`;
return createResultCard(callGemini(prompt, body));
}
6️⃣ Common Result Card Generator
UIResult.gs
function createResultCard(resultText) {
const card = CardService.newCardBuilder();
card.setHeader(
CardService.newCardHeader().setTitle("AI Assistant Result")
);
card.addSection(
CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText(resultText))
);
return card.build();
}
7️⃣ Reuse Your Existing Gemini Helper
GeminiHelpers.gs
function callGemini(prompt, text) {
const url = `https://generativelanguage.googleapis.com/v1/models/${GEMINI_MODEL}:generateContent?key=${GEMINI_API_KEY}`;
const payload = {
contents: [{ parts: [{ text: `${prompt}\n\n${text}` }] }]
};
const res = UrlFetchApp.fetch(url, {
method: "post",
contentType: "application/json",
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
const json = JSON.parse(res.getContentText());
return json.candidates?.[0]?.content?.parts?.[0]?.text || "No response from Gemini.";
}
🔬 Testing Instructions
- Install the Add-on as an unpublished Gmail Add-on
- Open any email
- Look for the AI Email Assistant icon
- Test:
- “Summarize Email”
- “Draft a Reply”
- “Extract Tasks”
- Verify Gemini generates results smoothly
🎉 What You Built in Issue #5
You now have:
✔ A fully functional Gmail side panel
✔ One-click AI-generated summaries
✔ Automatic draft replies
✔ Task extraction from long threads
✔ Reusable AI-powered components
✔ A foundation for a full Gmail AI copilot
This is a huge step — you’re now building the exact type of workflow automation companies pay thousands to implement.
🔥 Coming Next: Issue #6 — Gemini-Powered Slide Creator for Google Slides
You’ll build an AI tool that:
- Creates presentations automatically
- Generates slide structure, titles, bullets
- Inserts content into a Slide deck
- Builds entire presentations from a topic