-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.js
More file actions
287 lines (258 loc) · 8.95 KB
/
setup.js
File metadata and controls
287 lines (258 loc) · 8.95 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import 'dotenv/config';
import fs from 'fs';
import { Vonage } from '@vonage/server-sdk';
let vonage;
const PORT = '3000';
console.log('setup.js running...');
if (process.env.VONAGE_API_KEY && process.env.VONAGE_API_SECRET) {
// If the environment variables are already set, use them
console.log('Environment variables already set. Skipping setup.');
process.exit();
}
let step = 'SET_API_KEY';
console.log('Vonage setup utility for Github Codespaces -- press "q" to exit');
console.log('This utility will need your Vonage API key and API secret. They will be saved to your .env file,');
console.log('where they will be visible only to you and collaborators on this project.');
console.log('Find your API key and secret at: https://dashboard.vonage.com/getting-started-guide');
console.log('process.env.CODESPACE_NAME: ', process.env.CODESPACE_NAME);
console.log('Enter your API Key:');
let input = process.stdin;
input.on('data', data => {
if (data.toString().trim() === 'q') {
// exit
return process.exit();
}
switch (step) {
case 'SET_API_KEY':
return setApiKey(data);
break;
case 'SET_API_SECRET':
return setApiSecret(data);
break;
case 'SET_APP_NAME':
return setAppName(data);
break;
case 'BUY_NUMBER':
return buyNumberQuestion(data);
break;
case 'SET_COUNTRY_CODE':
return setCountryCode(data);
break;
case 'ENTER_PHONE_NUMBER':
return updatePhoneNumber(data);
break;
default:
}
});
function setApiKey(data) {
if (data.toString().replace(/\n/g, '').length === 0 || data.toString().replace(/\n/g, '') === ' ') {
console.log('(Can not be blank.) Enter you API key:');
} else {
process.env.VONAGE_API_KEY = data.toString().replace(/\n/g, '');
step = 'SET_API_SECRET';
console.log('Enter you API secret:');
}
return true;
}
function setApiSecret(data) {
if (data.toString().replace(/\n/g, '').length === 0 || data.toString().replace(/\n/g, '') === ' ') {
console.log('(Can not be blank.) Enter you API secret:');
} else {
process.env.VONAGE_API_SECRET = data.toString().replace(/\n/g, '');
step = 'SET_APP_NAME';
console.log('Enter a name for your Application:');
}
return true;
}
function setAppName(data) {
if (data.toString().replace(/\n/g, '').length === 0 || data.toString().replace(/\n/g, '') === ' ') {
console.log('(Can not be blank.) Enter a name for your Application:');
} else {
createApp(data);
}
return true;
}
function buyNumberQuestion(data) {
// console.log("data:", data);
if (data.toString().replace(/\n/g, '').length === 0 || data.toString().replace(/\n/g, '') === ' ') {
console.log('(Can not be blank.) Want to Buy a number? (Y/N):');
} else if (data.toString().replace(/\n/g, '').toLowerCase() === 'y'){
// process.env.VONAGE_API_SECRET = data.toString().replace(/\n/g, '');
step = 'SET_COUNTRY_CODE';
console.log('Set the country code for your number (ex. US or GB):');
} else if (data.toString().replace(/\n/g, '').toLowerCase() === 'n') {
// answered No to question
writeEnv();
}
return true;
}
function setCountryCode(data) {
if (data.toString().replace(/\n/g, '').length === 0 || data.toString().replace(/\n/g, '') === ' ') {
console.log('(Can not be blank.) Set the country code for your number (ex. US or GB):');
} else {
buyPhoneNumber(data)
}
return true;
}
function buyPhoneNumber(data){
const countryCode = data.toString().replace(/\n/g, '');
process.env.COUNTRY_CODE = data.toString().replace(/\n/g, '').toUpperCase();
//Search for a number
console.log('Searching for a number in country: ', countryCode);
vonage.numbers.getAvailableNumbers({
country: process.env.COUNTRY_CODE,
features: ['VOICE', 'SMS'],
})
.then((results) => {
const numbers = results.numbers;
console.log('Found numbers: ', numbers);
// Purchase a number
if (numbers.length === 0) {
console.log('No numbers found in this country. Please try another country code.');
return;
}
console.log('Found a number: ', numbers[0].msisdn);
vonage.numbers.buyNumber({
country: numbers[0].country,
msisdn: numbers[0].msisdn,
})
.then((result) => {
console.log('Bought the number!');
updatePhoneNumber(numbers[0]);
})
.catch((error) => {
console.error("Error buying number:", error);
});
})
.catch((error) => {
console.error(error)
});
}
async function updatePhoneNumber(number) {
console.log('Adding phone number to application...');
const options = {
country: process.env.COUNTRY_CODE,
msisdn: number.msisdn,
appId: process.env.API_APPLICATION_ID,
};
const resp = await vonage.numbers.updateNumber(options);
if (resp.errorCode !== '200') {
console.error(`Error: ${resp.errorCodeLabel}`);
} else {
console.log('Added number to application!');
process.env.CONFERENCE_NUMBER = number.msisdn;
writeEnv();
}
}
function createApp(data) {
console.log('Creating your Application...');
vonage = new Vonage({
apiKey: process.env.VONAGE_API_KEY,
apiSecret: process.env.VONAGE_API_SECRET
}, {
debug: false
});
vonage.applications.createApplication({
name: data.toString().replace(/\n/g, ''),
capabilities: {
voice: {
webhooks: {
answer_url: {
address: `https://${process.env.CODESPACE_NAME}-${PORT}.app.github.dev/sip/vapi/answer`,
http_method: "GET"
},
event_url: {
address: `https://${process.env.CODESPACE_NAME}-${PORT}.app.github.dev/sip/vapi/events`,
http_method: "POST"
}
}
},
messages: {
webhooks: {
inbound_url: {
address: `https://${process.env.CODESPACE_NAME}-${PORT}.app.github.dev/webhooks/inbound`,
http_method: "POST"
},
status_url: {
address: `https://${process.env.CODESPACE_NAME}-${PORT}.app.github.dev/webhooks/status`,
http_method: "POST"
}
}
},
rtc: {
webhooks: {
event_url: {
address: `https://${process.env.CODESPACE_NAME}-${PORT}.app.github.dev/webhooks/rtcevent`,
http_method: "POST"
}
}
}
}
}).then((app) => {
console.log('Application created with ID: ', app.id);
process.env.API_APPLICATION_ID = app.id;
fs.writeFile(import.meta.dirname + '/private.key', app.keys.private_key, (err) => {
if (err) {
console.log('Error writing private key: ', err);
} else {
console.log('Private key saved to private.key');
try {
// convert private.key to base64
console.log('Converting private key to base64...');
const privateKey = fs.readFileSync(import.meta.dirname + '/private.key');
const base64PrivateKey = privateKey.toString('base64');
process.env.PRIVATE_KEY64 = base64PrivateKey;
//Search and Buy phone number
process.env.VONAGE_APPLICATION_NAME = data.toString().replace(/\n/g, '');
step = 'BUY_NUMBER';
console.log('Want to Buy a number? (Y/N):');
} catch (error) {
console.error('An error occurred:', error);
}
}
});
}).catch((error) => {
console.error('Error creating Application: ', error);
process.exit();
});
return true;
}
function writeEnv() {
const contents = `ADMIN_NAME="${process.env.ADMIN_NAME}"
ADMIN_PASSWORD="${process.env.ADMIN_PASSWORD}"
VONAGE_API_KEY="${process.env.VONAGE_API_KEY}"
VONAGE_API_SECRET="${process.env.VONAGE_API_SECRET}"
VONAGE_APPLICATION_NAME="${process.env.VONAGE_APPLICATION_NAME}"
API_APPLICATION_ID="${process.env.API_APPLICATION_ID}"
COUNTRY_CODE="${process.env.COUNTRY_CODE}"
CONFERENCE_NUMBER="${process.env.CONFERENCE_NUMBER}"
PRIVATE_KEY64="${process.env.PRIVATE_KEY64}"`;
fs.writeFile(import.meta.dirname + '/.env', contents, (err) => {
if (err) {
console.log('Error writing .env file: ',err);
} else {
console.log('Environment variables saved to .env');
process.exit();
}
});
}
function createUser() {
//create user
const vonage = new Vonage({
apiKey: process.env.VONAGE_API_KEY,
apiSecret: process.env.VONAGE_API_SECRET,
applicationId: process.env.API_APPLICATION_ID,
privateKey: import.meta.dirname + process.env.PRIVATE_KEY
}, {debug: false});
vonage.users.create({
name: process.env.ADMIN_NAME,
display_name: process.env.ADMIN_NAME
},(err, response) => {
if (err) {
console.log('Error creating user: ',err);
} else {
console.log('User created. ID: ', response.id);
process.exit();
}
});
}