Skip to content

Instantly share code, notes, and snippets.

@dettmering
Created November 10, 2024 11:20
Show Gist options
  • Save dettmering/c040b4c0e0bb2d7995fd2f4cc1f1c5e0 to your computer and use it in GitHub Desktop.
Save dettmering/c040b4c0e0bb2d7995fd2f4cc1f1c5e0 to your computer and use it in GitHub Desktop.
export default {
async email(message, env, ctx) {
switch (message.to) {
case "[email protected]":
let subject = message.headers.get('subject');
const todoist_project = 1234;
// Get the email body text
const emailText = await new Response(message.raw).text();
// Call OpenAI API to get a summary of the email text
const openai_summary = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + env.OPENAI_KEY,
},
body: JSON.stringify({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: "Du bist ein Assistent, der mir hilft, E-Mail Inhalte kurz und knapp als eine Beschreibung für ein Todoist TODO zusammenzufassen. Antworte einzig und allein mit der Zusammenfassung und schreibe nichts sonst. Übersetzte nicht-deutsche Texte ins Deutsche."
},
{
role: "user",
content: `Fasse die folgende E-Mail in maximal 5 Stichpunkten zusammen. Füge ebenfalls als Stichpunkte die in der E-Mail Konversation beteiligten Sender und Empfänger sowie eventuelle Deadlines zu den Stichpunkten hinzu. Gib das Ergebnis formattiert als Mardown aus: Betreff der E-Mail ${subject}; Inhalt der E-Mail. ${emailText}`
}
]
})
});
const summaryData = await openai_summary.json();
const summary = summaryData.choices[0].message.content.trim();
// Call OpenAI API to get a summary of the email text
const openai_todo = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + env.OPENAI_KEY,
},
body: JSON.stringify({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: "Du bist ein Assistent, der mir hilft, E-Mail Inhalte kurz und knapp als eine ein Todoist TODO zusammenzufassen. Antworte einzig und allein mit dem TODO und schreibe nichts sonst. Das Wort TODO sollte nicht in deiner Ausgabe vorkommen. Übersetze nicht-Deutsche Texte auf Deutsch."
},
{
role: "user",
content: `Fasse die folgende E-Mail zusammen als einen Satz, der sich als Todo eignet. Betreff der E-Mail ${subject}; Inhalt der E-Mail. ${emailText}`
}
]
})
});
const todoData = await openai_todo.json();
const todo = todoData.choices[0].message.content.trim();
const requestBody = JSON.stringify({
content: todo, // Combine cleaned subject with summary
description: summary,
due_string: "today",
due_lang: "en",
project_id: todoist_project,
labels: ["E-Mail"]
});
const requestOptions = {
method: "POST",
headers: {
"Authorization": "Bearer " + env.TODOIST_KEY,
"Content-Type": "application/json"
},
body: requestBody
};
// Send the request to Todoist API to create a new task
ctx.waitUntil(fetch("https://api.todoist.com/rest/v2/tasks", requestOptions));
break;
default:
message.setReject("Unknown address");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment