Looking for a new way to engage your customers? Why not give an SMS subscription service a try? In recent months, more and more businesses have begun using SMS subscription services than ever before. Why you ask? Well, there's a few reasons.
First, SMS subscription services facilitate a direct line of communication to your customers. Instead of having them seek information out, your company can use SMS subscriptions to deliver information straight to them. Second, SMS subscription services establish a frictionless user experience. That is to say, they require little to no effort on the user's end to effectively utilize the service. Third, SMS subscription services work as informative reminders. In other words, customers can use them to receive the information they need, when they need it.
As a result of their effectiveness, members of our developer community have inquired about how to build these SMS subscription services. Fortunately, they're incredibly easy to build with TelAPI!
The following two-part blog post demonstrates how a simple SMS subscription service can be built with Ruby on Rails and TelAPI's Ruby helper library. Part 1 walks through the process of building an application with TelAPI that can be used to process incoming SMS messages and create/delete subscribers. Part 2 will explain how scheduled tasks can be used to generate requests to TelAPI's REST API in order to send SMS messages to all of your service's subscribers.
OK, so first things first. Go ahead and create a new Ruby on Rails application.
$ rails new sms-subscription
Upon doing so, open up your Gemfile and add the following gems to it:
Then go ahead and perform a 'bundle install' to ensure all dependencies from the Gemfile will be available in the application.
$ bundle install
Next, let's go ahead and create a 'Texts' controller with a 'subscribe' method that will be used to process incoming SMS subscriptions.
$ rails generate controller Texts subscribe
After our controller has been generated, let's go ahead and assign our TelAPI number to a variable in the 'subscribe' method. Also, while we are at it, let's assign numbers from incoming SMS messages to the 'from_number' variable. That way, when our TelAPI number receives an inbound SMS message, the phone number the message came from will be assigned to a variable. In addition, we'll assign the body of the incoming SMS message to a variable as well.
Next, create an if statement to handle incoming user subscriptions. In the case that a user texts "Subscribe" (or "Unsubscribe") in the body of a text message, we want to be sure our service recognizes the request and handles it appropriately. We also want to account for incoming messages in which the text's body is neither "Subscribe" or "Unsubscribe". The following if/else statement addresses this:
Once we have ported our code over to our hosting solution of choice (in this example, Heroku), we must then configure our TelAPI SMS Request URL to retrieve and execute our code above. This can be done through the TelAPI 'Manage Numbers' dashboard.
Now that we have our SMS Request URL configured properly, it's time to address how to store users in our database. First, we'll use scaffolding to generate a User model, User controller and all corresponding User views. Our User class will have only one attribute: number (i.e. the phone number a User subscribes from).
$ rails generate scaffold User number:string
Don't forget, we need to perform a database migration to alter our existing database and add the new User table to it.
$ heroku rake db:migrate
Now that we have a User class and a Users table in our database, we can then create instances of User when our service receives text messages with "Subscribe" in the body. To do this, we must include the following code after our initial if statement:
Next, we must account for individuals that would like to unsubscribe from our service. To do this, we must add the following line of code after our elsif statement:
Therefore, the code from the entire TextsController looks a little something like this:
Now, we have a working SMS subscription service in which users can 'subscribe' and 'unsubscribe' in order to be added to or removed from our database.
As you can see, creating the initial subscription component of this service was done very easily with TelAPI. In part 2 of this blog post, we will learn how to use a scheduled process to make requests to TelAPI's REST API and send outbound SMS messages to all of our subscribers.
If you have an questions regarding this blog post, please reach out to TelAPI's developer evangelist, Doug Crescenzi. He'll be sure to help you with them!