Β 
Znote (recipes)
Β Β Get Znote Β 

πŸ“© Build an AI Gmail Auto-Responder with Code

πŸš€ Automate responses to your tagged emails for faster, hands-free communication!

Β 

πŸ“© Build an AI Gmail Auto-Responder with Code

πŸš€ Automatically improve and finalize email drafts using AI.

✨ How It Works

In your Gmail inbox, add the label znote to the conversation you want to reply to. Then, create a new draft and provide specific instructions to help the AI compose an appropriate response.

Just follow these easy steps:

  • 🏷️ Add the znote label to any Gmail conversation you want help with.
  • πŸ“ Create a new draft in that thread with your reply instructions.
  • πŸ€– Run the last code block and the system will read threads labeled znote.
  • ⚑ It uses OpenAI to generate a better reply from your draft.
  • ✍️ The draft is updated with the improved response.
  • πŸ—‘οΈ The znote label is removed once it’s done.

πŸ› οΈ Prerequisites

  • πŸ“’ Follow and complete the Google API tutorial
  • πŸ“§ Finish the Gmail plugin tutorial
  • πŸ€– Complete the Ollama plugin tutorial

Then, before you start, make sure you have your OpenAI API key handy. You’ll need to store it securely as shown below.

1️⃣ Get Your OpenAI API Key

  • Log in to your OpenAI account.
  • Copy your API key (it usually starts with sk-).

2️⃣ Paste Your API Key

In the block below, replace the placeholder with your actual API key:

{
  "openai_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

⬆️ Paste your credentials above

3️⃣ Save Your Credentials

Run the following code to save your API key to a local JSON file:

const credentials = loadBlock('credentials');
_fs.writeFileSync(`${__dirname}/credentials-openai.json`, credentials);

⚠️ Tip: Keep your API key private and never share it publicly!


πŸš€ Automatically Polish Your Email Drafts with AI

This simple script scans your Gmail threads labeled "znote," improves the draft reply using OpenAI, and makes your emails clearer, friendlier, and more professional in just a few seconds.

//exec:node
const openai = require("./credentials-openai");
const API_KEY = openai.openai_api_key;
const MODEL = "gpt-4.1";

/**
 * Build the AI prompt
 */
function buildPrompt(threadMessages, draftContent) {
  const history = threadMessages
    .map(m => `From: ${m.From}\nSubject: ${m.Subject}\n\n${m.body}`)
    .join("\n\n---\n\n");

  return `
You are an assistant helping improve an email reply.

THREAD HISTORY:
${history}

CURRENT DRAFT:
${draftContent.body}

INSTRUCTIONS:
- Improve clarity and tone
- Fix grammar
- Keep it concise
- Keep original intent
- Return ONLY the improved email content.
`;
}

/**
 * MAIN
 */
const labelId = await getGmailLabelId("znote");
const threadIds = await getGmailThreadsWithLabel(labelId);

if (!threadIds.length) {
  print("βœ… No znote threads found.");
  
} else {

    for (const threadId of threadIds) {
    try {
        print(`πŸ”„ Processing thread ${threadId}`);

        const draft = await getGmailDraftForThread(threadId);
        if (!draft) {
        print("   ↳ No draft found. Skipping.");
        continue;
        }

        const threadMessages = await getGmailThreadMessages(threadId);
        const prompt = buildPrompt(threadMessages, draft.content);
        const improvedContent = await useOpenAIApi(
        null,
        API_KEY,
        MODEL,
        false,
        prompt
        );

        await updateGmailDraft(draft.id, draft.message, improvedContent);
        await removeGmailLabelFromThread(threadId, labelId);

        print("   βœ… Draft updated and label removed.");
    } catch (err) {
        print(`   ❌ Error processing thread ${threadId}`);
        print(err.message);
    }
  }

}

✨ It's amazing how much time you can save with a little bit of code magic! Plus, you can export and run this script outside Znote to make it fully autonomous.

Related recipes