-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (82 loc) · 3.15 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
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
import { CallsApi, Configuration, Bxml } from 'bandwidth-sdk';
import express from 'express';
const BW_ACCOUNT_ID = process.env.BW_ACCOUNT_ID;
const BW_VOICE_APPLICATION_ID = process.env.BW_VOICE_APPLICATION_ID;
const BW_NUMBER = process.env.BW_NUMBER;
const BW_USERNAME = process.env.BW_USERNAME;
const BW_PASSWORD = process.env.BW_PASSWORD;
const LOCAL_PORT = process.env.LOCAL_PORT;
const BASE_CALLBACK_URL = process.env.BASE_CALLBACK_URL;
if([
BW_ACCOUNT_ID,
BW_VOICE_APPLICATION_ID,
BW_NUMBER,
BW_USERNAME,
BW_PASSWORD,
LOCAL_PORT,
BASE_CALLBACK_URL
].some(item => item === undefined)) {
throw new Error('Please set the environment variables defined in the README');
}
const config = new Configuration({
username: BW_USERNAME,
password: BW_PASSWORD
});
const app = express();
app.use(express.json());
app.post('/calls', async (req, res) => {
const body = {
applicationId: BW_VOICE_APPLICATION_ID,
to: req.body.to,
from: BW_NUMBER,
answerUrl: `${BASE_CALLBACK_URL}/callbacks/outbound/voice`,
};
const callsApi = new CallsApi(config);
await callsApi.createCall(BW_ACCOUNT_ID, body);
res.sendStatus(200);
})
app.post('/callbacks/outbound/voice', async (req, res) => {
const callback = req.body;
const response = new Bxml.Response();
switch (callback.eventType) {
case 'answer':
const speakSentenceAnswer = new Bxml.SpeakSentence('Press 1 to choose option 1. Press 2 to choose option 2. Press pound when you are finished.');
const gather = new Bxml.Gather({
gatherUrl: `${BASE_CALLBACK_URL}/callbacks/outbound/gather`,
terminatingDigits: '#'
}, speakSentenceAnswer);
response.addVerbs(gather);
break;
case 'initiate':
const speakSentenceInitiate = new Bxml.SpeakSentence('Initiate event received but not intended. Ending call.');
const hangup = new Bxml.Hangup();
response.addVerbs([speakSentenceInitiate, hangup]);
break;
case 'disconnect':
console.log('The Disconnect event is fired when a call ends, for any reason.');
console.log(`Call ${callback.getCallId} has disconnected`);
break;
default:
console.log(`Unexpected event type ${callback.eventType} received`);
break;
}
res.status(200).send(response.toBxml());
})
app.post('/callbacks/outbound/gather', async (req, res) => {
const callback = req.body;
const response = new Bxml.Response();
if (callback.eventType == 'gather') {
const digits = callback.digits;
let speakSentence;
if (digits === '1') {
speakSentence = new Bxml.SpeakSentence('You have chosen option 1. Goodbye');
} else if (digits === '2') {
speakSentence = new Bxml.SpeakSentence('You have chosen option 2. Goodbye');
} else {
speakSentence = new Bxml.SpeakSentence('You did not choose a valid option. Goodbye');
}
response.addVerbs(speakSentence);
}
res.status(200).send(response.toBxml());
})
app.listen(LOCAL_PORT);