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 one 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 system for SMS customer support using Vonage's APIs and libraries.
To do this:
- Create a basic web app - create a basic web application with a link to open a support ticket.
- Purchase a number - purchase a Vonage phone number to send SMS and receive inbound SMS
- Process an inbound SMS - accept and process inbound SMS received from the customer
- Send an SMS reply with a ticket number - reply with a new ticket number when a ticket is opened
Prerequisites
In order for this tutorial to work you will need:
- A Vonage account
- A publicly accessible Web server so Vonage can make webhook requests to your app. If you're developing locally you should use a tool such as ngrok
- The source code for this tutorial from https://github.com/Nexmo/ruby-sms-customer-support/
- All US based customers must register a brand and campaign to comply with 10 DLC guidelines.
A Basic Web application
For this tutorial start off with a 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.
rails new customer-support
cd customer-support
rake db:create db:migrate
The page will be at the root of our application and will 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['VONAGE_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 server
Purchase a phone number
Before the app can receive an SMS a Vonage phone number has to be rented. Phone numbers can be purchased from the dashboard or directly from the command line with the Vonage CLI.
> vonage numbers:buy US --number=15555555555
Number 15555555555 purchased.
Finally, Vonage 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 Vonage CLI.
> vonage number:sms 15555555555 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 Vonage will make a call to your server to confirm it's available.
Process an Inbound SMS
When the customer sends their SMS it will be received by Vonage via the mobile carrier network. Vonage 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 Vonage server SDK to your project.
Gemfile
gem 'vonage'
gem 'dotenv-rails'
Note: To initialize the Server SDK 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 send an SMS. Only send a response if this was the first message on this ticket.
def send_response
return if ticket.messages.count > 1
client = Vonage::Client.new
result = client.sms.send(
from: ENV['VONAGE_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 code snippets you now have an SMS customer support solution using the Vonage SMS API.
Get the Code
All the code for this tutorial and more is available on GitHub.