Create a call dispatching application with Node.js and Google Calendar API

Creating a call dispatcher application with Proximus FlexIO

Learn how to create a call dispatching application using Proximus FlexIO, Node.js and the Google Calendar APIs. Thanks to this application, calls to your staff on duty will be automatically redirected to the appropriate person by gathering data from a team calendar on Google. Let’s stop this stone age process where we hand over a “on duty phone” from staff member to staff member!

Step 1 – the story

A customer calls the official support line. According to which agent is on duty guard (information available inside Google Calendar) the calling customer will be forwarded to the number inside the Description field of the calendar events.

Google calendar events
A Google Calendar for on duty staff

If no agent is available, a message can be left to a mailbox.  This message will then be saved on the proximusflexio.enco.io account storage as a *.wav file.  A mail will then be sent to the support mailbox containing the URL of the recorded message, which can be listened to by simply clicking on the link.

Step 2 – Requirements

Important: this solution builds up on the previous tutorial “Creating a simple IVR/FlexIO application with Node.js” and assumes the necessary setup is done and the related code already running. In this tutorial we will make use of this environment and will refer, especially for the final steps, to that previous tutorial. Shouldn’t you have done so already, we strongly recommend you start with that tutorial first.

What you need to implement the current solution:

  • A Google Cloud Platform account (https://cloud.google.com/) ;
  • A Google calendar ;
  • The Google API’s library ;
  • A valid email address (Gmail or any other domain) ;
  • The Node mailer library in case you want to use your own SMTP server for sending out emails.

Step 3 – Google platform account

The following steps will guide you through the setup of a service account on the Google Cloud Platform:

  • Inside the Google Cloud Platform console, go to IAM And Management, then Service accounts ;
  • Create a new service account by providing a name, an ID (this is also an email address we’ll need later on) and a description, the click create ;
  • This account doesn’t need permissions, so click continue ;
Google Cloud Platform console
  • This account doesn’t need a service account user nor admin.  The only thing which is needed is some information on credentials.  Please generate the key from here and download it (the generated json file will be needed at a later stage).
Completing the Google service account and credentials

Step 4 – Service account email

Following steps will guide you through the setup of a service account email on the Google Cloud Platform:

  • First we need to add the service account email (ID) to the calendar (click here to see how to proceed) ;
  • The email should have the following permission: Make changes to events ;
  • The calendar ID can be found here, this one will also be needed ;
  • Install npm i -s googleapis date-fns (date-fns will be used to format and manipulate dates) from your project’s command-line.  If you are using Visual Studio Code, this can be also achieved from the TERMINAL pane:
Terminal access in Visual Studio Code
  • Copy the credentials.json file downloaded above into the root of the project directory.
Including the credentials.json file in the root directory of your project

Step 5 – Appointments.js

This is the code which makes the integration with the Google Calendar possible.

Please do the following:

  • Create a file called appointments.js inside your Node.js project under api\routes:
Creating the appointments.js file for your call dispatcher solution
  • Copy the following code to it (line 4 shows where the link with the credentials.json file is made):
                    
const { google } = require(‘googleapis’);
const scopes = [‘https://www.googleapis.com/auth/calendar’];
const client = new google.auth.GoogleAuth({
keyFile: ‘credentials.json’,
scopes,
});

The complete code can be found on our github.

Step 6 – Using Nodemailer or FlexIO RCML to send emails

Once the above files are ready, mails.js has to be created in the same folder as appointments.js.

The following code snippet uses the Nodemailer library which makes it possible to send messages from your Gmail account to any mail address using the standard Google SMTP Server.

                    

"use strict";
const nodemailer = require("nodemailer");

async function mail(pCallSid, pFrom, pTo, pDateTime, pUrl) {
    let transporter = nodemailer.createTransport({
        host: 'smtp.googlemail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'yourmail@address.be',
            pass: 'yourpassword',
        },
    });

The full code can be found on GitHub.

As an alternative to nodemailer, you could also use the Proximus FlexIO Email RCML verb for the same purpose. However in such case the email will not be send from your Google gmail account. See step 7 on how to use this verb instead.

Step 7 – index.js

Index.js manages the application’s start up, routing and some other features like allowing other modules to add functionalities. It can also act as a simple HTTP web server to run web applications.

The initial index.js code from this blog article was changed a little bit to add some extra business logic and RCML handling to support the current use case.

The first two lines of code will import the modules necessary to integrate with the above appointments.js module (and the mails.js module in case RCML <Email> isn’t used):

                    
const { getAppointments } = require(‘./appointments’);
const { mail } = require(‘./mails’);

Using the post() method, two routes are established: ‘/’ and ‘/recorded’ (this last one replaces ‘/select’ in the previous blog article).

The ‘/’ route is defined using the Router.post() method which responds only to HTTP POST requests.   This is the first endpoint that the FlexIO platform will query upon any new call, which in our case is an incoming phone call from a customer .  The first argument to this method is the URL path while the second is a call back function that will be invoked in case an HTTP POST request with this path is received.

The call back function here calls send() on the response to return when we receive a POST request with path ‘/’.  This will send a request body of type application/xml which content is defined by let rsp or rsp2 depending if a support agent is available (Google Calendar event) or not.

The /recorded inside RCML line ”   <Record action=\”” + initialpath + “/recorded” + “\” maxLength=\”30\” finishOnKey=\”#\”>\n” redirects to router.post(‘/recorded’, (req, res, next) => {} where following business logic is set:

  • The caller will be prompted to leave a message in case no support agent is available ;
  • Otherwise, the call of the customer will be redirected to the phone number inside the Google Calendar Event (see step 1).

Please do the following:

  1. To use Nodemailer: replace the existing code inside index.js by following snippet under api\routes: index.js file on github using Nodemailer
  2. To use the RCML <Email> verb: replace the existing code inside index.js by following snippet under api\routes: index.js file on github using RCML Email

While reviewing your index.js file, you may wish to only try to call the on duty staff member for a limited time before switching to recording a voicemail. In such case, look for the Dial RCML verb in the code, and as explained on the online documentation of FlexIO, just make use of the timeout parameter. Ideally, you should also include the callerId parameter to the phone number you have reserved on the FlexIO platform.

Final steps

Once your code is ready, a few more steps are needed in order to start testing your new call dispatcher solution. Simply follow steps 7 to 10 from the blog article explaining how to build a simple IVR using Node.js.

Author: API Solutions Team

Biographical Info