Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

Sending SMS and Making Calls from your Website Programatically - Part 2 - Handling Incoming Calls and Messages

This post assumes that you have read the part 1 of this series, which covered making calls and sending messages programatically from twilio. This part will cover the handling of incoming messages and phone calls.


  1. Login to your twilio account and goto the 'Numbers' section. Here you can see all the numbers that you have registered with twilio and those you can use with twilio.
  2. Click on any of the listed numbers
  3. On the following page, you will see a textbox under Voice and another text box under Messaging section. Enter the URL to your page in this box so that twilio will invoke this page on receiving a call or message to the number you have selected.(Like a Landing URL or Call back reference). You may set the method to invoke your page(GET/POST), in the following drop down box.
  4. You may write the code to handle your call/message in this landing page. Try
    file_put_contents("test.txt",json_encode($_REQUEST));
    in your landing script so that you may see the various parameters passed to your page from twilio upon receiving a call or message, and you may use these parameters according to your logic.
  5. You may optionally echo an xml (twiml) formatted string for twilio to respond to the incoming call or message. For example,  
    $toPhone = $_REQUEST['From']; //sender's number
    $content = "Thank you for your response. Your sms response : ".$_REQUEST['Body'];
    //xml response
    echo '<?xml version="1.0" encoding="UTF-8"?><Response><Sms from="$twilio_number" to="$toPhone">'.$content.'</Sms></Response>';
    will acknowledge the sms sender with another message. 
PS: If you are having a trial account with twilio, you will only have limited permissions. You can call/text the numbers verified with twilio only.

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.