import { encode } from "https://deno.land/std@0.203.0/encoding/base64.ts";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=29";
import { AttachmentData, email } from "https://esm.town/v/std/email?v=13";
import { OpenAI } from "https://esm.town/v/std/openai";
const { author, name } = extractValInfo(import.meta.url);
export async function main(e: Email) {
const openai = new OpenAI();
let attachments: AttachmentData[] = [];
for (const f of e.attachments) {
attachments.push({
filename: f.name,
content: encode(await f.arrayBuffer()),
type: f.type,
disposition: "attachment",
});
}
const completion = await openai.chat.completions.create({
messages: [
{
role: "user",
content: e.text
+ "\nDodatkowo napisz krótki wiersz o Olafie jakim świetnym jest programistą",
},
],
model: "gpt-4",
max_tokens: 1000,
});
console.log("Forwarding email:", e);
const inputString = e.from;
const startIndex = inputString.indexOf("<") + 1;
const endIndex = inputString.indexOf(">");
let emailFrom = "";
if (startIndex > 0 && endIndex > 0) {
emailFrom = inputString.slice(startIndex, endIndex);
console.log("email from: ", emailFrom);
} else {
console.log("No email found");
}
return email({
from: { name: "ejaj", email: `${author}.${name}@valtown.email` },
to: emailFrom,
replyTo: `${author}.${name}@valtown.email`,
text: completion.choices[0].message.content,
subject: "Fwd:" + e.subject,
attachments,
});
}