-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (162 loc) · 6.31 KB
/
index.js
File metadata and controls
180 lines (162 loc) · 6.31 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
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
//const http = require('srnd-microservices/http')
const { GoogleSpreadsheet } = require('google-spreadsheet')
const VoiceResponse = require('twilio').twiml.VoiceResponse
const moment = require('moment-timezone');
const express = require('express');
const bodyParser = require('body-parser');
const { phone } = require('phone');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const UPDATE_LOCALES_QUERY = `
query PhoneLocales {
cms {
localizationConfigs {
items {
iso3166Alpha2Code
iso3166Alpha3Code
e164CountryCode
inboundPhoneNumbers
phoneGreetingAudio { contentfulBaseUrl }
phoneInvalidAudio { contentfulBaseUrl }
phoneConnectingAudio { contentfulBaseUrl }
}
}
}
}
`;
let locales = [];
let localesByAlpha2 = {};
let localesByAlpha3 = {};
let localesByE164 = {};
let localesByInboundPhoneNumber = {};
async function updateLocales() {
try {
const response = await fetch("https://graph.codeday.org/", {
body: JSON.stringify({ operationName: 'PhoneLocales', variables: {}, query: UPDATE_LOCALES_QUERY }),
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
const data = await response.json();
locales = data.data.cms.localizationConfigs.items;
localesByAlpha2 = Object.fromEntries(locales.map(l => [l.iso3166Alpha2Code.toUpperCase(), l]));
localesByAlpha3 = Object.fromEntries(locales.map(l => [l.iso3166Alpha3Code.toUpperCase(), l]));
localesByE164 = Object.fromEntries(locales.map(l => [l.e164CountryCode, l]));
localesByInboundPhoneNumber = Object.fromEntries(locales.flatMap(l => (l.inboundPhoneNumbers || []).map(n => [n, l])));
console.log('Updated locales.');
} catch (ex) {
console.error('Could not update locales.');
}
}
function fetchLocaleConfig(req) {
const fromCountry = (req.query?.FromCountry || req.body?.FromCountry)?.toUpperCase();
const fromPhone = req.query?.From || req.body?.From;
const fromE164 = fromPhone ? phone(fromPhone)?.countryCode.slice(1) : null;
const to = req.query?.To || req.body?.To;
if (fromCountry && fromCountry.length == 2 && fromCountry in localesByAlpha2) {
return localesByAlpha2[fromCountry];
} else if (fromCountry && fromCountry.length == 3 && fromCountry in localesByAlpha3) {
return localesByAlpha3[fromCountry];
} else if (fromE164 && fromE164 in localesByE164) {
return localesByE164[fromE164];
} else if (to && to in localesByInboundPhoneNumber) {
return localesByInboundPhoneNumber[to];
} else {
return localesByAlpha2['US'];
}
}
async function getNumberForExtension(ext) {
const doc = new GoogleSpreadsheet(process.env.GOOGLE_SHEET);
await googleLogin(doc);
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0];
const rows = (await sheet.getRows({
offset: 1,
})).filter((row) => row.ext === ext);
if (typeof(rows) === 'undefined' || rows === null || rows.length == 0) return null;
return {
"description": rows[0].description,
"number": rows[0].phone,
"direct": rows[0].direct
};
}
async function googleLogin(sheet) {
await sheet.useServiceAccountAuth({
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: atob(process.env.GOOGLE_PRIVATE_KEY),
});
}
app.all('/', async (req, res) => {
const { phoneGreetingAudio } = fetchLocaleConfig(req);
const response = new VoiceResponse();
const gather = response.gather({action: '/dial', method: 'GET'});
for (let i = 0; i < 3; i++) {
gather.play({}, phoneGreetingAudio.contentfulBaseUrl);
gather.pause({length: 3});
}
response.hangup();
res.send(response.toString());
});
app.all('/dial', async (req, res) => {
try {
const { phoneInvalidAudio, phoneConnectingAudio } = fetchLocaleConfig(req);
const digits = req.query.Digits.replace(/[^0-9]*/g, '');
const response = new VoiceResponse();
const phoneInfo = await getNumberForExtension(digits);
if (phoneInfo && phoneInfo.number && phoneInfo.number.substr(0,3) === 'qa:') {
response.say({voice:"woman"}, 'You\'re connected to the queue.');
response.dial().queue(phoneInfo.number.substr(3));
response.redirect({ method: 'GET' }, `/dial?Digits=${req.query.Digits}`);
} else if (phoneInfo && phoneInfo.number && phoneInfo.number.substr(0,2) === 'q:') {
response.say({voice:"woman"}, 'Please stay on the line and we will connect you as soon as we can.');
response.enqueue({
waitUrl: '/queue',
}, phoneInfo.number.substr(2));
} else if (phoneInfo && phoneInfo.number) {
let toDial = phoneInfo.number;
if (toDial.indexOf(',') !== -1) {
const allDials = toDial.split(',');
toDial = allDials[Math.floor(Math.random() * allDials.length)].replace(/[^0-9]/g, '');
}
if (phoneInfo.direct && phoneInfo.direct !== "" && phoneInfo.direct !== "no") {
response.dial().number(toDial);
} else {
response.play(phoneConnectingAudio.contentfulBaseUrl);
response.dial().number({
url: 'https://codeday-phone.fly.dev/connect?connectFor='+encodeURIComponent(phoneInfo.description),
method: 'GET'
}, toDial);
}
} else {
response.play(phoneInvalidAudio.contentfulBaseUrl);
response.redirect({method: 'get'}, '/phone');
}
res.send(response.toString());
} catch (ex) {
console.error(ex);
res.send('');
}
});
app.all('/connect', async (req, res) => {
const response = new VoiceResponse();
response.play('https://f1.codeday.org/phone/incoming-codeday.mp3');
response.say({voice: "woman"}, req.query.connectFor);
response.play('https://f1.codeday.org/phone/incoming-srnd-end.mp3');
res.send(response.toString());
});
app.all('/queue', async (req, res) => {
const response = new VoiceResponse();
if (req.query.QueuePosition) {
response.say({voice:"woman"}, `Thank you for your patience, you are caller number ${req.query.QueuePosition} in the queue.`);
}
response.play('https://f1.codeday.org/phone/queue_hold.mp3');
res.send(response.toString());
});
(async () => {
await updateLocales();
setInterval(updateLocales, 1000 * 60 * 15);
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Listening on http://0.0.0.0:${port}`))
})();