Skip to content

Get real-time Gmail Push Notifications to your API

Notifications You must be signed in to change notification settings

HMB-research/gmail-push-to-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gmail Push notifications to Discord

gmail_discord_integrations

Get real-time Gmail push notifications to your Discord channel!

Acknowledgements

Features

  • 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 called node-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.

Prerequisites

Installation

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. image

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.

License

MIT

About

Get real-time Gmail Push Notifications to your API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%