Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. PHP

Building Your Startup: Preparing for Text Messaging

This post is part of a series called Building Your Startup With PHP.
Building Your Startup: Increasing Security
Building Your Startup: Automatic Time-Zone Detection
Final product image
What You'll Be Creating

This tutorial is part of the Building Your Startup With PHP series on Envato Tuts+. In this series, I'm guiding you through launching a startup from concept to reality using my Meeting Planner app as a real-life example. Every step along the way, I'll release the Meeting Planner code as open-source examples you can learn from. I'll also address startup-related business issues as they arise.

Preparing for Text Messaging

When you're on the go and scheduled meeting times are approaching, receiving helpful text messages is often more useful than email reminders. I've always known that text (or SMS) messaging would play a useful part in Meeting Planner.

In today's episode, I'm going to walk you through the first half of my SMS configuration for the Meeting Planner application. Basically, I'll cover choosing an SMS provider, setting up the account, building the initial application infrastructure, and trial texting.

In the next episode, I'll build specific texting scenarios into the Meeting Planner application and walk you through those decisions and the coding that followed.

If you haven't yet, please try out Meeting Planner right now by scheduling your first meeting. Feel free to post feedback about your experience in the comments below. 

I do participate in the comment threads, but you can also reach me @reifman on Twitter. I'm always open to new feature ideas and topic suggestions for future tutorials.

As a reminder, all the code for Meeting Planner is written in the Yii2 Framework for PHP. If you'd like to learn more about Yii2, check out my parallel series Programming With Yii2. The more I build out Meeting Planner, the more impressed I am with Yii2 and their team of volunteers.

Choosing an SMS Provider

The easiest way to send text messages from your application is to subscribe to a service. Just as I use Mailgun for Meeting Planner's inbound and outbound email, I'll need an SMS provider to deliver text messages.

The two most prominent services I looked at were Twilio and Plivo. Both seemed like competent providers, but Twilio had broader services, richer documentation, and top-level user experience. 

Here's a screenshot from Twilio's SMS product page:

Building Startups Text and SMS - Twilio Home Page for SMS Features

Twilio offers such an array of services that you do have to dive in a bit to find SMS:

Building Startups Text and SMS - Twilio Product Offering Menu

Plivo also seemed like a good choice, but its website, documentation and API didn't seem to be quite as sophisticated as Twilio:

Building Startups Text and SMS - Plivo Home Page

However, Plivo is a lot cheaper than Twilio; specifically, it offers free inbound SMS:

Building Startups Text and SMS - Pricing Comparison Twilio vs Plivo

I decided to go with Twilio for my initial SMS implementation, but to modularize the features so I could easily switch them to a different provider.

I do have some concerns about the costs of texting in Meeting Planner as the audience scales. Will I offer SMS for free to all users even before there is a revenue stream or investors?

At the early alpha stage, this wasn't a top concern. Again, I'm still focused on delivering the best MVP I can for the beta release.

Getting Started With Twilio

Signing Up

Registering with Twilio is easy:

Building Startups Text and SMS - Twilio Sign Up

As a sophisticated communication services provider, they implement SMS verification in their registration process:

Building Startups Text and SMS - Twilio Verification

The SMS Dashboard

Once verified, you land on a friendly, well-designed dashboard:

Building Startups Text and SMS - Twilio SMS Dashboard

Gathering Our Credentials

First, I accessed the account ID and token from the API Credentials page:

Building Startups Text and SMS - Twilio SMS Dashboard

I made note of these for later integration with Meeting Planner.

Obtaining a Phone Number

Twilio provides you a phone number from which you can send SMS from your application:

Building Startups Text and SMS - Twilio Get Your Phone Number

I chose a number with a Seattle area code, where Meeting Planner is based:

Building Startups Text and SMS - Twilio Your New Phone Number is

Then, using Twilio, I sent my first test message:

Building Startups Text and SMS - Twilio Test Send a Message

The message arrived very quickly on my phone.

Thinking About Incoming Messages

Thinking beyond the MVP, incoming message capability would be great for Meeting Planner. For example, you could text that you're running late or need a convenient link to directions for your phone's navigation app. Currently, you have to click through to the web to the meeting invitation to do this.

Twilio offers a rich set of services for responding to inbound texts, including a texting markup language called TwiML.

Building Startups Text and SMS - Twilio Incoming Messages

For the moment, I'm not going to worry too much about inbound messages. However, anytime people text your Twilio number, you are charged; in other words, it's ripe for abuse.

Let's look at a couple of easy ways to manage costs.

Controlling Costs

For alpha and beta testing, I am limiting text support to North American phone numbers. This will keep costs a bit lower. Twilio offers a built-in way to filter by geography:

Building Startups Text and SMS - Controlling Geo Permissions for Managing Costs

Twilio also provides Messaging Services which you can configure to behave in specific ways tailored for your application, including blocking all SMS:

Building Startups Text and SMS

As you can see, they also see inbound text spam and abuse as a weakness in the general SMS platform (not theirs) which they are thoughtful about.

Integrating Twilio Into Meeting Planner

Next, I wanted to get the basic SMS functionality operating within Meeting Planner.

Finding a Yii Extension for Twilio

It turns out there are several available extensions for Twilio with the Yii Framework. I decided to install Filip Ajdačić's YII2 wrapper for Twilio PHP SDK (GitHub) because his name was the most unusual (just kidding, his extension seemed to be regularly maintained).

I added the extension to composer.json. As it's technically in development mode, this worked better than directly requiring the extension:

Then, I updated the environment:

I wasn't too worried about its development status because it's a relatively simple, straightforward extension that just activates the Twilio API.

Adding the Credentials

First, I added the keys from above to my initialization files:

Then, I added registration of the component in /frontend/config/main.php:

I also found it best to place a few of the variables in common\config\params-local.php for easier access across the application:

Building an SMS Model

Then I built an Sms.php model to use programmatically when texts were needed:

Initially, findUserNumber() was a stub, and transmit() would only send to a test number in params-local.php, my personal cell phone.

Here's some test code which I used to send my first message from Meeting Planner:

Here are the results:

Building Startups Text and SMS - My First Twilio Text

Note: yeah, I know I should charge my phone—but it's really the iPhone 6's poor battery life that's the problem.

So, that's how you get signed up for Twilio and implement basic functionality.

Looking Ahead

In the next episode, we'll explore actual integration of SMS with Meeting Planner. Here are some questions that will come up:

  • How will people provide their phone numbers for texting?
  • Which Meeting Planner features should use SMS for notifications and delivery?
  • How will people decide what they want to receive texts from Meeting Planner for?
  • Will Meeting Planner process inbound texts and respond to them?
  • How will I control SMS costs and prevent abuse during the MVP stage of the startup?
  • What would it take to switch to Plivo to lower costs?

As always, please stay tuned for more upcoming tutorials in the Building Your Startup With PHP series. As former Presidential candidate Donald Trump used to say, "We are going to win, win so much you'll get tired of winning. You'll say, 'Please Jeff, stop winning'. And I'll say, 'No, sorry, get comfortable, there will be more ongoing winning.'"

Have you scheduled a meeting via Meeting Planner yet? No? Go! Do it. Do it now! And as always, let me know what you think below or in the comments. I appreciate it. I can't keep winning without your help.

Related Links

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.