SMS Customer Support

The general availability of SMS makes it a versatile solution for customer support. Phone numbers can be printed, read out, and put on websites, allowing anyone online or offline to engage with your business.

Providing customer support over SMS is an easy way to provide a full two-way communication system to anybody with a phone connected to a mobile network.

In this tutorial

You will build a simple system for SMS customer support using Nexmo's APIs and libraries.

To do this:

Prerequisites

In order for this tutorial to work you will need:

A Basic Web application

For this tutorial start off with a simple web application with one page. The user will be able to click on a link to open their SMS app and request support. Your app will collect the inbound SMS and open a new ticket. Finally, the app will reply with a new SMS to the user confirming their ticket number.

Start by creating a basic app.

Creating a basic Rails application
rails new customer-support
cd customer-support
rake db:create db:migrate

The page will be at the root of our application and simply provide a link to your SMS app with some prefilled text.

Adding a first page
rails g controller pages index
app/views/pages/index.html.erb
<h1>ACME Support</h1>

<p>
  <a href="sms://<%= ENV['NEXMO_NUMBER'] %>?body=Hi ACME, I'd like some help with: " class='button'>
    Get support via SMS
  </a>
</p>

With this in place the server can be started.

Starting the server
rails s

Purchase a phone number

Before the app can receive an SMS a Nexmo phone number has to be rented. Phone numbers can be purchased from the dashboard or directly from the command line with the Nexmo CLI.

> nexmo number:buy --country_code US --confirm
Number purchased: 12182615555

Finally, Nexmo must be informed of the webhook endpoint to make an HTTP request to when an inbound SMS is received. This can be done using the dashboard or the Nexmo CLI.

> nexmo link:sms 12182615555 http://[your.domain.com]/support
Number updated

Note: Ensure your server is running and publicly available before trying to set up a new callback URL for webhooks. When you are setting up a new webhook Nexmo will make a call to your server to confirm it's available.

Process an SMS

When the customer sends their SMS it will be received by Nexmo via the mobile carrier network. Nexmo will subsequently make a webhook to your application.

This webhook will contain the original text sent, the phone number the message came from, and a few more parameters. For more details see the Inbound Message documentation.

Your app should process the incoming webhook, extract the text and number, open a new ticket, or update an existing ticket. If this is a customer's first request the app should send a confirmation message back to the customer with their ticket number.

This is achieved by saving the incoming message and opening a new ticket if the number does not already have an open ticket.

Add a ticket and a message model
rails g controller support index
rails g model Ticket number
rails g model Message text ticket:references
rake db:migrate
app/controllers/support_controller.rb
class SupportController < ApplicationController
  def index
    save_message
    send_response
    render nothing: true
  end

  private

  def ticket
    @ticket ||= Ticket.where(
      number: params[:msisdn]
    ).first_or_create
  end

  def save_message
    message = Message.create(
      text: params[:text],
      ticket: ticket
    )
  end

Send an SMS reply with a ticket number

To send the confirmation to the customer's SMS, add the Nexmo library to your project.

Gemfile
gem 'nexmo'
gem 'dotenv-rails'

Note: To initialize the Nexmo client library you will need to pass it your API key and secret. We highly recommend that you do not store your API credentials in your code but to use environment variables instead.

With the library initialized the application can now make
an SMS Request. Only send a response if this was the first message on this ticket.

Add a ticket and a message model
  def send_response
    return if ticket.messages.count > 1

    client = Nexmo::Client.new
    result = client.send_message(
      from: ENV['NEXMO_NUMBER'],
      to: ticket.number,
      text: "Dear customer, your support" \
            "request has been registered. " \
            "Your ticket number is #{ticket.id}. " \
            "We intend to get back to any " \
            "support requests within 24h."
    )
  end

Conclusion

In this tutorial you've learned how to receive an SMS from a customer's phone and send an SMS reply to them. With these building blocks you now have an SMS customer support solution using the Nexmo SMS API.

Get the Code

All the code for this tutorial and more is available on GitHub.

Resources

Previous   Next