/
realtime.php
135 lines (113 loc) · 3.96 KB
/
realtime.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
<?php
/**
* Simple Real-time example
* @see http://thecallr.com/docs/real-time/
*/
/* Composer */
// require 'vendor/autoload.php';
/* Not using Composer? Uncomment below */
require '../src/THECALLR/Realtime/Server.php';
require '../src/THECALLR/Realtime/Request.php';
require '../src/THECALLR/Realtime/Response.php';
require '../src/THECALLR/Realtime/CallFlow.php';
require '../src/THECALLR/Realtime/Command.php';
/* Classes used here */
use \THECALLR\Realtime\Server;
use \THECALLR\Realtime\Request;
use \THECALLR\Realtime\Command;
use \THECALLR\Realtime\CallFlow;
/* Recommended */
date_default_timezone_set('UTC');
set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});
/* Create a new call flow */
$flow = new CallFlow;
/* When a call is inbound (a DID is being called),
this callback will be called */
$flow->onInboundCall(function (Request $request) {
// your code
/* your callback **MUST** return a label to execute */
return 'ask_age';
});
/* When an outbound call is answered,
this callback will be called */
$flow->onOutboundCall(function (Request $request) {
// your code
/* label to execute */
return 'ask_age';
});
/* Define a label with a command and its parameters,
along with the **async** result callback */
$flow->define(
'ask_age',
'read',
['media_id' => 'TTS|TTS_EN-GB_SERENA|Hello there. How old are you?',
'max_digits' => 3,
'attempts' => 3,
'timeout_ms' => 5000],
function ($result, $error, Request $request) use ($flow) {
// your code
/* if the 'read' command succeeds, the result will be in $result, and $error will be null.
if it fails, the error will be in $error, and result will be null */
/* here we store some variables in the call
they can be used in subsequent labels */
$flow->variables->result = $result;
$flow->variables->error = $error;
$flow->variables->age = $result;
/* label to execute */
return 'say_age';
}
);
/* This label is using the $age variable store above */
$flow->define(
'say_age',
'play',
['media_id' => 'TTS|TTS_EN-GB_SERENA|You are {age} years old.'],
function ($result, $error, Request $request) {
/* special label to hangup */
return '_hangup';
}
);
// /* This label is using the $age variable store above */
// $flow->define('say_age2',
// new Command('play',
// ['media_id' => 'TTS|TTS_EN-GB_SERENA|You are {age} years old.']),
// function ($result, $error, Request $request) {
// /* special label to hangup */
// return '_hangup';
// });
// /* This label is using the $age variable store above */
// $flow->define('say_age3',
// new Command\Play('TTS|TTS_EN-GB_SERENA|You are {age} years old.'),
// function ($result, $error, Request $request) {
// /* special label to hangup */
// return '_hangup';
// });
/* This label is using the $age variable store above */
$flow->define(
'say_age4',
Command::Play('TTS|TTS_EN-GB_SERENA|You are {age} years old.'),
function ($result, $error, Request $request) {
/* special label to hangup */
return '_hangup';
}
);
/* Real-time Server */
$server = new Server;
/* Register a callback to receive raw input. Useful for debugging. */
$server->setRawInputHandler(function ($data) {
$data = date('c').' <<<< '.$data."\n";
file_put_contents('/tmp/RT_DEBUG', $data, FILE_APPEND);
});
/* Register a callback to receive raw output. Useful for debugging. */
$server->setRawOutputHandler(function ($data) {
$data = date('c').' >>>> '.$data."\n";
file_put_contents('/tmp/RT_DEBUG', $data, FILE_APPEND);
});
/* Match your call flow against a REALTIME10 app hash */
//$server->registerCallFlow('DEADBEEF', $flow);
/* Or any hash! */
$server->registerCallFlow('*', $flow);
/* Start */
$server->start();