Skip to content

Commit 57527bf

Browse files
committed
Moved hard coded annoucements to .env
1 parent 79bed74 commit 57527bf

File tree

2 files changed

+65
-64
lines changed

2 files changed

+65
-64
lines changed

.env.template

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

22
TWIL_FLEX_ACCOUNT_SID=AC
33
TWIL_FLEX_ACCOUNT_KEY=
4-
NUMBER_POOL=[]
4+
NUMBER_POOL=[]
5+
OUT_OF_SESSION_MESSAGE_ASYNC_CHANNEL=
6+
OUT_OF_SESSION_MESSAGE_FOR_CALL=
7+
CONNECTING_CALL_ANNOUCEMENT=
8+
CALL_ANNOUCEMENT_LANGUAGE=
9+
CALL_ANNOUCEMENT_VOICE=

routes/proxypoc.js

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ async function timeout(data) {
1313
}
1414

1515
async function doCleanupConversation(conversationSid) {
16-
await new Promise((a) => {setTimeout(a, Math.random(10000) + 100)});
1716
if ( !conversationSid) {
1817
console.error('Underfined conversationSid');
1918
}
@@ -24,8 +23,8 @@ async function doCleanupConversation(conversationSid) {
2423
null,
2524
{
2625
onAttemptFail: async (data) => {
27-
console.warn(`Got error response while cleaning up convo ${conversationSid}: ${JSON.stringify(data)}`);
2826
if ( data.error.status === 429) {
27+
console.warn(`Got error response while cleaning up convo, will retry ${conversationSid}: ${JSON.stringify(data)}`);
2928
await timeout(data);
3029
return true;
3130
} else {
@@ -53,7 +52,6 @@ async function cleanupConversation(conversationSid) {
5352

5453
// Finds a single conversation for a participant with the given address and proxy address
5554
async function getOpenConversationForAddressPair(address, proxyAddress) {
56-
5755
if (address === undefined) {
5856
throw "getOpenConversationsForAddressPair: address is missing";
5957
}
@@ -112,9 +110,6 @@ async function fetchProxyAddressesInOpenConversationsForAddress(address) {
112110
return proxyAddresses;
113111
}
114112

115-
116-
117-
118113
// Helper function for create /Conversations endpoint with retry handling
119114
async function createConversation(sessionOpts) {
120115
const res = await retry( async () => {
@@ -239,9 +234,9 @@ async function handleInboundCall(call) {
239234
if ( !conversation) {
240235
console.log(`Didnt find matching session (conversation) for ${address}/${proxyAddress}`);
241236
response.say({
242-
voice: 'woman',
243-
language: 'en'
244-
}, 'Sorry, I dont know who to connect you to!');
237+
voice: process.env.CALL_ANNOUCEMENT_VOICE,
238+
language: process.env.CALL_ANNOUCEMENT_LANGUAGE
239+
}, process.env.OUT_OF_SESSION_MESSAGE_FOR_CALL);
245240
} else {
246241

247242
// We got the conversation, let's get the participants in the convo.
@@ -254,9 +249,9 @@ async function handleInboundCall(call) {
254249
});
255250

256251
response.say({
257-
voice: 'woman',
258-
language: 'en'
259-
}, 'Connecting you, please wait!');
252+
voice: process.env.CALL_ANNOUCEMENT_VOICE,
253+
language: process.env.CALL_ANNOUCEMENT_LANGUAGE
254+
}, process.env.CONNECTING_CALL_ANNOUCEMENT);
260255

261256
// We call the second participant from the associated proxy_address
262257
const dial = response.dial({
@@ -268,6 +263,56 @@ async function handleInboundCall(call) {
268263
return response;
269264
}
270265

266+
// Creates a conversation and adds the participants to the conversation
267+
async function handleCreateSession(sessionOpts, addresses) {
268+
269+
let newConversation;
270+
try {
271+
newConversation = await createConversation(sessionOpts);
272+
console.log(`Created new conversation successfully: ${newConversation.sid}`)
273+
} catch (e) {
274+
console.error(`Couldnt create a new session for ${JSON.stringify(addresses)}: ${JSON.stringify(e)}`)
275+
const error = {
276+
message: 'Could not create session',
277+
raw_message: JSON.stringify(e),
278+
}
279+
280+
throw error;
281+
}
282+
283+
try {
284+
const participants = [];
285+
for ( let i = 0; i < addresses.length; ++i) {
286+
participants[i] = await handleAddParticipant(newConversation.sid, addresses[i]);
287+
}
288+
289+
const result = {
290+
sid: newConversation.sid,
291+
participants,
292+
}
293+
294+
return result;
295+
296+
} catch(e) {
297+
console.error(`Couldnt add participants to a new session for ${JSON.stringify(addresses)}: ${e}`)
298+
if ( newConversation) {
299+
try {
300+
await cleanupConversation(newConversation.sid);
301+
} catch (ce) {
302+
console.log(`Couldnt clean up conversation ${newConversation.sid}: ${JSON.stringify(ce)}`);
303+
}
304+
}
305+
const error = {
306+
sid: newConversation.sid,
307+
message: 'Could not add participants session',
308+
raw_message: JSON.stringify(e),
309+
}
310+
311+
throw error;
312+
}
313+
}
314+
315+
271316
/*
272317
* Calls handleInboundCall to handle inbound call event
273318
*/
@@ -362,64 +407,15 @@ router.use('/global-webhook', async function(req, res, next) {
362407
// Send a friendly message
363408
const messageOpts = {
364409
author: 'System',
365-
body: 'Thank you for contacting us. Please call us on 123123123',
366-
410+
body: process.eng.OUT_OF_SESSION_MESSAGE_ASYNC_CHANNEL,
367411
}
412+
368413
await client.conversations.conversations(req.body.ConversationSid).messages.create(messageOpts);
369414
await cleanupConversation(req.body.ConversationSid.sid);
370415

371416
res.send({});
372417
});
373418

374-
375-
async function handleCreateSession(sessionOpts, addresses) {
376-
377-
let newConversation;
378-
try {
379-
newConversation = await createConversation(sessionOpts);
380-
console.log(`Created new conversation successfully: ${newConversation.sid}`)
381-
} catch (e) {
382-
console.error(`Couldnt create a new session for ${JSON.stringify(addresses)}: ${JSON.stringify(e)}`)
383-
const error = {
384-
message: 'Could not create session',
385-
raw_message: JSON.stringify(e),
386-
}
387-
388-
throw error;
389-
}
390-
391-
try {
392-
const participants = [];
393-
for ( let i = 0; i < addresses.length; ++i) {
394-
participants[i] = await handleAddParticipant(newConversation.sid, addresses[i]);
395-
}
396-
397-
const result = {
398-
sid: newConversation.sid,
399-
participants,
400-
}
401-
402-
return result;
403-
404-
} catch(e) {
405-
console.error(`Couldnt add participants to a new session for ${JSON.stringify(addresses)}: ${e}`)
406-
if ( newConversation) {
407-
try {
408-
await cleanupConversation(newConversation.sid);
409-
} catch (ce) {
410-
console.log(`Couldnt clean up conversation ${newConversation.sid}: ${JSON.stringify(ce)}`);
411-
}
412-
}
413-
const error = {
414-
sid: newConversation.sid,
415-
message: 'Could not add participants session',
416-
raw_message: JSON.stringify(e),
417-
}
418-
419-
throw error;
420-
}
421-
}
422-
423419
/*
424420
* Creates a session (conversation) for the given address(es)
425421
* For each address submitted, it will find a proxy number and address/proxy number as a participant to the newly created conversation

0 commit comments

Comments
 (0)