Building Your Startup: Preparing for Text Messaging

This tutorial is part of the Building Your Startup With PHP series on СodeHolder 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.

Building Your Startup: Preparing for Text Messaging

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.

Building Your Startup: Preparing for Text Messaging

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 Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

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 Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

The SMS Dashboard

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

Building Your Startup: Preparing for Text Messaging

Gathering Our Credentials

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

Building Your Startup: Preparing for Text Messaging

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 Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

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 Your Startup: Preparing for Text Messaging

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 Your Startup: Preparing for Text Messaging

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

Building Your Startup: Preparing for Text Messaging

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:

"filipajdacic/yii2-twilio": "dev-master"

Then, I updated the environment:

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing ezyang/htmlpurifier (v4.7.0)
  - Installing ezyang/htmlpurifier (v4.8.0)
    Downloading: 100%

  - Installing twilio/sdk (4.10.0)
    Downloading: 100%

  - Installing filipajdacic/yii2-twilio (dev-master 7d547a0)
    Cloning 7d547a0a47c9864f2e8b5fb5f43748fbd24fc4b1

Writing lock file
Generating autoload files

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:

twilio_sid = "ACxxxxXXxxxxxXXxxxXXxxxxxxxxxxxe1"
twilio_token = "d3xxxxXXxxxxxXXxxxXXxxxxxxxxxxx41"
twilio_number = "1206XXXYYYY"
# for next episode
#twilio_service_sid = "MxxxxXXxxxxxXXxxxXXxxxxxxxxxxxGf6"

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

return [
    'id' => 'mp-frontend',
    'name' => 'Meeting Planner',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontendcontrollers',
    'components' => [
        'Yii2Twilio' => [
          'class' => 'filipajdacicyiitwilioYiiTwilio',
          'account_sid' => $config['twilio_sid'],
          'auth_key' => $config['twilio_token'],
        'log' => [

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

return [
  'sms_number' => '1206XXXYYYY',
  'twilio_service_id' => 'MGXXXXXYYYYZZZZ11134446',
  'twilio_test_number' => '1-206-NNN-QQQQ',

Building an SMS Model

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

namespace commonmodels;
use Yii;
use yiibaseModel;
use frontendmodelsUserContact;

class Sms
  private $sms;
  private $service_id;
  private $mp_number;
  private $test_number;

   function __construct() {

     $this->sms = Yii::$app->Yii2Twilio->initTwilio();
     $this->mp_number = Yii::$app->params['sms_number'];
     $this->service_id = Yii::$app->params['twilio_service_id'];
     $this->test_number = Yii::$app->params['twilio_test_number'];

  public function transmit($user_id,$body='') {
    // to do - lookup usercontact to sms
    // see if they have a usercontact entry that accepts sms
    // transmit
    $to_number = $this->test_number;
    $to_number = $this->findUserNumber($user_id);
    if (!$to_number)
      return false;
    try {
        $message = $this->sms->account->messages->create(array(
            "From" => $this->mp_number,
            "To" => $to_number,   // Text this number
            'MessagingServiceSid' => $this->service_id,
            "Body" => $body,
    } catch (Services_Twilio_RestException $e) {
            echo $e->getMessage();

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:

$user_id = 1;
$s = new commonmodelsSms();
$s->transmit($user_id,'First test from Meeting Planner codebase!');

Here are the results:

Building Your Startup: Preparing for Text Messaging

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