In this tutorial we’re going to build an SMS reminder app with Node.js. We’re going to use the user’s Google Calendar to get appointments and then send the text message with Twilio.
Setting Things Up
First you’re going to need to have a Google account and a Twilio account. If you don’t have those yet, you can go ahead and sign up. Here are the links:
You don’t need to worry about Twilio, it’s free to try.
Google Console Project
Once you have a Google account, go to the Google Console and create a new app. By default the Google Console page shows you the dashboard of the most recent app that you’ve worked on. But if you haven’t work on any projects yet it will show the following:
From there you can click on the select project
menu at the upper right corner and select create a project
. This opens a modal window which allows you to enter the title of the project.
Once the project is created, the dashboard is displayed. From there you can click on the use Google APIs
, search for the Google Calendar API and enable it.
Once the API is enabled, it will ask you to create credentials. Click Go to Credentials
to begin setting it up. This will show you the following:
Click on the Add credentials
button then select OAuth 2.0 client ID
.
This will ask you to configure consent screen first. Click on configure consent screen
.
Enter a value for the Product name shown to users
text field and click on save
.
Once that’s configured you can now create the client ID. Select Web application
for the application type, leave the default name (if you want), enter http://localhost:3000/login
for the Authorized redirect URIs
then click create
.
This opens a modal which displays the client ID and client secret. Take note of those for now as we will be using them later.
Twilio
Once you’ve created a Twilio account, go to the settings page and take note of the values for the AccountSID
and AuthToken
under the Live API Credentials
.
Next go to the programmable voice dashboard
. This is where you can see the sandbox number. You can use this number for testing twilio. But Later on you will need to buy a phone number so that the text messages sent by twilio won’t have “sent from twilio sandbox” added to it. Another limit of the Twilio sandbox number is that it can only be used with verified numbers. Which means you have to register a phone number with twilio in order to send a message to it. You can do this from the manage caller IDs page
.
Building the App
Now we’re ready to build the app. Before we proceed I’d like to provide a brief overview on how we’re going to implement the app. There’s going to be three major files: one for the server, one for caching events from Google Calendar and one for reminding the user. The server is used for allowing the user to login and obtaining an access token. The events will be saved in the MySQL database and the global app configuration will be added in a .json
file. Node’s implementation of cron
will be used for executing the task for caching events and reminding the user.
Installing the Dependencies
On your working directory, create a package.json
file and add the following:
{
"name": "google-calendar-twilio",
"version": "0.0.1",
"dependencies": {
"config": "^1.17.1",
"cron": "^1.1.0",
"express": "^4.13.3",
"googleapis": "^2.1.6",
"moment": "^2.10.6",
"moment-timezone": "^0.4.1",
"mysql": "felixge/node-mysql",
"twilio": "^2.6.0"
}
}
In this file we’re specifying the name and version of the libraries which our app depends on. Here’s a break down of usage for each library:
config
- used for storing and retrieving global app configuration.cron
- used for executing a specific task at a specific time of the day. In this app we’re using it for running the task for caching events from the users Google calendar and sending text reminders.express
- the defacto web framework for Node.js. We’re using it to serve the login page.googleapis
- the official Node.js client for Google’s APIs.moment
- a date and time library. We’re using it to easily format the dates that we get from the Google Calendar API.moment-timezone
- the timezone plugin for moment. This sets the default timezone for the app.mysql
- a MySQL client for Node.js.twilio
- the official Twilio client for Node.js. This allows us to send text reminders.
Execute npm install
from your terminal to install all the dependencies.
Database
As mentioned earlier, we’re going to use the MySQL database for this app. Go ahead and create a new database using the database management tool of your choice. Then use the following SQL dump file to create the tables: appointment-notifier.sql
.
There are two tables in the database: users
and appointments
. The users
table is used for storing the user’s data. In the case of this app, we’re only going to store one user, and only the access token is stored.
The appointments
table is used for storing the events which we got from the Google Calendar API. Note that it has no user_id
field in it because we only have one user. And we’re going to fetch all the rows which have zero as the value for the notified
field.
App Configuration
On your working directory, create a config
folder then inside it create a default.json
file. This is where we will put the global app configuration. This includes the timezone, the phone number to which we’re going to send the reminders, the database, google app and Twilio settings.
Here’s the template, be sure to fill in all the fields.
{
"app": {
"timezone": "Asia/Manila"
},
"me": {
"phone_number": ""
},
"db": {
"host": "localhost",
"user": "root",
"password": "secret",
"database": "calendar_notifier"
},
"google":{
"client_id": "THE CLIENT ID OF YOUR GOOGLE APP",
"client_secret": "THE CLIENT SECRET OF YOUR GOOGLE APP",
"redirect_uri": "http://localhost:3000/login",
"access_type": "offline",
"scopes": [
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/calendar"
]
},
"twilio": {
"sid": "YOUR TWILIO SID",
"secret": "YOUR TWILIO SECRET",
"phone_number": "+YOUR TWILIO PHONE NUMBER / SANDBOX NUMBER"
}
}
Common Files
As good developers we need to avoid code repetition as much as we can. That’s why we need to put code that’s needed by those three major files (server, cache, notify) that I mentioned earlier into separate files. Create a common
folder on your working directory. This is where we’re going to add the common files.
Database
Create a db.js
file inside the common
directory then add the following:
var config = require('config');
var db_config = config.get('db');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: db_config.host,
user: db_config.user,
password: db_config.password,
database: db_config.database
});
exports.db = connection;
This uses the config library to get the configuration values that we’ve added earlier on the config/default.json
file. Specifically we’re getting the database configuration so that we can connect to the database. Then we’re exporting this module so that we can use it later on from another file.
Time
The time.js
file is used for setting the default timezone with the moment-timezone
library. We also export the value for the timezone since we’re going to use it later when running the two cron tasks (caching events and notifying users).
var config = require('config');
var app_timezone = config.get('app.timezone');
var moment = require('moment-timezone');
moment.tz.setDefault(app_timezone);
exports.config = {
timezone: app_timezone
};
exports.moment = moment;
The google.js
file is used for initializing the Google client and the OAuth2 client. In order to initialize the OAuth2 client we need to pass in the client ID, client secret and the redirect URL which we have added in the configuration file earlier. Then we initialize the Google Calendar service. Lastly, we export the OAuth2 client, calendar and the Google configuration.
var config = require('config');
var google_config = config.get('google');
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(google_config.client_id, google_config.client_secret, google_config.redirect_uri);
var calendar = google.calendar('v3');
exports.oauth2Client = oauth2Client;
exports.calendar = calendar;
exports.config = google_config;
Continue reading %How to Build an SMS Appointment Reminder App with Twilio%