-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·73 lines (60 loc) · 2.32 KB
/
index.js
File metadata and controls
executable file
·73 lines (60 loc) · 2.32 KB
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
var http = require('http');
var path = require('path');
var twilio = require('twilio');
var express = require('express');
var bodyParser = require('body-parser');
var randomUsername = require('./randos');
var config = require('./config');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/token', function(request, response) {
var identity = randomUsername();
var capability = new twilio.Capability(config.TWILIO_ACCOUNT_SID,
config.TWILIO_AUTH_TOKEN);
capability.allowClientOutgoing(config.TWILIO_TWIML_APP_SID);
capability.allowClientIncoming(identity);
var token = capability.generate();
response.send({
identity: identity,
token: token
});
});
app.post('/voice', function (req, res) {
console.log("abc:"+req.body.CallStatus);
var twiml = new twilio.TwimlResponse();
if(req.body.To) {
twiml.dial({ callerId: config.TWILIO_CALLER_ID}, function() {
if (/^[\d\+\-\(\) ]+$/.test(req.body.To)) {
this.number({statusCallbackEvent:['initiated ringing answered'],statusCallback:'https://fathomless-cove-49625.herokuapp.com/events',statusCallbackMethod: 'POST'},req.body.To);
} else {
this.client({statusCallbackEvent:['initiated ringing answered'],statusCallback:'https://fathomless-cove-49625.herokuapp.com/events',statusCallbackMethod: 'POST'},req.body.To);
}
});
} else {
twiml.say("Thanks for calling!");
}
res.set('Content-Type', 'text/xml');
res.send(twiml.toString());
});
var server = http.createServer(app);
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Express server running on *:' + port);
});
const io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
app.post('/events', function(request, response) {
var to = request.body.To;
var fromNumber = request.body.From;
var callStatus = request.body.CallStatus;
var callSid = request.body.CallSid;
io.emit('call progress event', { to, fromNumber, callStatus, callSid });
console.log(to, fromNumber, callStatus, callSid);
response.send('Event received');
});