This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
139 lines (117 loc) · 3.53 KB
/
server.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
// Get port from environment or use port 3000
var port = process.env.PORT || 3000;
// Initialize express, socket.io and node-phonenumber
var express = require("express");
var app = express();
var server = app.listen(port);
var io = require("socket.io")(server);
var phone = require("node-phonenumber");
var phoneUtil = phone.PhoneNumberUtil.getInstance();
var xml = require("xml");
// Set ejs as template engine
app.set("view engine", "ejs");
// Serve static files from 'public/' folder
app.use(express.static(__dirname + "/public"));
// Initialize express body-parser middleware
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
// Serve 'index.html'
app.get("/", function(req, res) {
res.render("index");
});
// Serve 'connect.html'
app.get("/connect", function(req, res) {
var namespace = "demo" + Math.random();
res.render("connect", {
namespace: namespace,
url: "https://" + req.headers.host,
isSuccess: false
});
});
app.get("/success", function(req, res) {
var namespace = req.query.token;
res.render("connect", {
namespace: namespace,
isSuccess: true
});
io.of("/" + namespace).on("connection", function(socket) {
console.log("connected");
});
});
// Process API POST requests
app.post("/", function(request, response) {
var namespace = request.query.token;
var socket = namespace ? io.of("/" + namespace) : io.sockets;
if (request.body.event == "newCall") {
// Send call to voicemail
response.header("Content-Type", "application/xml");
response.send(
xml({
Response: [
{
_attr: {
onAnswer:
"https://" +
request.headers.host +
(namespace ? "?token=" + namespace : ""),
onHangup:
"https://" +
request.headers.host +
(namespace ? "?token=" + namespace : "")
}
},
namespace ? {} : { Dial: [{ Voicemail: null }] }
]
})
);
// Parse and format numbers
var formatNumber = function(number) {
if (number.match("anonymous")) {
return "Anonym";
}
var parsedNumber = phoneUtil.parse("+" + number, "DE");
var format = phone.PhoneNumberFormat.INTERNATIONAL;
if (parsedNumber.getCountryCode() == 49) {
format = phone.PhoneNumberFormat.NATIONAL;
}
return phoneUtil.format(parsedNumber, format).replace(/...$/, "xxx");
};
// Get POST parameters
var from = formatNumber(request.body.from);
var to = formatNumber(request.body.to);
var direction = request.body.direction == "in" ? "Eingehend" : "Ausgehend";
// Send 'from', 'to' and 'direction' to all connected socket.io clients
socket.emit("new call", {
from: from,
to: to,
direction: direction,
callId: request.body.callId
});
} else if (request.body.event == "answer") {
response.send();
socket.emit("answer", {
callId: request.body.callId
});
} else if (request.body.event == "hangup") {
response.send();
var getCause = function(cause) {
switch (cause) {
case "busy":
return "Besetzt";
case "normalClearing":
return "Aufgelegt";
case "cancel":
return "Abgebrochen";
case "noAnswer":
return "Abgewiesen";
default:
return "Beendet";
}
};
var cause = getCause(request.body.cause);
socket.emit("end call", {
callId: request.body.callId,
cause: cause
});
}
});