Serverless SMS Messaging using JavaScript and Twilio Functions

November 12, 2019
Written by
Felistas Ngumi
Contributor
Opinions expressed by Twilio contributors are their own

Serverless SMS Messaging using JavaScript and Twilio Functions

Function As A Service is a solution that allows developers to develop, deploy, and run event-driven functions in the cloud that are fully managed. Without the need for maintaining infrastructure, "serverless" architecture is achieved. In this tutorial, you will learn how to send Twilio Programmable SMS using the new Twilio CLI.

Project Setup

You will need the following items in order to get started:

  1. A Twilio account
  2. Twilio CLI
  3. Node Version 8 and above.

Getting Started

First, install the Twilio CLI. I'm currently using version 1.8.1. Run the following command in your preferred terminal if on a Mac:

$ brew tap twilio/brew && brew install twilio

If you are on a different platform, you can find the setup instructions here.

To confirm a successful installation of the CLI has occurred, type twilio in a new terminal session and the output below should appear.

Twilio CLI console

Login to your Twilio account by running twilio login in your terminal. You will be prompted for your Twilio Account SID and Auth Token which you can access in your Twilio Console. As seen in the image above, the serverless commands for Twilio functions are disabled by default. To install the serverless plugin run the command below:

$ twilio plugins:install @twilio-labs/plugin-serverless

Now create a folder where all your project files will be stored.

$ mkdir demo && cd demo

Configuring Twilio to Send SMS From our Function

After creating a free account on Twilio and purchasing a trial number with SMS capabilities, you will need to double-check if permissions are allowed to send SMS to your country region. If not, navigate to the permission page here and enable it.

Creating our Server Function

Inside your directory, create the serverless project by running twilio serverless:init greetings. This command creates a project directory and installs all the necessary dependencies. Next, run cd greetings and open the project in your favorite editor.

The assets folder holds static files in your project and the functions folder holds your functions as the name suggests. Delete the functions folder contents then add your custom file using the following commands.

Run rm -rf functions/*. Then run cd functions && touch greetings.js in your terminal.

Navigate to the .env file and add the following keys if they’re not already present:

ACCOUNT_SID=your_account_SID
AUTH_TOKEN=your_account_token

Next, inside the greetings.js file add the following lines of code:

exports.handler = function(context, event, callback) {
    context.getTwilioClient().messages.create({
      to: '+254712345678',
      from: '+12017287474', 
      body: 'Good Morning?'
    }).then(msg => {
      callback(null, msg.sid);
    }).catch(err => callback(err));
  }

Note: Change the to and from keys in the code to your phone number and your newly purchased Twilio number respectively.

The above piece of code exports a function that when an event is triggered, sends an SMS to the specified recipient. Before you deploy your function, try sending an SMS locally first. Check the “Enable ACCOUNT_SID and AUTH_TOKEN” option in your Functions Configuration so that Twilio can include your account credentials on every function call.

Run twilio serverless:start, copy the greetings functions URL which should be http://localhost:3000/greetings and paste it in a new tab in your browser. You should have received a text on your phone.

To deploy your function, run twilio serverless:deploy. After a successful deployment, you should expect to see a similar screen once you navigate to the functions console.

Twilio Functions Configuration

On your terminal, copy the URL under Functions. In my case, the URL is https://greetings-1921-dev.twil.io/greetings. Paste it in your browser and an SMS should be sent to the specified number.

Conclusion

In this tutorial, we have learned how to send an Outbound SMS using Twilio functions. You could extend this application by generating custom responses based on the incoming message.

I would love to hear from you. Let's chat on Twitter or send me an email. Happy hacking!