Get real-time Gmail push notifications to your Discord channel!
- Gmailpush library for handling Gmail API push notification
- discord.js for Node.js to interact with the Discord API
- No need to periodically poll the Gmail API's history feed for updates
- Just subscribe and whenever a change occurs, the app will instantly notify you
- Once a new email is pushed, it will route as a text message to your Discord webhook URL
- Choose which part of the email (subject, body, from, to) you want to include them in the message to Discord
- Automatic
refresh_token
is used with a scheduler module callednode-schedule
to extend the seven-days watch expiration as indicated here and here, respectively:-
Renewing mailbox watch - You must re-call watch() at least every 7 days or else you will stop receiving updates for the user. We recommend calling watch() once per day. The watch() response also has an expiration field with the timestamp for the watch expiration.
-
A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.
-
- Create a new project on the Google Cloud console
- Generate the
credentials.json
file by creating an OAuth2 client ID and client secret for your new project - Enable the Gmail API for your new project
- Enable the Pub/Sub API for your new project
- Create a Pub/Sub Topic and a Subscription for that Topic
- Grant Gmail privileges to Cloud Pub/Sub to allow publishing notifications to your topic. To do this, you need to grant publish privileges to [email protected]
- Make sure to select PUSH as the Delivery Type, and then enter your endpoint URL. You should get a VPS on the cloud with a public domain (HTTPS) for setting up the endpoint URL for google to send you updates. Deploy the Node.js server however you like. For testing purposes, I recommend using ngrok
- Create a Discord Webhook, then copy and paste the URL in the
.env
file
Clone this repo and then install required libraries with npm:
git clone https://github.com/jzvi12/gmail-push-to-discord.git
cd gmail-push-to-discord/
npm i
Rename the .env_exmaple
file to .env
and then add your credentials:
[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
From the prerequisites, download and place your credentials.json
file in the app root directory.
Generate a Gmail access token by running the following:
node tools/getNewToken.js
Copy the verification URL from the terminal and paste it a browser where the same Gmail account is already logged in. Just allow access to your new app using your account, then copy and paste the given code into the terminal.
If everything goes well, you'll get the access token in the console. Just copy it into a file and name it token.json
.
If you have created a user label and want to monitor this label, then you may want to run the following to get the label ID for the corresponding label name:
node tools/eventTracker.js
Just perform any action on your Gmail web client like deleting an email, creating a draft, or receiving a new email, then you will get some JSON data. Just look for the label list. You will then need to set this label ID in the .env
file (EMAIL_LABEL
).
Send a "Watch Request" so that Google starts watching your Gmail account for push notifications on the given user mailbox:
node tools/watchRequest.js
You should see a JSON response. Make sure you see status: 200
which means the request was accepted.
At this point you should be able to execute the main application:
node index.js
It's recommended to have an advanced process manager for your production Node.js applications such as pm2
:
sudo npm install pm2@latest -g
then you can start it as follows:
pm2 start index.js --name <your-app-name> --time
Your application will then start in the background, and if it crashes for whatever reason, it'll be automatically restarted.