-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
245 lines (214 loc) · 7.56 KB
/
index.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
241
242
243
244
245
<?php
/**
* In a real system, most of this information would be in configuration globals
* or server settings. This file would likely be in a view and the javascript
* possibly moved to its own file.
*
* Author: Tom Phillips
* Date: 3/2/2014
*/
include 'twilio-php-latest/Services/Twilio/Capability.php';
require 'twilio-config.php';
// This is the dev account info
$accountSid = TWILIO_ACCOUNT_SID;
$authToken = TWILIO_AUTH_TOKEN;
$appSid = TWILIO_APP_SID;
$clientName = CLIENT_NAME;
// get the Twilio Client name from the page request parameters, if given
if (isset($_REQUEST['client'])) {
$clientName = $_REQUEST['client'];
}
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming($clientName);
$token = $capability->generateToken();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test Twilio Phone</title>
<script type="text/javascript"
src="//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<link href="css/main.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
Twilio.Device.setup("<?php echo $token; ?>");
Twilio.Device.ready(function (device) {
$("#log").text("Ready.");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
$("#log").text("Successfully established call");
});
Twilio.Device.disconnect(function (conn) {
$("#log").text("Call ended");
});
Twilio.Device.incoming(function (conn) {
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
Twilio.Device.presence(function (pres) {
if (pres.available) {
// create an item for the client that became available
$("<li>", {id: pres.from, text: pres.from}).click(function () {
$("#number").val(pres.from);
call();
}).prependTo("#people");
}
else {
// find the item by client name and remove it
$("#" + pres.from).remove();
}
});
function call() {
// get the phone number or client to connect the call to
params = {"PhoneNumber": $("#number").val()};
Twilio.Device.connect(params);
}
function hangup() {
Twilio.Device.disconnectAll();
}
/**
* Handle input via the .dialer keypad.
*/
function addNumber(thisDiv) {
var phoneNum;
if (thisDiv.id != 'star' && thisDiv.id != 'pound') {
//Proceed if string is less than 10 digits, or less than 11 digits if it starts with a 1.
if ( ($("#number").val().length < 10) || ( ($("#number").val().charAt(0) == '1') && ($("#number").val().length < 11))) {
phoneNum = $("#number").val() + thisDiv.id;
$("#number").val(phoneNum);
}
}
}
/**
* Toggles the text messaging area which includes the text message send button.
*/
function showTextingArea() {
var newLinkText;
$("#textMessageArea").toggle();
if ($("#textLink").text() == "Send text") {
newLinkText = "Cancel text";
} else {
newLinkText = "Send text";
}
$("#textLink").text(newLinkText);
}
/**
* Updates the character count of the current text message length so the user knows how
* many more characters they have before it's rejected.
*/
function updateCount()
{
var len;
len = $("#textMessage").val().length;
$("#textCharacterCount").text("(" + len + "/160)");
}
/**
* Send an AJAX request to send the text message via the Twilio REST client. This
* AJAX request is -NOT- Asynchronous because we do actually care if there was an
* error message.
*/
function sendTextMessage()
{
var xmlhttp;
xmlhttp = new XMLHttpRequest();
$("#log").text("Sending..");
xmlhttp.open("POST", "sendText.php", false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
$("#sendText").text("Sending...");
xmlhttp.send("number=" + $("#number").val() + "&textMessage=" + $("#textMessage").val());
$("#sendText").text("Send");
$("#log").text(xmlhttp.responseText);
}
</script>
</head>
<body>
<div id="phonePad" class="dialer">
<h3>Test Phone</h3>
<div class="phoneNumberField">
<input type="text" class="phoneNumber" id="number" name="number" placeholder="Enter a phone number to call"/>
</div>
<div class="textCell">
<div id="textCellLink" class="textButton"><a href="#" id="textLink" onclick="showTextingArea();">Send text</a></div>
<div id="textMessageArea" class="textMessage" style="display: none;">
<textarea id="textMessage" name="textMessage" onInput="updateCount();"></textarea>
<a href="#" id="sendText" class="sendText" onclick="sendTextMessage();">Send</a>
<div id="textCharacterCount">(0/160)</div>
</div>
</div>
<div id="log">Loading...</div>
<div class="numberRow">
<div id="1" class="numberButton" onclick="addNumber(this);">
<div class="number">1</div>
<div class="letters"> </div>
</div>
<div id="2" class="numberButton" onclick="addNumber(this);">
<div class="number">2</div>
<div class="letters">ABC</div>
</div>
<div id="3" class="numberButton" onclick="addNumber(this);">
<div class="number">3</div>
<div class="letters">DEF</div>
</div>
</div>
<div class="numberRow">
<div id="4" class="numberButton" onclick="addNumber(this);">
<div class="number">4</div>
<div class="letters">GHI</div>
</div>
<div id="5" class="numberButton" onclick="addNumber(this);">
<div class="number">5</div>
<div class="letters">JKL</div>
</div>
<div id="6" class="numberButton" onclick="addNumber(this);">
<div class="number">6</div>
<div class="letters">MNO</div>
</div>
</div>
<div class="numberRow">
<div id="7" class="numberButton" onclick="addNumber(this);">
<div class="number">7</div>
<div class="letters">PQRS</div>
</div>
<div id="8" class="numberButton" onclick="addNumber(this);">
<div class="number">8</div>
<div class="letters">TUV</div>
</div>
<div id="9" class="numberButton" onclick="addNumber(this);">
<div class="number">9</div>
<div class="letters">WXYZ</div>
</div>
</div>
<div class="numberRow">
<div id="star" class="numberButton" onclick="addNumber(this);">
<div class="number">*</div>
<div class="letters"> </div>
</div>
<div id="0" class="numberButton" onclick="addNumber(this);">
<div class="number">0</div>
<div class="letters"> </div>
</div>
<div id="pound" class="numberButton" onclick="addNumber(this);">
<div class="number">#</div>
<div class="letters"> </div>
</div>
</div>
<div class="buttons">
<button class="call" onclick="call();">
Call
</button>
<button class="hangup" onclick="hangup();">
Hangup
</button>
</div>
</div>
<!-- Twilio client/buddy list would eventually go here -->
<ul id="people"/>
</body>
</html>