How to Build SMS Meeting Reminders with Node
Articles

How to Build SMS Meeting Reminders with Node

The Telnyx SMS API makes it easy to integrate SMS communications into your apps. The developer-friendly API allows you to build the types of messaging capabilities you want with ease. By using our Meeting Reminder tutorial, you’ll be able to remind customers of their upcoming meetings via SMS.
In this guide we’ll go over how to create the meeting scheduler using the Telnyx API. We’ll be using Node for the examples in this article, but you can access guides for other languages here.

Configuration

Create a config.json file in your project directory. First, use this guide to provision an SMS number and messaging profile, and create an API key. Then add those to the config file, along with your country code.
1
2
3
4
5
{
    "API_KEY": "YOUR_API_KEY",
    "COUNTRY_CODE": "+1",
    "FROM_NUMBER": "YOUR_TELNYX_NUMBER" 
}
Note: This file contains a secret key, it should not be committed to source control.
Server Initialization
First, import the config file and initialize the Telnyx library.
1
2
const config = require('./config.json');
const telnyx = Telnyx(config.API_KEY);
Then create an express app that watches the templates directory with Nunjucks, a simple templating language, and parses form data.
1
2
3
4
5
6
7
8
const app = express();
app.use(express.urlencoded());
app.set('views', `${__dirname}/templates`);

expressNunjucks(app, {
    watch: true,
    noCache: true,
});
Collect User Input
Create a simple HTML form, index.html, which collects the meeting date, time, customer name, and phone number. The full HTML source can be found in our GitHub repo, and we'll serve it at the root.
1
2
3
app.get('/', (req, res) => {
    res.render('index');
});
Implement the SMS Notification
Create a function that sends an SMS message parameterized on the destination number and text.
1
2
3
4
5
6
7
function sendReminder(to, message) {
    telnyx.messages.create({
        to: `${config.COUNTRY_CODE}${to}`,
        from: config.FROM_NUMBER,
        text: message 
    });
}
Parse User Input and Schedule the Message
Within the POST handler, parse the meeting time and compute how far into the future it is. We're using the moment Javascript library for this. Note that moment will use the current timezone for both datetime instances.
1
2
3
4
5
6
7
8
9
app.post('/', (req, res) => {
    const meetingDatetime = moment(
        `${req.body.meeting_date} ${req.body.meeting_time}`,
        'YYYY-MM-DD hh:mm');
    const now = moment();

    const delta = moment.duration(meetingDatetime.diff(now));
    // ...
}
If the meeting is sooner than 3 hours, 5 minutes from now, return an error.
1
2
3
4
5
if (delta < moment.duration({hours: 3, minutes: 5})) {
    res.render('index', {message: 'Can only schedule meetings at least 3 hours, 5 minutes in the future'});
} else {
    // ...
}
Remind the User
If the time is valid, compute when to send the reminder and schedule the function call. Note that we're using setTimeout here, but a production solution should make use of an asynchronus processing toolkit.
1
2
3
4
5
6
const reminderDatetime = meetingDatetime.subtract(3, 'hours');
const message = `${req.body.customer_name}, you have a meeting scheduled for ${meetingDatetime}`;

setTimeout(() => {
    sendReminder(req.body.phone, message);
}, reminderDatetime.diff(now));
Finally, render the success template, success.html.
1
2
3
4
5
6
res.render('success', {
    name: req.body.customer_name,
    meetingName: req.body.meeting_name,
    meetingDT: meetingDatetime,
    phone: req.body.phone
});
Start the Server
Start the Express app.
1
2
const port = 3000;
app.listen(port, () => console.log(`App running on port ${port}`));
Running the Project
Simply run node scheduler.js at the command line.
Share on Social

Worth checking out

By using the site, you agree to our use of cookies. Accept and close Find out more here.