-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendEmail.js
55 lines (44 loc) · 1.43 KB
/
sendEmail.js
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
const { authenticate } = require("@google-cloud/local-auth");
const path = require("path");
const {
createLabel,
getUnrepliedMail,
sendReply,
addLabel,
} = require("./utils");
exports.sendEmail = async (req, res) => {
const SCOPES = [
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/gmail.labels",
"https://mail.google.com/",
];
const labelName = "Vacation-Mails";
// Authenticate google account
const auth = await authenticate({
keyfilePath: path.join(__dirname, "credentials.json"),
scopes: SCOPES,
});
const labelId = await createLabel(auth, labelName);
console.log(`Label has been created ${labelId}`);
// check for mails in every 45 to 120 seconds
const main = async () => {
try {
const messages = await getUnrepliedMail(auth);
console.log(`Found ${messages.length} unreplied messages`);
for (const message of messages) {
await sendReply(auth, message);
console.log(`Sent reply to message with id ${message.id}`);
await addLabel(auth, message, labelId);
console.log(`Added label to message with id ${message.id}`);
}
} catch (error) {
throw error;
}
};
checkTime = () => {
return Math.floor(Math.random() * (120 - 45 + 1) + 45) * 1000;
};
setInterval(main, checkTime());
res.status(200).send("Email Responder is runninng...");
};