forked from shelomito12/gmail-push-to-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (95 loc) · 2.75 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
require("dotenv").config();
const jsonToken = require("./token.json");
const Gmailpush = require("gmailpush");
const express = require("express");
const app = express();
const { google } = require("googleapis");
const schedule = require("node-schedule");
const fs = require("fs").promises;
const { WebhookClient } = require("discord.js");
const webhook = new WebhookClient({ url: process.env.WEBHOOK_URL });
// Initialize with OAuth2 config and Pub/Sub topic
const gmailpush = new Gmailpush({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
pubsubTopic: process.env.TOPIC_URL,
});
const users = [
{
email: process.env.EMAIL,
token: {
access_token: jsonToken.access_token,
refresh_token: jsonToken.refresh_token,
scope: jsonToken.scope,
token_type: jsonToken.token_type,
expiry_date: jsonToken.expiry_date,
},
},
];
const job = schedule.scheduleJob("0 0 * * *", async () => {
try {
const gmailpushHistoryJson = await fs.readFile("gmailpush_history.json");
const prevHistories = JSON.parse(gmailpushHistoryJson);
await Promise.all(
prevHistories.map(async (prevHistory) => {
try {
const { token } = users.find(
(user) => user.email === prevHistory.emailAddress
);
gmailpush._api.auth.setCredentials(token);
gmailpush._api.gmail = google.gmail({
version: 'v1',
auth: gmailpush._api.auth,
});
await gmailpush._api.gmail.users.watch({
userId: prevHistory.emailAddress,
requestBody: {
topicName: gmailpush._api.pubsubTopic,
},
});
} catch (err) {
console.error(err);
}
})
);
} catch (err) {
console.error(err);
}
});
app.post(
// Use URL set as Pub/Sub Subscription endpoint
"/pubsub",
express.json(),
async (req, res) => {
res.sendStatus(200);
const email = gmailpush.getEmailAddress(req.body);
const token = users.find((user) => user.email === email).token;
const { subject } = await gmailpush
.getNewMessage({
notification: req.body,
token,
})
.then((message) => {
if (message === null) {
return {};
}
if (!message.labelIds.includes(process.env.EMAIL_LABEL)) {
return {};
}
return message;
});
if (subject) {
webhook
.send({ content: subject })
.then((message) => console.log(`Sent message: ${message.content}`))
.catch(console.error);
} else {
console.log(
"Not sending message: Email Subject does not match label ID."
);
}
}
);
app.listen(3002, () => {
console.log("Server listening on port 3002...");
});