Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jzvi12 committed Nov 24, 2022
0 parents commit 15570a4
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env_sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[email protected]
CLIENT_ID=xxxxxxxxxxxxxx.apps.googleusercontent.com
CLIENT_SECRET=xxxxxxxxxxxxxx
TOPIC_URL=projects/<your-project-id>/topics/MyPush
SUBSCRIPTION_URL=projects/<your-project-id>/subscriptions/MyPush-sub
EMAIL_LABEL=UNREAD
WEBHOOK_URL=https://discord.com/api/webhooks/get/yours
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
credentials.json
token.json
gmailpush_history.json
.env
*.sh
node_modules
logs
backup
Binary file added favicon.ico
Binary file not shown.
38 changes: 38 additions & 0 deletions getNewToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require('dotenv').config();
const readline = require('readline');
const {google} = require('googleapis');

function getToken() {
const auth = new google.auth.OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
'urn:ietf:wg:oauth:2.0:oob'
);

const authUrl = auth.generateAuthUrl({
access_type: 'offline',
scope: ['https://www.googleapis.com/auth/gmail.readonly'],
});

console.log('Authorize this app by visiting this url:');
console.log(authUrl);

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question('Enter the code from that page here: ', (authCode) => {
rl.close();
auth.getToken(authCode, (err, token) => {
if (err) {
return console.log('Error retrieving access token', err);
}

console.log('Token:');
console.log(token);
});
});
}

getToken();
67 changes: 67 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require("dotenv").config();
const jsonToken = require("./token.json");
const Gmailpush = require("gmailpush");
const express = require("express");
const app = express();
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,
},
},
];

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...");
});
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "gmail-push-to-discord",
"version": "1.0.0",
"description": "Get real-time Gmail push notifications to your Discord channel!",
"keywords": [
"gmail",
"discord",
"bot",
"api",
"webhook"
],
"homepage": "https://github.com/jzvi12/gmail-push-to-discord",
"repository": {
"type": "git",
"url": "https://github.com/jzvi12/gmail-push-to-discord.git"
},
"main": "index.js",
"scripts": {
"start": "node ./index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"bugs": "https://github.com/jzvi12/gmail-push-to-discord/issues",
"author": "jzvi12",
"license": "MIT",
"dependencies": {
"@google-cloud/pubsub": "^3.2.1",
"discord.js": "^14.6.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"gmailpush": "^1.0.5",
"googleapis": "^61.0.0"
},
"engines": {
"node": ">=10"
}
}
29 changes: 29 additions & 0 deletions watch_request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require('dotenv').config();
const jsonToken = require('./token.json');
const {google} = require('googleapis');

const auth = new google.auth.OAuth2(process.env.CLIENT_ID, process.env.CLIENT_SECRET);

auth.setCredentials({
access_token: jsonToken.access_token,
refresh_token: jsonToken.refresh_token,
scope: jsonToken.scope,
token_type: jsonToken.token_type,
expiry_date: jsonToken.expiry_date
});

const gmail = google.gmail({
version: 'v1',
auth,
});

gmail.users
.watch({
userId: process.env.EMAIL,
requestBody: {
labelIds: [process.env.EMAIL_LABEL],
topicName: process.env.TOPIC_URL,
},
})
.then((result) => console.log(result))
.catch((err) => console.log(err));

0 comments on commit 15570a4

Please sign in to comment.