π Automate responses to your tagged emails for faster, hands-free communication!
π Automatically improve and finalize email drafts using AI.
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:
Then, before you start, make sure you have your OpenAI API key handy. Youβll need to store it securely as shown below.
sk-).In the block below, replace the placeholder with your actual API key:
{
"openai_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
β¬οΈ Paste your credentials above
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!
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.