Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created March 8, 2010 22:41
Show Gist options
  • Save mheadd/325890 to your computer and use it in GitHub Desktop.
Save mheadd/325890 to your computer and use it in GitHub Desktop.
{
"tropo": [
{
"say": {
"value": "Hello. Welcome to my demo application."
}
},
{
"ask": {
"attempts": 3,
"bargein": true,
"choices": {
"value": "[5 DIGITS]",
"mode": "dtmf"
},
"requied": true,
"say": {
"value": "Please enter your 5 digit zip code"
},
"timeout": 5
}
},
{
"say": {
"value": "Thank you. Goodbye."
}
},
{
"hangup": {
}
}
]
}
<?php
require('tropo-php-helper-classes.php');
// Set up our prompts and input collection options.
$greeting = new Say("Hello. Welcome to my demo application.");
$choices = new Choices("[5 DIGITS]", "dtmf");
$zipCodePrompt = new Say("Please enter your 5 digit zip code");
$ask = new Ask(3, true, $choices, NULL, NULL, true, $zipCodePrompt, 5);
$goodbye = new Say("Thank you. Goodbye.");
// Create a new instante of the Tropo class and add prompts and options.
$tropo = new Tropo();
$tropo->Say($greeting);;
$tropo->Ask($ask);
$tropo->Say($goodbye);
$tropo->Hangup();
echo $tropo;
?>
<?php
/**
* The main Tropo class
*
*/
class Tropo {
public $tropo = array();
public function Ask(Ask $ask) {
$this->ask = sprintf($ask);
}
public function Conference(Conference $conference) {
$this->conference = sprintf($conference);
}
public function Hangup() {
$hangup = new Hangup();
$this->hangup = sprintf($hangup);
}
public function On(On $on) {
$this->on = sprintf($on);
}
public function Record(Record $record) {
$this->record = sprintf($record);
}
public function Redirect(Redirect $redirect) {
$this->redirect = sprintf($redirect);
}
public function Reject() {
$reject = new Reject();
$this->reject = sprintf($reject);
}
public function Result(Result $result) {
$this->result = sprintf($result);
}
public function Say(Say $say) {
$this->say = sprintf($say);
}
public function StartRecording(Record $startRecording) {
$this->startRecording = sprintf($startRecording);
}
public function StopRecording() {
$stopRecording = new stopRecording();
$this->stopRecording = sprintf($stopRecording);
}
public function Transfer(Transfer $transfer) {
$this->transfer = sprintf($transfer);
}
public function __set($name, $value) {
array_push($this->tropo, array($name => $value));
}
public function __toString() {
return str_replace(array("\\", "\"{", "}\""), array("", "{", "}"), json_encode($this));
}
}
/**
* Base classes for different Tropo actions.
*
*/
class BaseClass {
private function __set($attribute, $value) {
$this->$attribute= $value;
}
}
class EmptyBaseClass {
public function __toString() {
return json_encode($this);
}
}
/**
* Action classes. Each object represents a Tropo action.
*
*/
class Ask extends BaseClass {
private $_attempts;
private $_bargein;
private $_choices;
private $_minConfidence;
private $_name;
private $_requied;
private $_say;
private $_timeout;
public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $requied=NULL, Say $say=NULL, $timeout=NULL) {
$this->_attempts = $attempts;
$this->_bargein = $bargein;
$this->_choices = sprintf($choices);
$this->_minConfidence = $minConfidence;
$this->_name = $name;
$this->_requied = $requied;
$this->_say = sprintf($say);
$this->_timeout = $timeout;
}
public function __toString() {
if(isset($this->_attempts)) { $this->attempts = $this->_attempts; }
if(isset($this->_bargein)) { $this->bargein = $this->_bargein; }
if(isset($this->_choices)) { $this->choices = $this->_choices; }
if(isset($this->_minConfidence)) { $this->minConfidence = $this->_minConfidence; }
if(isset($this->_name)) { $this->name = $this->_name; }
if(isset($this->_requied)) { $this->requied = $this->_requied; }
if(isset($this->_say)) { $this->say = $this->_say; }
if(isset($this->_timeout)) { $this->timeout = $this->_timeout; }
return json_encode($this);
}
}
class Choices extends BaseClass {
private $_value;
private $_mode;
public function __construct($value, $mode=NULL) {
$this->_value = $value;
$this->_mode = $mode;
}
public function __toString() {
$this->value = $this->_value;
if(isset($this->_mode)) { $this->mode = $this->_mode; }
return json_encode($this);
}
}
class Conference extends BaseClass {
private $_id;
private $_mute;
private $_name;
private $_on;
private $_playTones;
private $_required;
private $_terminator;
public function __construct($id=NULL, $mute=NULL, $name=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL) {
$this->_id = $id;
$this->_mute = $mute;
$this->_name = $name;
$this->_on = sprintf($on);
$this->_playTones = $playTones;
$this->_required = $required;
$this->_termiantor = $terminator;
}
public function __toString() {
if(isset($this->_id)) { $this->id = $this->_id; }
if(isset($this->_mute)) { $this->mute = $this->_mute; }
if(isset($this->_name)) { $this->name = $this->_name; }
if(isset($this->_on)) { $this->on = $this->_on; }
if(isset($this->_playTones)) { $this->playTones = $this->_playTones; }
if(isset($this->_required)) { $this->required = $this->_required; }
if(isset($this->_terminator)) { $this->terminator = $this->_terminator; }
return json_encode($this);
}
}
class Hangup extends EmptyBaseClass { }
class On extends BaseClass {
private $_event;
private $_next;
private $_say;
public function __construct($event=NULL, $next=NULL, Say $say=NULL) {
$this->_event = $event;
$this->_next = $next;
$this->_say = sprintf($say);
}
public function __toString() {
if(isset($this->_event)) { $this->event = $this->_event; }
if(isset($this->_next)) { $this->next = $this->_next; }
if(isset($this->_say)) { $this->say = $this->_say; }
return json_encode($this);
}
}
class Record extends BaseClass {
}
class Redirect extends BaseClass {
}
class Reject extends EmptyBaseClass { }
class Result Extends BaseClass {
}
class Say extends BaseClass {
private $_value;
private $_as;
private $_event;
private $_format;
public function __construct($value, SayAs $as=NULL, Event $event=NULL, Format $format=NULL) {
$this->_value = $value;
$this->_as = $as;
$this->_event = $event;
if(is_object($format)) {
if(isset($format->date)) {
$this->_format = $format->date;
}
else {
$this->_format = $format->duration;
}
} else {
$this->_format = $format;
}
}
public function __toString() {
$this->value = $this->_value;
if(isset($this->_as)) { $this->as = $this->_as; }
if(isset($this->_event)) { $this->event = $this->_event; }
if(isset($this->_format)) { $this->format = $this->_format; }
return json_encode($this);
}
}
class Session extends BaseClass {
}
class StartRecording extends BaseClass {
}
class StopRecording extends EmptyBaseClass { }
class Transfer extends BaseClass {
}
/**
* Attribute classes. These object are used with action classes to set option values.
*
*/
class Date {
public static $monthDayYear = "mdy";
public static $dayMonthYear = "dmy";
}
class Duration {
public static $hoursMinutesSeconds = "hms";
public static $hoursMinutes = "hm";
}
class Event {
public static $continue = 'continue';
public static $incomplete = 'incomplete';
public static $error = 'error';
public static $hangup = 'hangup';
}
class Format {
public $date;
public $duration;
public static $ordinal = "ordinal";
public static $digits = "digits";
public function __construct($date=NULL, $duration=NULL) {
$this->date = $date;
$this->duration = $duration;
}
}
class SayAs {
public static $date = "DATE";
public static $digits = "DIGITS";
public static $number = "NUMBER";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment