Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 184 lines (151 sloc) 5.762 kb
125edda @mheadd First commit.
authored
1 <?php
2
3 define("APPLICATION_PATH", "http://{your-app-host}/address-finder/index.php");
4 define("GRAMMAR_PATH", "http://{your-couchdb-host}/{your-couchdb-database}/_design/app/_list/grammar-by-zip/names?group=true&street_zip=");
5
6 // English prompts.
7 $english = array();
8 $english["getzip"] = 'Please enter your five digit zip code.';
9 $english["sorry"] = 'Sorry, I had trouble hearing you.';
10 $english["zipdtmf"] = 'Please enter your zip code using your touch tone key pad.';
11 $english["didyousay"] = 'Did you say, ';
12 $english["address"] = 'Please say your street address, beginning with your street number.';
13 $english["recording"] = 'Please say your full addrress, including your street nunber, street name and zip code. Press the pound key when finished.';
14 $english["gotaddress"] = 'I heard you say, ';
15 $english["correct"] = 'Is that correct?';
16 $english["thanks"] = 'Thank you.';
17 $english["sorry"] = 'Sorry, Lets try again.';
18 $english["goodbye"] = 'Goodbye.';
19
20 // Spanish prompts.
21 $spanish = array();
22 $spanish["getzip"] = 'Por favor, ingrese su c—digo postal cinco d’gitos.';
23 $spanish["sorry"] = 'Lo sentimos, no ten’a problemas para escuchar usted.';
24 $spanish["zipdtmf"] = 'Por favor, ingrese su c—digo postal utilizando la marcaci—n por tonos teclado.';
25 $spanish["didyousay"] = 'Ha dicho usted, ';
26 $spanish["address"] = 'Por favor diga su direcci—n, empezando por el nœmero de su calle.';
27 $spanish["recording"] = '';
28 $spanish["gotaddress"] = 'He oido que usted dice, ';
29 $spanish["correct"] = 'Es correcto?';
30 $spanish["thanks"] = 'Gracias.';
31 $spanish["sorry"] = 'Lo sentimos, Vamos a intentarlo de nuevo.';
32 $spanish["goodbye"] = 'Adios.';
33
34 // Set prompts , TTS and ASR settings.
35 $prompts = array("english" => $english, "spanish" => $spanish);
36 $prompt = $prompts["english"];
37 $tts_voice = 'Allison';
38 $asr_recognizer = 'en-us';
39
40 // Helper function to confirm a prompt.
41 function confirm($prompt) {
42 global $tts_voice, $asr_recognizer;
43 $response = ask($prompt, array("choices" => 'yes,no', "voice" => $tts_voice, "recognizer" => $asr_recognizer, "bargein" => false));
44 if($response->value == 'yes') {
45 _log("*** Caller confirmed input. ***");
46 return true;
47 }
48 else {
49 _log("*** Caller rejected input.");
50 return false;
51 }
52 }
53
54 // Helper function to make an HTTP request.
55 function makeHTTPCall($url) {
56
57 _log("*** URL: $url ***");
58
59 $ch = curl_init($url);
60 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
61
62 // Execute.
63 $output = curl_exec($ch);
64 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
65
66 // Return results.
67 if($code != '200') {
68 return false;
69 }
70 else {
71 return true;
72 }
73
74 }
75
76 // Create a call record.
77 function createRecord($sessionid, $callerid) {
78 $url = APPLICATION_PATH . "?uri=create&sessionid=$sessionid&callerid=$callerid";
79 return makeHTTPCall($url);
80 }
81
82 // Save a call record for a completed call.
83 function saveRecord($sessionid, $address, $zip) {
84 $address = urlencode($address);
85 $url = APPLICATION_PATH . "?uri=save&sessionid=$sessionid&address=$address&zip=$zip";
86 return makeHTTPCall($url);
87 }
88
89 // Get the caller's zip code.
90 function getZip() {
91
92 global $prompt, $tts_voice, $asr_recognizer;
93
94 $zip = ask($prompt["getzip"], array("choices" => '[5 DIGITS]', "voice" => $tts_voice, "recognizer" => $asr_recognizer, "bargein" => false));
95
96 // Caller entered a zip code.
97 if($zip->name == 'choice') {
98 _log("Confidence level: ". $zip->choice->confidence);
99
100 if($zip->choice->confidence < .40) {
101 say($prompt["sorry"], array("voice" => tts_voice));
102 $zip = ask($prompt["zipdtmf"], array("choices" => '[5 DIGITS]', "mode" => 'dtmf', "voice" => $tts_voice, "recognizer" => $asr_recognizer));
103 return $zip->value;
104 }
105 else if($zip->choice->confidence < .60) {
106 if(confirm($prompt["didyousay"] . implode(" ", str_split($zip->value)) . "?")) {
107 return $zip->value;
108 }
109 else {
110 $zip = ask($prompt["zipdtmf"], array("choices" => '[5 DIGITS]', "mode" => 'dtmf'));
111 return $zip->value;
112 }
113 }
114 else {
115 return $zip->value;
116 }
117 }
118 else {
119 recordAddress();
120 }
121
122 }
123
124 // Get the caller's address.
125 function getAddress($zip) {
126
127 global $prompt, $tts_voice, $asr_recognizer;
128
129 $grammar = GRAMMAR_PATH . $zip;
130
131 _log("*** Grammar URL: $grammar ***");
132
133 $address = ask($prompt["address"], array("choices" => $grammar, "timeout" => 30.0, "voice" => $tts_voice, "recognizer" => $asr_recognizer, "bargein" => false));
134 _log("*** Captured address: " . $address->value . " ***");
135 if(confirm($prompt["gotaddress"] . $address->value . ", " . $prompt["correct"] . "?")) {
136 return $address->value;
137 }
138 else {
139 say($prompt["sorry"], array("voice" => $tts_voice));
140 recordAddress();
141 }
142
143 }
144
145 // Record a caller's address.
146 function recordAddress() {
147 global $prompt, $tts_voice, $currentCall;
148 $recording_location = APPLICATION_PATH . "?uri=record&sessionid=" . $currentCall->sessionId;
149
150 $recording = record($prompt["recording"],
151 array( "voice" => $tts_voice,
152 "recordMethod" => "POST",
153 "recordFormat" => 'audio/mp3',
154 "recordURI" => $recording_location,
155 "beep" => true, "maxTime" => 60,
156 "terminator" => "#", "silenceTimeout" => 3.0)
157 );
158
159 say("I recorded your address as " . $recording->recordURI);
160 say($prompt["thanks"], array("voice" => $tts_voice, "bargein" => false));
161 hangup();
162
163 }
164
165 // Get call properties.
166 $sessionid = $currentCall->sessionId;
167 $callerid = $currentCall->callerID;
168
169 // Main call flow.
170 if(!createRecord($sessionid, $callerid)) {
171 say("Sorry, there was a problem. Please try your call again later.", array("voice" => $tts_voice, "bargein" => false));
172 hangup();
173 }
174 else {
175
176 $zip = getZip();
177 _log("Zip: $zip");
178 $address = getAddress($zip);
179 say($prompt["thanks"], array("voice" => $tts_voice, "bargein" => false));
180 saveRecord($sessionid, $address, $zip);
181 hangup();
182 }
183
184 ?>
Something went wrong with that request. Please try again.