Snooze+ is an inbox app for Intercom used to automate sending delayed responses to customers.
- Local Development
- Development Tools
- Database
- Project Structure
- Scripts
- Steps To Deploy Application
- Application Hosting - Opalstack Node.js
- Authentication
- Required Environment Variables
- Tools and Dependencies
- Install Node.js and npm: Ensure that Node.js (version 20.x or higher) and npm (version 6.x or higher) are installed on your machine download Node.js
- Clone the Repository: Clone this repo to your machine:
- Navigate to the Project Directory: Navigate to the root directory of the application:
- Create Environment Variables File: Create an
.env.localfile with the required environment variables - Install Dependencies: Run the command
npm installto install dependencies - Setup Database: Setup a PostgreSQL database and run the migrations
- Start the Application: Run the command
npm run devto start the application - Access the Application: Access the application at
http://localhost:8706 - Intercom: Ensure that you have an Intercom account with an Inbox App created
The following tools are used to maintain code quality:
- ESLint for code linting
- Prettier for code formatting
- TypeScript for static type checking
VS Code launch configurations are provided for:
- Debugging the application
- Debugging tests
Jest is configured for testing with:
- TypeScript support
- Environment variables from .env.test
- Watch mode for development
The application uses PostgreSQL as its relational database to store user and message data.
- Install PostgreSQL: Ensure that PostgreSQL is installed on your system (download PostgreSQL)
- Create a Database: Create a new PostgreSQL database for the application
- Configure Environment Variables: Update your .env file with your database connection details
- Run Migrations: Apply the database migrations to create the necessary tables
The application uses Flyway for database migrations. Migration scripts are located in the database/migrations/ directory. To apply migrations, navigate to the database/ directory and run:
cd database
flyway migrateThe database consists of two main tables:
users: Stores user authentication and authorization datamessages: Stores scheduled messages to be sent to customers
Database interactions are handled using the pg library.
database/
migrations/ # Database migrations
schema-model/ # Database schema models
scripts/ # Deployment scripts
src/
config/ # Configuration files
controllers/ # Controllers for handling HTTP requests
middleware/ # Custom middleware functions
models/ # Database models or schemas
routes/ # Route definitions
services/ # Business logic and data access logic
types/ # TypeScript type definitions
utilities/ # Utility functions and helpers
app.ts # Main application file
| Script | Description |
|---|---|
npm start |
Start the application |
npm run dev |
Start the application in development mode |
npm run type-check |
Check for type errors |
npm run dev-build |
Build for development |
npm run prod-build |
Build for production with tests |
npm run copy-files |
Copy additional files to dist |
npm run lint |
Run linter and fix issues |
npm run test |
Run tests in watch mode |
- Clone this repo to your local machine
- Navigate to the root directory of the application
- Create a .env.production file with the required environment variables
- Run the command
npm run prod-buildto get the application files ready for deployment - Start a SSH session with the server
- On the server, run the stop script
- Start a SFTP session with the server
- Upload files from your local machine to the server
- Local file path:
./SnoozePlus-Intercom-Integration/dist/ - Remote server path:
/home/activelabs/apps/snoozeplus/
- Local file path:
- Go back to the SSH session and on the server, run the start script
Application server: opal7.opalstack.com
Database server: opal7.opalstack.com
The app can be controlled using the start and stop scripts. Each script takes a single argument for the app name (e.g. snoozeplus_dev, snoozeplus). If no argument is passed the default app name is snoozeplus.
- Navigate to the directory
~/apps/snoozeplus/ - Run the start or stop script with the app name
To start the app, run:
./start [app_name]Stop the app by running:
./stop [app_name]The application uses OAuth 2.0 for authentication with Intercom utilizing Passport with the Intercom Strategy. Here is an overview of how the authentication process works:
- Login: When a user attempts to access a protected route, they are redirected to the Intercom login page
- Authorization: The user logs in to their Intercom account and authorizes the application to access their Intercom data
- Callback: After authorization, Intercom redirects the user back to the application with an authorization code
- Token Exchange: The application exchanges the authorization code for an access token
- Session Management: The access token is encrypted and stored in the user's session, with the session being managed using express-session
- Authenticated Requests: The access token is used to make authenticated requests to the Intercom API on behalf of the user
The application validates incoming requests to ensure they originate from allowed Intercom Canvas Kit IP addresses or Intercom Webhook Notification IP addresses. The allowed IP addresses are specified in the IP_ALLOWLIST environment variable, which should be a comma-separated list of IP addresses.
The application validates the signatures of incoming requests to ensure they are sent by Intercom. This is done using the X-Body-Signature header for Canvas Kit requests and the X-Hub-Signature header for Webhook Notification requests.
The environment variables for this application can be found in the .env.sample file.
All of the environment variables are required for the application to run correctly. Note that NODE_ENV must be set to production in the .env.production file.
The application uses the following tools:
- Node.js: JavaScript runtime environment
- TypeScript: Typed superset of JavaScript that compiles to plain JavaScript
- ESLint: Linting utility for JavaScript and TypeScript
- Prettier: Opinionated code formatter
- Jest: JavaScript testing framework
- PostgreSQL: Open-source relational database
- Flyway: Database schema migration and versioning tool
The application leverages the following packages:
- Express: Web framework for Node.js
- Express Session: Session middleware for Express
- Morgan: HTTP request logger middleware for Node.js
- Winston: Logging library for Node.js
- Logtail: Cloud-based logging solution
- Passport: Authentication middleware for Node.js
- Passport-Intercom: Intercom authentication strategy for Passport
- Node.js Crypto Module: Provides cryptographic functionality
- Node Fetch: A light-weight module that brings Fetch API to Node.js
- Node Schedule: Cron-like and not-cron-like job scheduler for Node.js
- Retry: Abstraction for exponential and custom retry strategies
- pg: PostgreSQL client for Node.js