-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
291 lines (251 loc) · 7.68 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const fs = require("fs");
const { google } = require("googleapis");
const { promisify } = require("util");
const readline = require("readline");
const client_id =
"";
const client_secret = "";
const redirect_uris = ["http://localhost:3000"];
const tokenPath = "token.json"; // Path to store token
const SCOPES = ["https://www.googleapis.com/auth/gmail.modify"];
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
async function base64url(source) {
let encoded = Buffer.from(source).toString("base64");
encoded = encoded.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
return encoded;
}
async function getAuthorizationCode() {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES,
});
console.log("Authorize this app by visiting this URL:", authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question("Enter the authorization code: ", (code) => {
rl.close();
resolve(code.trim());
});
});
}
async function getAccessToken() {
try {
const token = await promisify(fs.readFile)(tokenPath);
oAuth2Client.setCredentials(JSON.parse(token));
} catch (err) {
const authCode = await getAuthorizationCode();
const { tokens } = await oAuth2Client.getToken(authCode);
oAuth2Client.setCredentials(tokens);
// Store the obtained token
fs.writeFileSync(tokenPath, JSON.stringify(tokens));
}
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function initializeGmail() {
await getAccessToken();
try {
// Log the tokens (optional)
console.log("Access token:", oAuth2Client.credentials.access_token);
console.log("Refresh token:", oAuth2Client.credentials.refresh_token);
// Create Gmail client
return google.gmail({
version: "v1",
auth: oAuth2Client,
userEmail: "mishrasanjayisback@gmail.com",
});
} catch (err) {
console.error("Error loading credentials:", err);
return null;
}
}
async function storeToken(token) {
try {
fs.writeFileSync(tokenPath, JSON.stringify(token));
console.log("Token stored to", tokenPath);
} catch (err) {
console.error("Error storing token:", err);
}
}
async function getStoredToken() {
try {
const token = fs.readFileSync(tokenPath);
return JSON.parse(token);
} catch (err) {
return null;
}
}
async function checkForNewEmails(gmail) {
try {
const response = await gmail.users.messages.list({
userId: "me",
labelIds: ["INBOX"],
maxResults: 1,
});
const messages = response.data.messages || [];
if (messages.length > 0) {
const firstMessageId = messages[0].id;
const messageDetails = await gmail.users.messages.get({
userId: "me",
id: firstMessageId,
});
const toHeader = messageDetails.data.payload.headers.find(
(header) => header.name === "To"
);
const fromHeader = messageDetails.data.payload.headers.find(
(header) => header.name === "From"
);
const recipientAddress = toHeader ? toHeader.value : null;
const senderAddress = fromHeader ? fromHeader.value : null;
console.log("Recipient Address:", recipientAddress);
console.log("Sender Address:", senderAddress);
return {
id: messageDetails.data.threadId,
messages: [messageDetails.data],
recipientAddress,
senderAddress,
};
} else {
console.log("No new emails found.");
return null;
}
} catch (error) {
console.error("Error checking for new emails:", error);
return null;
}
}
async function sendReplies(gmail, emailDetails, userEmail) {
try {
if (
emailDetails &&
emailDetails.messages &&
emailDetails.messages.length > 0
) {
const recipientAddress = emailDetails.senderAddress;
const receivedSubject = emailDetails.messages[0].payload.headers.find(
(header) => header.name === "Subject"
).value;
if (recipientAddress) {
const replyMessage = `Can't Read Emails for now, Will get bakck to you soon.`;
// Encode the entire email message as a base64 string
const encodedReply = Buffer.from(
`To: ${recipientAddress}\r\nFrom: ${userEmail}\r\nSubject: Re: ${receivedSubject}\r\n\r\n${replyMessage}`
).toString("base64");
const response = await gmail.users.messages.send({
userId: "me",
requestBody: {
threadId: emailDetails.id,
raw: encodedReply,
},
});
if (response.status === 200) {
console.log("Reply sent successfully.");
return true;
} else {
console.error("Error sending replies. Status code:", response.status);
return false;
}
} else {
console.error("Error: Recipient address not found.");
return false;
}
} else {
console.error("Error: Email messages are undefined or empty.");
return false;
}
} catch (error) {
console.error("Error sending replies:", error);
return false;
}
}
async function labelAndMoveEmail(gmail, threadId, labelName) {
try {
// Use the Gmail API to get the list of labels
const labels = await gmail.users.labels.list({
userId: "me",
});
// Check if the desired label exists
const label = labels.data.labels.find((label) => label.name === labelName);
if (label) {
// Apply the label to the thread
await gmail.users.threads.modify({
userId: "me",
id: threadId,
requestBody: {
addLabelIds: [label.id],
},
});
console.log(`Email labeled with "${labelName}"`);
return true;
} else {
// If the label doesn't exist, create it
const createdLabel = await gmail.users.labels.create({
userId: "me",
requestBody: {
name: labelName,
labelListVisibility: "labelShow",
messageListVisibility: "show",
},
});
if (createdLabel.status === 200) {
console.log(`Label "${labelName}" created.`);
// Apply the label to the thread
await gmail.users.threads.modify({
userId: "me",
id: threadId,
requestBody: {
addLabelIds: [createdLabel.data.id],
},
});
console.log(`Email labeled with "${labelName}"`);
return true;
} else {
console.error(
`Error creating label "${labelName}". Status code:`,
createdLabel.status
);
return false;
}
}
} catch (error) {
console.error("Error labeling and moving email:", error);
return false;
}
}
async function runApplication() {
const gmail = await initializeGmail();
if (!gmail) {
console.error("Error initializing Gmail API");
return;
}
while (true) {
try {
const emailDetails = await checkForNewEmails(gmail);
if (emailDetails) {
const success = await sendReplies(
gmail,
emailDetails,
"mishrasanjayisback@gmail.com"
);
console.log(`New email received with ID: ${emailDetails.id}`);
console.log(`Recipient Address: ${emailDetails.recipientAddress}`);
if (success) {
await labelAndMoveEmail(gmail, emailDetails.id, "AWAY");
}
}
const interval = Math.floor(Math.random() * (120 - 45 + 1) + 45);
console.log(`Waiting for ${interval} seconds...`);
await sleep(interval * 1000); // Convert seconds to milliseconds
} catch (error) {
console.error("Error:", error);
}
}
}
// Run the application
runApplication();