Cloud ServicesHow-Tos

Getting started with Twilio Serverless

How to series

How to Create an Application for Conference Call 📲

1*JCvOmzTCS G2imqHjqGIjg

Connecting with people through virtual communication is the need of the hour. We get thousands of IVR calls in our day-to-day lives. There may be times when we need to dial up and navigate through an automatic voice process before reaching a customer care agent.

Have you ever wondered what goes behind the whole concept of initiating a conference call?

As a tech geek and a developer by profession, I was curious about understanding and trying my hands at building a Twilio-powered conference call handler. I was intrigued by the coding, handling, testing, and building and application.

I am planning to describe the ins and outs of the process in words, and that may take more than one blog. But that’s the most exciting part of being a developer.

What is Twilio?

Twilio is a cloud communications platform that provides developers with APIs to build, manage, and scale communication applications. With Twilio, you can directly integrate voice calls, video calls, messaging, and other communication services into your applications, enabling seamless user interactions across different platforms.

You can learn more about Twilio here — https://www.twilio.com/docs

Getting Started

We will create an asynchronous handler for a serverless function for Twilio to initiate conference calls for speakers and registrants based on a TalkCode.

Prerequisites

Before you begin, ensure you have the following:

  1. Twilio Account: To get started, sign up for a free account here. You’ll receive trial credits to test the platform.
  2. Twilio CLI: Install the Twilio Command Line Interface to manage your Twilio projects from the terminal.
  3. ngrok: This tool creates a secure tunnel to your localhost, making it accessible from the internet. This is essential for testing webhooks. Download it here.
  4. Twilio Serverless: We will use this technique, to save time on setting up everything from scratch. Twilio Serverless is a powerful toolkit to deploy your Twilio Application. To setup Twilio CLI and Serverless template, click here.

Let’s Build

Now, once you create a pre-made app template, let’s dive into the core functionality of our conference call application.

Following is an overview of the code that manages conference calls using Twilio:

Step 1: Initializing Data

We start by importing the necessary assets and initializing our data module:

const assets = Runtime.getAssets();
const { Data } = require(assets["/data.js"].path);

Step 2: Fetching Talk Information

Using the TalkCode from the event, we fetch relevant talk details:

const code = event.TalkCode;
const talk = data.getTalkByCode(code);

Step 3: Configuring Domain Name

To handle local testing, we configure the domain name:

let domainName = context.DOMAIN_NAME;
if (domainName.startsWith("localhost")) {
domainName = "<YOUR NGROK URL>";
}

This ensures that our application can communicate correctly, whether locally or in production.

Step 4: Calling Speakers

Next, we iterate through the speakers and initiate calls using Twilio’s conferences API:

/* This block of code is iterating over each speaker in the `talk.speakers` array and initiating a
call to each speaker using Twilio's `conferences` API. */


for (const speaker of talk.speakers) {
const participant = await client
.conferences(talk.code)
.participants.create({
to: speaker.phoneNumber,
from: context.TWILIO_PHONE_NUMBER,
label: speaker.name,
beep: true,
startConferenceOnEnter: true,
conferenceStatusCallback: `https://${domainName}/status-handler`,
conferenceStatusCallbackEvents: ["leave"],
});

speakerCallSids.push(participant.callSid);
}

Step 5: Calling Registrants

After calling speakers, we call the registrants in a similar manner, ensuring to handle duplicates:

const registrants = await data.getRegistrants(talk);
console.log("Calling registrants....");
let registrantPhoneNumbers = registrants.map((r) => r.phoneNumber);
const registrantsPhoneNumbersSet = new Set(registrantPhoneNumbers);
registrantPhoneNumbers = [...registrantsPhoneNumbersSet];

/* The `const promises = registrantPhoneNumbers.map(async (registrantPhoneNumber) => { ... }` code
block is creating an array of promises. */


const promises = registrantPhoneNumbers.map(async (registrantPhoneNumber) => {
const participant = await client
.conferences(talk.code)
.participants.create({
to: registrantPhoneNumber,
from: context.TWILIO_PHONE_NUMBER,
beep: false,
startConferenceOnEnter: false,
});

return participant.callSid;
});

Step 6: Logging and Callback

Finally, we log the results and send a response:

/* The `callback(null, { talkCode: event.TalkCode, speakerCallSids, registrantCallSids });` statement
is used to send a response back to the caller of this function. */


callback(null, {
talkCode: event.TalkCode,
speakerCallSids,
registrantCallSids,
});

Ready to get started?

Following the steps outlined in this guide, you can easily set up and manage conference calls for your users. Set up your Twilio account, install the required tools, and begin building your own conference call application today!

To get the complete code for this application: https://github.com/RajSM139/twilio-voice-sms-demo-app/

* [IMPORTANT] Protected Functions

In Twilio Serverless, protected functions are a way to secure your serverless functions by restricting access to them. These functions can only be invoked by authenticated users or services, ensuring that sensitive operations are not exposed to unauthorized access.

You can follow the Twilio Docs on Security to find out how to do it.
For this demo, you can generate a temporary X-Twilio-Signature to test our conference endpoint. Click here.

If you have any questions or need assistance, feel free to leave a comment below or reach out. Happy coding!

## Commands

  • Create a New Project: twilio serverless:init my-project
  • To start the server: twilio serverless:start
  • To deploy: twilio serverless:deploy

## Links for reference:

Credits

Thank You, Twilio, for the wonderful course on hands-on with Twilio Serverless, TwiML, Programmable Voice, and SMS.

Rajesh Mishra

I'm a developer who loves sharing insights, technical how-tos, and lessons learned from the world of code. While much of what I write may not be groundbreaking, I believe in documenting for future me—and for anyone else who might find it useful. Beyond tech, I also dive into life's experiences and moments, reflecting on personal growth and sharing stories that resonate. Whether you're here for practical tips or a fresh perspective on life, I hope you find something meaningful.

Leave a Reply

Your email address will not be published. Required fields are marked *