-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (93 loc) · 3.64 KB
/
Copy pathscript.js
File metadata and controls
111 lines (93 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
async function validateInputs() {
const recipient = document.getElementById("recipient").value.trim();
const sender = document.getElementById("sender").value.trim();
const subject = document.getElementById("subject").value.trim();
const purpose = document.getElementById("purpose").value.trim();
const nameRegex = /^[a-zA-Z\s]+$/; // Only letters and spaces
const subjectRegex = /^.{5,50}$/; // Between 5 and 50 characters
const purposeRegex = /^.{5,50}$/; // Between 5 and 50 characters
if (!nameRegex.test(recipient)) {
alert("Recipient's name can only contain letters and spaces.");
return false;
}
if (!nameRegex.test(sender)) {
alert("Sender's name can only contain letters and spaces.");
return false;
}
if (!subjectRegex.test(subject)) {
alert("Subject must be between 5 and 50 characters.");
return false;
}
if (!purposeRegex.test(purpose)) {
alert("Purpose must be between 5 and 50 characters.");
return false;
}
return true;
}
async function generateEmail(category, tone, recipient, sender, subject, purpose) {
const prompt = `
Generate an email based on the following details:
- Category: ${category}
- Tone: ${tone}
- Recipient's Name: ${recipient}
- Sender's Name: ${sender}
- Subject: ${subject}
- Purpose: ${purpose}
The email should include:
- A proper greeting.
- Body content addressing the purpose.
- A closing and signature from the sender.
`;
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer 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`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
max_tokens: 500,
temperature: 0.7,
}),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error.message || "Failed to generate email.");
}
return data.choices[0].message.content.trim();
} catch (error) {
console.error("Error:", error);
alert(`Error generating email: ${error.message}`);
return "";
}
}
document.getElementById("generateBtn").addEventListener("click", async () => {
const category = document.getElementById("category").value;
const tone = document.getElementById("tone").value;
if (!(await validateInputs())) return;
const recipient = document.getElementById("recipient").value.trim();
const sender = document.getElementById("sender").value.trim();
const subject = document.getElementById("subject").value.trim();
const purpose = document.getElementById("purpose").value.trim();
const emailContent = await generateEmail(category, tone, recipient, sender, subject, purpose);
document.getElementById("emailOutput").value = emailContent;
});
document.getElementById("copyBtn").addEventListener("click", () => {
const emailContent = document.getElementById("emailOutput");
emailContent.select();
emailContent.setSelectionRange(0, 99999); // For mobile compatibility
navigator.clipboard.writeText(emailContent.value);
alert("Email copied to clipboard!");
});
document.getElementById("downloadBtn").addEventListener("click", () => {
const emailContent = document.getElementById("emailOutput").value;
const blob = new Blob([emailContent], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "email_template.txt";
a.click();
URL.revokeObjectURL(url);
});