Edit on GitHub
Report issue
Page history

Submitted by @{[ TutorialCtrl.tutorial.github_username ]} {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Respond to SMS Messages with Twilio and Google Cloud Functions description: Learn how to receive and respond to SMS messages with Twilio and Google Cloud Functions. author: jmdobry tags: Cloud Functions, Twilio, Node.js date_published: 2017-02-17


Introduction

This tutorial demonstrates using Google Cloud Functions to reply to an SMS message using Twilio. The Cloud Function is implemented in Node.js.

The sample Cloud Function is triggered by a webhook request from Twilio when a SMS message is sent to your Twilio phone number. The webhook validates that the request came from Twilio and then sends a simple reply.

Prerequisites

  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.

Getting Started with Twilio

  1. Create a Twilio account.
  2. In your Twilio console, create a phone number.
  3. Once you have a phone number, click Manage Numbers and then click on your phone number.

  4. Under Messaging:

    1. Set Configure with to Webhooks/TwiML.
    2. Set A Message Comes In to Webhook and enter the following URL:

      https://us-central1-[YOUR_PROJECT_ID].cloudfunctions.net/reply
      

      Replace [YOUR_PROJECT_ID] with your Google Cloud Platform project ID.

    3. Click Save.

  5. Return to your Twilio Account Settings and take note of the Auth Token for your Live Credentials. You will need it later in this tutorial.

Preparing the Cloud Function

  1. Create a package.json file by running the following:

    npm init
    

    or

    yarn init
    
  2. Install the single dependency used by the Cloud Function:

    npm install --save twilio
    

    or

    yarn add twilio
    

    This dependency is used by the Cloud Function validate the request and formulate the response.

  3. Create a file named config.json with the following contents:

    {
      "TWILIO_AUTH_TOKEN": "[YOUR_TWILIO_AUTH_TOKEN]"
    }
    

    Replace [YOUR_TWILIO_AUTH_TOKEN] with your Twilio Auth Token from step 5 of the Getting Started with Twilio section.

Writing the Function Code

Create a file named index.js with the following contents:

'use strict';

const twilio = require('twilio');
const config = require('./config.json');

const MessagingResponse = twilio.twiml.MessagingResponse;

const projectId = process.env.GCLOUD_PROJECT;
const region = 'us-central1';

exports.reply = (req, res) => {
  let isValid = true;

  // Only validate that requests came from Twilio when the function has been
  // deployed to production.
  if (process.env.NODE_ENV === 'production') {
    isValid = twilio.validateExpressRequest(req, config.TWILIO_AUTH_TOKEN, {
      url: `https://${region}-${projectId}.cloudfunctions.net/reply`
    });
  }

  // Halt early if the request was not sent from Twilio
  if (!isValid) {
    res
      .type('text/plain')
      .status(403)
      .send('Twilio Request Validation Failed.')
      .end();
    return;
  }

  // Prepare a response to the SMS message
  const response = new MessagingResponse();

  // Add text to the response
  response.message('Hello from Google Cloud Functions!');

  // Send the response
  res
    .status(200)
    .type('text/xml')
    .end(response.toString());
};

Notice the named export reply—this is the function that will be executed whenever an SMS message is sent to your Twilio number.

The reply function does the following:

  1. Validates that the request came from Twilio.
  2. Sends a reply to the SMS message.

Deploying and Testing the Cloud Function

  1. Read about deploying Cloud Functions.
  2. Run the following to deploy the function:

    gcloud beta functions deploy reply --trigger-http --stage-bucket [YOUR_STAGE_BUCKET]
    

    Replacing [YOUR_STAGE_BUCKET] with your Cloud Functions staging bucket.

  3. Send an SMS message to your Twilio phone number and observe the response you receive from the Cloud Function.

To view the logs for the Cloud Function, run the following:

gcloud beta functions logs view reply
See more by @{[ TutorialCtrl.tutorial.github_username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.