-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwiml.php
240 lines (218 loc) · 6.75 KB
/
twiml.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/* State Types */
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// We use the number index to move sequentially through the list of the numbers we'll be dialing
define('DIAL_NUMBER_INDEX', 'dialNumberIndex');
define('DIAL_ACTION', 'dialAction');
define('DIAL_COOKIE', 'state-'.AppletInstance::getInstanceId());
/* States */
define('DIAL_STATE_DIAL', 'dialStateDial');
define('DIAL_STATE_NO_ANSWER', 'dialStateNoAnswer');
define('DIAL_STATE_RECORDING', 'dialStateRecording');
define('DIAL_STATE_HANGUP', 'dialStateHangup');
$response = new Response();
// Default State
$state = array();
$state[DIAL_ACTION] = DIAL_STATE_DIAL;
$state[DIAL_NUMBER_INDEX] = 0;
$version = AppletInstance::getValue('version', null);
/* Get current instance */
$dial_whom_selector = AppletInstance::getValue('dial-whom-selector');
$dial_whom_user_or_group = AppletInstance::getUserGroupPickerValue('dial-whom-user-or-group');
$dial_whom_number = AppletInstance::getValue('dial-whom-number');
$dial_timeout_number = AppletInstance::getValue('dial-timeout-number');//added
$dial_callerId = AppletInstance::getValue('dial-callerId'); //added
$dial_recordwhole = AppletInstance::getValue('dial-recordwhole'); //added
$no_answer_action = AppletInstance::getValue('no-answer-action', 'hangup');
$no_answer_group_voicemail = AppletInstance::getAudioSpeechPickerValue('no-answer-group-voicemail');
$no_answer_redirect = AppletInstance::getDropZoneUrl('no-answer-redirect');
$no_answer_redirect_number = AppletInstance::getDropZoneUrl('no-answer-redirect-number');
$numbers = array();
$voicemail = null;
if ($dial_whom_selector === 'user-or-group')
{
$dial_whom_instance = null;
if(is_object($dial_whom_user_or_group))
{
$dial_whom_instance = get_class($dial_whom_user_or_group);
}
switch($dial_whom_instance)
{
case 'VBX_User':
foreach($dial_whom_user_or_group->devices as $device)
{
if($device->is_active == 1)
$numbers[] = $device->value;
}
$voicemail = $dial_whom_user_or_group->voicemail;
break;
case 'VBX_Group':
foreach($dial_whom_user_or_group->users as $user)
{
$user = VBX_User::get($user->user_id);
foreach($user->devices as $device)
{
if($device->is_active == 1)
$numbers[] = $device->value;
}
}
$voicemail = $no_answer_group_voicemail;
break;
default:
$response->addSay('Missing user or group to dial');
break;
}
}
else if ($dial_whom_selector === 'number')
{
$numbers[] = $dial_whom_number;
$voicemail = null;
}
else
{
error_log("Unexpected dial-whom-selector value of '$dial_whom_selector'");
}
/* Grab current state of applet */
if(isset($_COOKIE[DIAL_COOKIE]))
{
$stateString = str_replace(', $Version=0', '', $_COOKIE[DIAL_COOKIE]);
$state = json_decode($stateString, true);
if(is_object($state))
{
$state = get_object_vars($state);
}
}
$dial_status = isset($_REQUEST['DialStatus'])? $_REQUEST['DialStatus'] : null;
if($dial_status)
{
switch($dial_status)
{
case 'answered':
$state[DIAL_ACTION] = DIAL_STATE_HANGUP;
break;
default:
break;
}
}
if($dial_callerId == 'called'){
$dial_callerId = isset($_REQUEST['Called'])? $_REQUEST['Called'] : null;
}
// This loop exists only so that we can quickly make state transitions by
// setting a new DIAL_ACTION and jumping to the top of the loop.
$keepLooping = true;
while($keepLooping)
{
// By default we'll only go once through the loop.
$keepLooping = false;
switch($state[DIAL_ACTION])
{
case DIAL_STATE_DIAL:
error_log('numbers left to try');
if ($state[DIAL_NUMBER_INDEX] < count($numbers))
{
// There are still more numbers left to try
$dial = $response->addDial( //added - important!
array(
'action' => current_url(),
'callerId' => $dial_callerId,
'timeout' => $dial_timeout_number,
//'record' => $dial_recordwhole,
)
);
if ($dial_whom_selector === 'user-or-group')
{
$name = null;
if ($dial_whom_user_or_group instanceof VBX_User)
{
$name = $dial_whom_user_or_group->first_name . " " . $dial_whom_user_or_group->last_name;
}
else if ($dial_whom_user_or_group instanceof VBX_Group)
{
$name = $dial_whom_user_or_group->name;
}
else
{
// In practice, this should never ever happen.
$name = "Unknown";
}
$dial->addNumber($numbers[$state[DIAL_NUMBER_INDEX]],
array(
'url' => site_url('twiml/whisper?name='.urlencode($name)),
)
);
}
else
{
// If we're just dialing an arbitrary number, we don't announce anything about
// who's calling because we don't really know anything.
$dial->addNumber($numbers[$state[DIAL_NUMBER_INDEX]]);
}
$state[DIAL_NUMBER_INDEX] = $state[DIAL_NUMBER_INDEX] + 1;
}
else
{
// We've dialed all the phone numbers and gotten no answer
$state[DIAL_ACTION] = DIAL_STATE_NO_ANSWER;
// Note that we'd like to go through the machine again with our new state
$keepLooping = true;
}
break;
case DIAL_STATE_HANGUP:
$response->addHangup();
break;
case DIAL_STATE_NO_ANSWER:
if ($dial_whom_selector == 'number')
{
if(empty($no_answer_redirect_number))
{
$response->addHangup();
}
$response->addRedirect($no_answer_redirect_number);
}
else
{
if ($no_answer_action === 'voicemail')
{
$response->append(AudioSpeechPickerWidget::getVerbForValue($voicemail, new Say("Please leave a message.")));
$response->addRecord(array(
'transcribe' => true,
'transcribeCallback' => site_url('twiml/transcribe'),
));
$state[DIAL_ACTION] = DIAL_STATE_RECORDING;
}
else if ($no_answer_action === 'redirect')
{
if(empty($no_answer_redirect))
{
$response->addHangup();
}
$response->addRedirect($no_answer_redirect);
}
else if ($no_answer_action === 'hangup')
{
$response->addHangup();
}
else
{
trigger_error("Unexpected no_answer_action");
}
}
break;
case DIAL_STATE_RECORDING:
if(isset($_REQUEST['testing']))
break;
OpenVBX::addVoiceMessage(
$dial_whom_user_or_group,
$_REQUEST['CallGuid'],
$_REQUEST['Caller'],
$_REQUEST['Called'],
$_REQUEST['RecordingUrl'],
$_REQUEST['Duration']
);
break;
}
}
setcookie(DIAL_COOKIE, json_encode($state), time() + (5 * 60));
$response->Respond();
?>