Project for Push Notifications to Webhooks
Introduction
GmailNotify This is a quick project made to consume Pub-Sub messages sent via Gmail to be processed and finally execute a Webhook for external services to receive a push of email data.
It is in essense a set of NodeJS Cloud Functions to handle Gmail Notifications which can be configured to receive wide range of Gmail notifications based on the config/appconfig.json file setting.
Technologies Used
- NodeJS v14
- google-cloud/pubsub v0.18.0
- google-cloud/storage v5.14.0
- googleapis v85.0.0
- phin v3.6.0
Google Cloud Resources Used
- Cloud Functions
- Cloud Storage
- Pub/Sub
These are just the primary resources, other supporting resources are also used but mainly for networking, security and permissions.
Code Structure
The code is structured for clean and simple implementation as well as maintenance.
Primary Folder's Explanation
/config- contains theappconfig.jsonfile (renamed fromsample-appconfig.jsontemplate file) after putting in the values./credentials- contains thegoogle-key.jsonfile (renamed fromsample-google-key.jsontemplate file) after replacing the content from the actual json key file download from Google Cloud Platform./modules- contains the individual modules*.jsfiles./modules/functions- contains the core Cloud Functions in their respective*.jsfiles./tests- contains all the JEST test files used to perform checks and validation on the functionality of the application locally, before deploying the Cloud Functions.
Compiling
There are scripts already part of the package.json file to publish the project into the /publish directory and zip the output with the project name and version (from package.json.version property) for quick upload and deployment of Cloud Functions.
Publish Command
npm run publish
Don't forget to rename the
sample-*.jsonfiles in/configand/credentialsfolder as required (see below section Basic Application Configuration).
Publish outputs to a zip file
/publish/GNFunc_v{version}.zip
Run all tests
npm run test
Google Cloud Setup
Create a Cloud Storage
Create Cloud Functions
Functional Flow
The Cloud Function is deployed so that it starts the code in the /main.js file (which is renamed to /index.js on publish) and executes the assigned Cloud Function Entry Point.
Google Cloud Credentials
To get the Google Cloud Credentials, you need to complete the following steps on your Google Cloud Platform account.
Basic Application Configuration
You must have the Google Cloud Credentials .json file as well as have created the Cloud Storage, before you configure the application.
Rules
While entering values for the application config, please keep in mind the following.
gcp.storage.*- any folder name property (likegcp.storage.rootFolderNameorgcp.storage.debugFolderName, etc.) must end with a/(forward slash) if a value is provided.gcp.storage.historyFilename- property must have a filename ending with.jsonfile extension.gcp.auth.googleKeyFilePath- property must have a file path ending with a filename that has the.jsonfile extension.gcp.auth.subject- property must be your full valid email address of the Gmail account you want to recevie the push notification from.gmail- property object is based on the Gmail API and more information on the appropriate values for this properties can be found in the Gmail API Reference.
Minimum Configuration
For you to quickly deploy this application, you only need to provide the following configuration
For /config/sample-appconfig.json file.
external.webhookUrl- a full qualified URL to an external service that accepts anPostrequest with the body{ text: 'email_text_here' }gcp.pubsub.topic- the full topic name as provided in the Cloud Pub/Sub Dashboard.gcp.storage.bucketName- the full bucket name as provided in the Cloud Storage Dashboard.gcp.auth.subject- the full gmail email address you are to recevie the Pub/Sub notifications from.
For /credentials/sample-google-key.json file.
- Simply replace the content of this sample file with the content from your download
.jsoncloud key file.
Rename Config & Credentials Files
-
Rename the
sample-google-key.jsonfile togoogle-key.jsonfile. -
Rename the
sample-appconfig.jsonfile toappconfig.jsonfile.