-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (44 loc) · 1.32 KB
/
index.js
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
var twilio = require('twilio');
var query = require('readline-sync').question;
var e = require('events');
var events = new e.EventEmitter();
var server = require('./server');
var repo = require('./repo');
var accountSid = process.env.TWILIO_SID;
var authToken = process.env.TWILIO_TOKEN;
var target = query('Who are we targetting? ');
var client = new twilio(accountSid, authToken);
// Initiate the web server
server(events);
var url = '';
events.on('init', createCall);
events.on('connect', handleConnect);
events.on('disconnect', handleDisconnect);
events.on('disconnect', createCall);
function handleCall(CallInstance) {
console.log('Call created: ' + CallInstance.sid);
repo.add(CallInstance);
}
function handleConnect(callSid) {
console.log('Call Connected: ' + callSid);
}
function handleDisconnect(callSid) {
console.log('Call Disconnected: ' + callSid);
repo.remove(callSid);
}
function createCall() {
debugger;
client.calls.create({
to: target,
from: process.env.TWILIO_FROM,
url: url + '/handler.xml',
statusCallback: url + '/status/',
statusCallbackEvent: [
"completed",
"answered"
]
})
.then((message) => handleCall(message))
.catch((error) => console.error(error.message));
}
events.emit('init');