Ahoy! We now recommend you build your appointment reminders with Twilio's built-in Message Scheduling functionality. Head on over to the Message Scheduling documentation to learn more about scheduling messages.
This Node.js Express web application sends out reminders for future appointments that customers can create through the application as well. This is done through a background job that runs every minute.
On this tutorial we'll point out the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.
Check out how Yelp uses SMS to confirm restaurant reservations for diners.
Let's get started! Click the button below to get started.
Before we can use the Twilio API to send reminder text messages we need to configure our account credentials. These can be found on your Twilio Console. You'll also need an SMS-enabled phone number - you can find or purchase a new one here.
1TWILIO_ACCOUNT_SID=Your-Account-SID2TWILIO_AUTH_TOKEN=Your-Twilio-Auth-Token3TWILIO_PHONE_NUMBER=Your-Twilio-Phone-Number4MONGO_URL=Mongo-Url5MONGO_URL_TEST=mongodb://127.0.0.1:27017/appointment-reminders6NODE_ENV=production
In order to send an appointment reminder we need to have an appointment first!
On the controller we input the information required (a customer's name and phone number, plus a time and date for the appointment) by saving it on an Appointment
model.
We use mongoose in this application to store our model in MongoDB.
1var AppointmentSchema = new mongoose.Schema({2name:String,3phoneNumber: String,4notification : Number,5timeZone : String,6time : {type : Date, index : true}7});
routes/appointments.js
1'use strict';23const express = require('express');4const momentTimeZone = require('moment-timezone');5const moment = require('moment');6const Appointment = require('../models/appointment');7const router = new express.Router();8910const getTimeZones = function() {11return momentTimeZone.tz.names();12};1314// GET: /appointments15router.get('/', function(req, res, next) {16Appointment.find()17.then(function(appointments) {18res.render('appointments/index', {appointments: appointments});19});20});2122// GET: /appointments/create23router.get('/create', function(req, res, next) {24res.render('appointments/create', {25timeZones: getTimeZones(),26appointment: new Appointment({name: '',27phoneNumber: '',28notification: '',29timeZone: '',30time: ''})});31});3233// POST: /appointments34router.post('/', function(req, res, next) {35const name = req.body.name;36const phoneNumber = req.body.phoneNumber;37const notification = req.body.notification;38const timeZone = req.body.timeZone;39const time = moment(req.body.time, 'MM-DD-YYYY hh:mma');4041const appointment = new Appointment({name: name,42phoneNumber: phoneNumber,43notification: notification,44timeZone: timeZone,45time: time});46appointment.save()47.then(function() {48res.redirect('/');49});50});5152// GET: /appointments/:id/edit53router.get('/:id/edit', function(req, res, next) {54const id = req.params.id;55Appointment.findOne({_id: id})56.then(function(appointment) {57res.render('appointments/edit', {timeZones: getTimeZones(),58appointment: appointment});59});60});6162// POST: /appointments/:id/edit63router.post('/:id/edit', function(req, res, next) {64const id = req.params.id;65const name = req.body.name;66const phoneNumber = req.body.phoneNumber;67const notification = req.body.notification;68const timeZone = req.body.timeZone;69const time = moment(req.body.time, 'MM-DD-YYYY hh:mma');7071Appointment.findOne({_id: id})72.then(function(appointment) {73appointment.name = name;74appointment.phoneNumber = phoneNumber;75appointment.notification = notification;76appointment.timeZone = timeZone;77appointment.time = time;7879appointment.save()80.then(function() {81res.redirect('/');82});83});84});8586// POST: /appointments/:id/delete87router.post('/:id/delete', function(req, res, next) {88const id = req.params.id;8990Appointment.remove({_id: id})91.then(function() {92res.redirect('/');93});94});9596module.exports = router;
Now that we have our Appointment
created, let's see how to schedule a reminder for it.
Every minute we'd like our application to check the appointments database to see if any appointments are coming up that require reminders to be sent out.
To do this we use node-cron.
We configure on the start function both the job code we'd like to run, and the interval on which to run it. Then we call it from the application execution entry point like this: scheduler.start()
scheduler.js
1'use strict';23const CronJob = require('cron').CronJob;4const notificationsWorker = require('./workers/notificationsWorker');5const moment = require('moment');67const schedulerFactory = function() {8return {9start: function() {10new CronJob('00 * * * * *', function() {11console.log('Running Send Notifications Worker for ' +12moment().format());13notificationsWorker.run();14}, null, true, '');15},16};17};1819module.exports = schedulerFactory();
This start
function uses a notificationsWorker
, next we'll see how it works.
To actually execute our recurring job logic, we create a worker function which uses a Static Model Method to query the database for upcoming appointments and sends reminders as necessary.
workers/notificationsWorker.js
1'use strict';23const Appointment = require('../models/appointment');45const notificationWorkerFactory = function() {6return {7run: function() {8Appointment.sendNotifications();9},10};11};1213module.exports = notificationWorkerFactory();
Next, let's see how the Appointment
job works in detail.
Our recurring job uses a static model method of the Appointment
model to query the database for appointments coming up in the current minute and send out reminder messages using a Twilio REST Client we previously initialized with our Twilio account credentials.
Because of the fact that appointments are defined in different time zones, we use Moment.js library in order to properly query every upcoming appointment considering its time zone.
models/appointment.js
1'use strict';23const mongoose = require('mongoose');4const moment = require('moment');5const cfg = require('../config');6const Twilio = require('twilio');78const AppointmentSchema = new mongoose.Schema({9name: String,10phoneNumber: String,11notification: Number,12timeZone: String,13time: {type: Date, index: true},14});1516AppointmentSchema.methods.requiresNotification = function(date) {17return Math.round(moment.duration(moment(this.time).tz(this.timeZone).utc()18.diff(moment(date).utc())19).asMinutes()) === this.notification;20};2122AppointmentSchema.statics.sendNotifications = function(callback) {23// now24const searchDate = new Date();25Appointment26.find()27.then(function(appointments) {28appointments = appointments.filter(function(appointment) {29return appointment.requiresNotification(searchDate);30});31if (appointments.length > 0) {32sendNotifications(appointments);33}34});3536/**37* Send messages to all appoinment owners via Twilio38* @param {array} appointments List of appointments.39*/40function sendNotifications(appointments) {41const client = new Twilio(cfg.twilioAccountSid, cfg.twilioAuthToken);42appointments.forEach(function(appointment) {43// Create options to send the message44const options = {45to: `+ ${appointment.phoneNumber}`,46from: cfg.twilioPhoneNumber,47/* eslint-disable max-len */48body: `Hi ${appointment.name}. Just a reminder that you have an appointment coming up.`,49/* eslint-enable max-len */50};5152// Send the message!53client.messages.create(options, function(err, response) {54if (err) {55// Just log it for now56console.error(err);57} else {58// Log the last few digits of a phone number59let masked = appointment.phoneNumber.substr(0,60appointment.phoneNumber.length - 5);61masked += '*****';62console.log(`Message sent to ${masked}`);63}64});65});6667// Don't wait on success/failure, just indicate all messages have been68// queued for delivery69if (callback) {70callback.call();71}72}73};747576const Appointment = mongoose.model('appointment', AppointmentSchema);77module.exports = Appointment;
All that is left is to send the actual SMS. We'll see that next.
This code is called for every appointment coming up that requires a reminder to be sent. We provide a configuration object with a to
field, which is the customer's phone number, a from
field, which is a number in our account, and a body
field, which contains the text of the message. Then we pass it to the sendMessage
method along with a callback to log errors and success.
models/appointment.js
1'use strict';23const mongoose = require('mongoose');4const moment = require('moment');5const cfg = require('../config');6const Twilio = require('twilio');78const AppointmentSchema = new mongoose.Schema({9name: String,10phoneNumber: String,11notification: Number,12timeZone: String,13time: {type: Date, index: true},14});1516AppointmentSchema.methods.requiresNotification = function(date) {17return Math.round(moment.duration(moment(this.time).tz(this.timeZone).utc()18.diff(moment(date).utc())19).asMinutes()) === this.notification;20};2122AppointmentSchema.statics.sendNotifications = function(callback) {23// now24const searchDate = new Date();25Appointment26.find()27.then(function(appointments) {28appointments = appointments.filter(function(appointment) {29return appointment.requiresNotification(searchDate);30});31if (appointments.length > 0) {32sendNotifications(appointments);33}34});3536/**37* Send messages to all appoinment owners via Twilio38* @param {array} appointments List of appointments.39*/40function sendNotifications(appointments) {41const client = new Twilio(cfg.twilioAccountSid, cfg.twilioAuthToken);42appointments.forEach(function(appointment) {43// Create options to send the message44const options = {45to: `+ ${appointment.phoneNumber}`,46from: cfg.twilioPhoneNumber,47/* eslint-disable max-len */48body: `Hi ${appointment.name}. Just a reminder that you have an appointment coming up.`,49/* eslint-enable max-len */50};5152// Send the message!53client.messages.create(options, function(err, response) {54if (err) {55// Just log it for now56console.error(err);57} else {58// Log the last few digits of a phone number59let masked = appointment.phoneNumber.substr(0,60appointment.phoneNumber.length - 5);61masked += '*****';62console.log(`Message sent to ${masked}`);63}64});65});6667// Don't wait on success/failure, just indicate all messages have been68// queued for delivery69if (callback) {70callback.call();71}72}73};747576const Appointment = mongoose.model('appointment', AppointmentSchema);77module.exports = Appointment;
That's it! Our application is all set to send out reminders for upcoming appointments.
We hope you found this sample application useful. If you're a Node.js/Express developer working with Twilio you might enjoy these other tutorials:
Build a ready-for-scale automated SMS workflow for a vacation rental company.
Make browser-to-phone and browser-to-browser calls with ease.