Sending SMS and Making Calls from your Website Programatically - Part 1

There may be many ways to send/receive SMS or to make phone calls programatically. Here, I'm explaining the twilio method.  I assume the reader to have knowledge on PHP, web designing and hosting.

This guide is divided into two parts, part 1 covers outgoing calls and messages and part 2 covers handling of incoming calls and messages.


Using Twilio

Twilio is a Cloud Service which enables a programmer to send/receive messages, make/receive phone calls etc, programatically. Steps for setting up twilio are as follows.
  1. Sign Up/Register with twilio (Its FREE!!). You may need to verify your account by typing in a verification     code which you will receive via SMS. After this step, you will get your first twilio phone number.
  2. Now you will be taken to a test drive area, from where you may perform a test drive of the various features offered by twilio. You may do an optional test drive and then proceed to your account.
  3. On the Dashboard Section, you will see an Account SID and AUTH Token(This one will be masked by dots, click on the lock icon to reveal the token). You will need these two for making calls and sending messages.
  4. Install Twilio library to your web server or download the php source file from here and include the file into your code, as follows
    require_once '/path/to/twilio-php/Services/Twilio.php';
  5. Copy these two to your PHP code as follows
    $twilio_sid = "your twilio sid here";
    $twilio_token = "your twilio auth token";
    $twilio_number = "your twilio number here";

  6. Initialize twilio services as follows
    $client = new Services_Twilio($twilio_sid, $twilio_token);
  7. Now, you need to create an xml file(termed TwiML) for twilio, which gives twilio directions for what to do. An example is given below 
  8. <response>
          <say voice="woman">Hello World</say>
    </response>
    A complete reference to the various options available may be found here
  9. Making a Call : the following php code segments makes a call
    $toPhone = +911234567890;
    $call = $client->account->calls->create("$twilio_number", "$toPhone", "path to xml file", array());
    echo $call->sid;
    The statement performs a call and provides us a SID which can uniquely identify the call.
  10. Sending an SMS: This is similar to making a call
    $message = $client->account->sms_messages->create("$twilio_number", "$toPhone", "Hello World", array());
    echo $message->sid;
    This also returns a unique SID
  11. Additional Parameters may be passed to create method via 'array()'. Like, array("Parameter_1" => "value","Parameter_2" => "value") and so on. More references can be found here
    1. Calls
    2. SMS
    Example:
    $call = $client->account->calls->create("$twilio_number", "$toPhone", "path/to/xml", array("Record" => 'true',"StatusCallback" => "http://domain.com/landingPage.php"));
The Part 2 of this post will detail on how to handle incoming calls and messages via Twilio.

No comments:

Post a Comment