-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoicemail.js
146 lines (127 loc) · 4.47 KB
/
voicemail.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
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
//----Bandwidth Number to call: +17042662707-----
var Bandwidth = require("node-bandwidth");
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var http = require("http").Server(app);
var client = new Bandwidth({
// uses my environment variables. Go to dev.bandwidth.com, look under Account -> API Information -> Credentials OR .zsrh file
userId : process.env.BANDWIDTH_USER_ID, // <-- note, this is not the same as the username you used to login to the portal
apiToken : process.env.BANDWIDTH_API_TOKEN,
apiSecret : process.env.BANDWIDTH_API_SECRET
});
//BANDWIDTH
app.use(bodyParser.json());
//use a json body parser
app.set('port', (process.env.PORT || 4000));
//set port to an environment variable port or 4000
const getBaseUrlFromReq = (req) => {
return 'http://' + req.hostname;
};
app.get("/", function (req, res) {
console.log(req);
res.send("Bandwdith_Simple_Voicemail_System");
//res.send(can be a website);
});
let toNumber = "+19197891146";
//+19198251930 conference room number
app.post('/out-call', function (req, res) { //OUTBOUND CALLS
var this_url2 = getBaseUrlFromReq(req);
if (req.body.eventType === 'answer') {
console.log("Incoming CallId: " + req.body.tag);
console.log("Outgoing CallId: " + req.body.callId);
console.log(req);
client.Bridge.create({
bridgeAudio: true,
callIds : [req.body.tag, req.body.callId]
})
.then(function (bridge) {
console.log("BridgeId: " + bridge.id);
console.log("---Call has been bridged----");
})
.catch(function(err){
console.log(err);
console.log("----Could not bridge the call----");
});
}
// else if (req.body.eventType === "timeout" && !(index === listOfNumbers.length)){
// createCallWithCallback(req.body.from, this_url2, req.body.tag);
// console.log(req.body);
// console.log("-----The phone call has timed out-----");
// }
else if (req.body.eventType === "timeout"){
console.log("Tag: " + req.body.tag)
client.Call.speakSentence(req.body.tag, "You have reached Bandwidth. Sorry we can't get to the phone right now, please leave your message after the beep. BEEP.")
.then(function(res){
console.log("---Voicemail message spoken---");
})
.catch(function(err){
console.log(err);
console.log("Could not speak the voicemail intro sentence");
})
}
else if (req.body.eventType === "hangup"){ //should automatically stop recording here
console.log(req.body);
console.log("----Your call has hungup----");
}
else{
console.log(req.body);
}
});
app.post('/in-call', function (req, res) { //INBOUND CALLS
if (req.body.eventType === "incomingcall"){
console.log("Incoming callId: " + req.body.callId);
var this_url1 = getBaseUrlFromReq(req);
createCallWithCallback(req.body.to, this_url1, req.body.callId);
}
else if (req.body.eventType === "speak" && req.body.state === "PLAYBACK_STOP"){
console.log("CallId for the recording: " + req.body.callId);
client.Call.enableRecording(req.body.callId)
.then(function (res) {
console.log("---Recording has started----");
})
.catch(function(err){
console.log(err);
console.log("Could not record");
});
}
else if (req.body.eventType === "recording" && req.body.state === "complete"){
console.log("---The original inbound call has been hungup---");
client.Recording.createTranscription(req.body.recordingId)
.then(function(transcription){
console.log("-----Transcribing-----");
})
.catch(function(err){
console.log(err);
console.log("Sorry, something went wrong with the transcription");
});
}
else if (req.body.eventType === "transcription" && req.body.state === "completed"){
console.log(req.body);
console.log("Transcribed");
}
else{
console.log(req.body);
}
});
var createCallWithCallback = function(FromBWnumber, this_url, tag){
return client.Call.create({
from: FromBWnumber,
to: toNumber,
callTimeout: 10,
callbackUrl: this_url + '/out-call',
tag: tag
})
.then(function (call) {
console.log("Outgoing call Id: " + call.callId);
console.log(call);
console.log("----Outbound call has been created----");
})
.catch(function(err){
console.log(err);
console.log("---Outbound call could not be created---");
})
};
http.listen(app.get('port'), function(){
console.log('listening on *: ' + app.get('port'));
});