Welcome to the grand Twilio, Meetup, API Web App Mashup!

Setup

Sample code at https://github.com/simeonf/twilio-sample

Twilio

https://www.twilio.com/try-twilio

Sign up for free account.

Twilio texts/calls you verification code.

Get assigned a trial #

API overview

Tonight we’re all about incoming calls.

Basic flow:

flow.png

But we can also make calls via the API and send/receive sms.

TwiML - Twilio Markup Language - xml dialect for talking to Twilio.

hello.php

Output TwiML with tags representing actions:

hello.php
    1: <?php
    2: header("content-type: text/xml");
    3: echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    4: ?>
    5: <Response>
    6: <Say>Hello World</Say>
    7: </Response>

metadata.php

Every request to your web app from Twilio includes metadata about the call:

metadata.php
    1: <?php
    2:
    3: function loggit($o){
    4:     $fh = fopen("./log.txt", "a");
    5:     if (flock($fh, LOCK_EX)) {  // exclusive lock
    6:         fwrite($fh, print_r($o, true)); // get text version of array, object, etc
    7:         fwrite($fh, "\n"); // Add a newline between entries
    8:         fflush($fh);
    9:         flock($fh, LOCK_UN);    // release the lock
   10:     }
   11:     fclose($fh);
   12: }
   13:
   14:
   15: loggit($_GET);
   16: header("content-type: text/xml");
   17: echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   18: ?>
   19: <Response>
   20: <Say>Hello World</Say>
   21: </Response>

metadata.php logging output

Array
(
    [AccountSid] => ACf19be4dd11ab1ac1065f73d9047d1e9d
    [ToZip] => 95202
    [FromState] => CA
    [Called] => +12093908913 # Free twilio phone #
    [FromCountry] => US
    [CallerCountry] => US
    [CalledZip] => 95202
    [Direction] => inbound
    [FromCity] => MODESTO
    [CalledCountry] => US
    [CallerState] => CA
    [CallSid] => CAe40733dbabd7eccebc04fbd69aea7611
    [CalledState] => CA
    [From] => +12099999999 # was my home phone number
    [CallerZip] => 95358
    [FromZip] => 95358
    [CallStatus] => ringing
    [ToCity] => STOCKTON
    [ToState] => CA
    [To] => +12093908913
    [ToCountry] => US
    [CallerCity] => MODESTO
    [ApiVersion] => 2010-04-01
    [Caller] => +12099999999 # Also home phone #
    [CalledCity] => STOCKTON
)

secrets.php

Call to save or hear a secret number.

secrets.php
    1: <?php
    2: require("loggit.php");
    3: $phone = $_GET['Caller']; // phone number of caller
    4:
    5: header("content-type: text/xml");
    6: echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    7:
    8: if(!isset($_GET['ACTION']) or $_GET['ACTION'] == 'index'): // front page of our app
    9: ?><Response>
   10:     <Gather timeout="10" finishOnKey="#" method="GET" action="secrets.php?ACTION=menu">
   11:      <Say>Please choose from one of the following menu options followed by the #:</Say>
   12:      <Pause />
   13:      <Say>Press 1 to set your secret.</Say>
   14:      <Say>Press 2 to retrieve your secret.</Say>
   15:     </Gather>
   16:    </Response>
   17: <?php
   18: elseif($_GET['ACTION'] == 'menu' and $_GET['Digits'] == '1'): // input your secret
   19: ?>
   20:   <Response>
   21:     <Gather timeout='10' finishOnKey='#' method="GET" action='secrets.php?ACTION=save'>
   22:       <Say>Please enter a number, followed by the #</Say>
   23:     </Gather>
   24:   </Response>
   25: <?php
   26: elseif($_GET['ACTION'] == 'menu' and $_GET['Digits'] == '2'): // get your secret and go back to beginning
   27:     $obj = get_data();
   28:     $secret = $obj[$phone];
   29: ?>
   30:     <Response><Say>Your number was <?php echo $secret; ?>!</Say>
   31:         <Redirect method="GET">secrets.php?ACTION=index</Redirect>
   32:     </Response>
   33: <?php
   34: elseif($_GET['ACTION'] == 'save'):  // set secret and redirect to beginning
   35:     $obj = get_data();
   36:     $obj[$phone] = $_GET['Digits'];
   37:     save_data($obj);
   38: ?>
   39:     <Response>
   40:         <Redirect method="GET">secrets.php?ACTION=index</Redirect>
   41:     </Response>
   42: <?php
   43: endif;
   44: ?>

Let’s test this out!

Meetup API

See http://www.meetup.com/meetup_api/ for documentation

Members

member.php
    1: <?php
    2: function get_url($url) {
    3:     $ch = curl_init();
    4:     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    5:     curl_setopt($ch, CURLOPT_HEADER, false);
    6:     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    7:     curl_setopt($ch, CURLOPT_URL, $url);
    8:     curl_setopt($ch, CURLOPT_REFERER, $url);
    9:     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   10:     $result = curl_exec($ch);
   11:     curl_close($ch);
   12:     return $result;
   13: }
   14:     // Go to the meetup.com account page to see member id.
   15:     $id = "8473295";
   16:     // Go to http://www.meetup.com/meetup_api/key/ for your API key
   17:     $key = "33505274b224c38287a213473374a27";
   18:     $url = "https://api.meetup.com/2/member/$id?key=$key";
   19:     echo get_url($url);
   20:
   21: 

Get back something like:

{"lon":-121.01000213623047,
 "link":"http:\/\/www.meetup.com\/members\/8473295",
 "state":"CA",
 ... SNIP ...
 "photo":{"photo_link":"http:\/\/photos4.meetupstatic.com\/photos\/member\/4\/4\/b\/2\/member_6317586.jpeg",
 "thumb_link":"http:\/\/photos4.meetupstatic.com\/photos\/member\/4\/4\/b\/2\/thumb_6317586.jpeg",
 "photo_id":6317586},
 "city":"Modesto",
 "country":"us",
 "name":"Simeon Franklin",
 "other_services":{"twitter":{"identifier":"@simeonfranklin"},
 "linkedin":{"identifier":"http:\/\/www.linkedin.com\/in\/simeonfranklin"}},

 }

Groups

groups.php
    1: <?php
    2:     require("./url.php");
    3:     // Go to the meetup.com account page to see member id.
    4:     $id = "8473295";
    5:     // Go to http://www.meetup.com/meetup_api/key/ for your API key
    6:     $key = "33505274b224c38287a213473374a27";
    7:     // see http://www.meetup.com/meetup_api/docs/2/groups/
    8:     $url = "https://api.meetup.com/2/groups?key=$key&member_id=$id";
    9:     $json = get_url($url);
   10:     $json = json_decode($json);
   11:     foreach($json->results as $record){
   12:         echo $record->id . ": " . $record->name . "<br>";
   13:     }
   14:
   15: 

Events

events.php
<?php
    require("./url.php");
    // Member ID here, got it by using API on myself to see what Groups I'm part of
    $id = "3128712";
    // Go to http://www.meetup.com/meetup_api/key/ for your API key
    $key = "33505274b224c38287a213473374a27";
    // see http://www.meetup.com/meetup_api/docs/2/groups/
    $url = "https://api.meetup.com/2/events?key=$key&group_id=$id";
    $json = get_url($url);
    $json = json_decode($json);
    foreach($json->results as $record){
        $time = ($record->time + $record->utc_offset)/1000; //micro to seconds
        echo $record->id . ": " . $record->name . " on " . date("F jS Y", $time) . "<br>";
    }

The Project!

Choose your language, create your Twilio/Meetup accounts as necessary, and write an app!

Calling your phone number should:

/

#