-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (129 loc) · 4.33 KB
/
index.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express = require("express");
const app = express();
const path = require("path");
const { authenticate } = require("@google-cloud/local-auth");
const fs = require("fs").promises;
const { google } = require("googleapis");
const port = 4800;
// These are the scopes that we want to access
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/",
];
// I kept the label name
const labelName = "Auto-Mail";
app.get("/", async (req, res) => {
// Here, I am taking Google GMAIL authentication
const auth = await authenticate({
keyfilePath: path.join(__dirname, "credentials.json"),
scopes: SCOPES,
});
// Here, I am getting authorized Gmail ID
const gmail = google.gmail({ version: "v1", auth });
// Function to find all the labels available on the current Gmail
const response = await gmail.users.labels.list({
userId: "me",
});
// Function to find all emails that are unreplied or unseen
async function getUnrepliesMessages(auth) {
const gmail = google.gmail({ version: "v1", auth });
const response = await gmail.users.messages.list({
userId: "me",
labelIds: ["INBOX"],
q: "is:unread",
});
return response.data.messages || [];
}
// Function to generate the label ID
async function createLabel(auth) {
const gmail = google.gmail({ version: "v1", auth });
try {
const response = await gmail.users.labels.create({
userId: "me",
requestBody: {
name: labelName,
labelListVisibility: "labelShow",
messageListVisibility: "show",
},
});
return response.data.id;
} catch (error) {
if (error.code === 409) {
const response = await gmail.users.labels.list({
userId: "me",
});
const label = response.data.labels.find(
(label) => label.name === labelName
);
return label.id;
} else {
throw error;
}
}
}
async function main() {
// Create a label for the App
const labelId = await createLabel(auth);
// Repeat in random intervals
setInterval(async () => {
// Get messages that have no prior reply
const messages = await getUnrepliesMessages(auth);
// Check if there are any emails that did not get a reply
if (messages && messages.length > 0) {
for (const message of messages) {
const messageData = await gmail.users.messages.get({
auth,
userId: "me",
id: message.id,
});
const email = messageData.data;
const hasReplied = email.payload.headers.some(
(header) => header.name === "In-Reply-To"
);
if (!hasReplied) {
// Craft the reply message
const replyMessage = {
userId: "me",
resource: {
raw: Buffer.from(
`To: ${
email.payload.headers.find(
(header) => header.name === "From"
).value
}\r\n` +
`Subject: Re: ${
email.payload.headers.find(
(header) => header.name === "Subject"
).value
}\r\n` +
`Content-Type: text/plain; charset="UTF-8"\r\n` +
`Content-Transfer-Encoding: 7bit\r\n\r\n` +
`Thank you for your message. I'm currently on vacation, taking a break to recharge.
I'll respond to you promptly upon my return. Appreciate your understanding..\r\n`
).toString("base64"),
},
};
await gmail.users.messages.send(replyMessage);
// Add label and move the email
await gmail.users.messages.modify({
auth,
userId: "me",
id: message.id,
resource: {
addLabelIds: [labelId],
removeLabelIds: ["INBOX"],
},
});
}
}
}
}, Math.floor(Math.random() * (120 - 45 + 1) + 45) * 1000);
}
main();
res.json({ "this is Auth": auth });
});
app.listen(port, () => {
console.log(`server is running ${port}`);
});