-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure.js
386 lines (342 loc) · 11.1 KB
/
configure.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
var program = require('commander');
var fs = require('fs');
var exec = require('child_process').exec;
program
.version('Twilio Hackpack for Heorku and Express v0.1')
.option('-n, --new', 'We need to set up a new AppSID and Number')
.option('-a, --app_sid [appSid]', 'Use this Twilio App SID')
.option('-c, --caller_id [callerID]', 'Use this Twilio Number')
.option('-d, --domain [url]', 'Use this custom domain')
.option('-as, --account_sid [accSid]', 'Use this Twilio Account SID')
.option('-at, --auth_token [auth]', 'Use this Twilio Auth Token')
.option('-v, --voice [url]', 'Use this Voice Url')
.option('-s, --sms [url]', 'Use this SMS Url')
.option('-f, --friendly [name]', 'Use this friendly name [HackPack for Heroku and Express]',
'Hackpack for Heroku and Express')
.parse(process.argv);
function Configure(){
if(!(this instanceof Configure)) {
return new Configure();
}
this.account_sid = process.env.TWILIO_ACCOUNT_SID;
this.auth_token = process.env.TWILIO_AUTH_TOKEN;
this.app_sid = process.env.TWILIO_APP_SID;
this.phone_number = process.env.TWILIO_CALLER_ID;
this.voice_url = '/autoprovision/1';
this.sms_url = '/autoprovision/0';
this.client_voice_url = '/voice';
this.host_;
this.client;
}
Configure.prototype.start = function() {
console.info('Configuring your Twilio hackpack...');
console.info('Checking if your credentials are set...');
if(!this.account_sid){
throw new Error("ACCOUNT_SID is not set");
}
if(!this.auth_token){
throw new Error("AUTH_TOKEN is not set");
}
console.log('Checking for host name...');
if(!this.host_){
console.log('Setting hostname...');
this.host_ = this.getHerokuHostName();
console.log('Hostname is now ' + this.host_);
}
console.log('Creating Twilio client...');
TwilioClient = require('heroku-twilio').Client;
try{
this.client = new TwilioClient(this.account_sid, this.auth_token, this.host_);
}catch(e){
throw new Error('Could not create Twilio Rest Client, exception: ' + e);
}
// If 'http://' isn't found in the voice_url, append the voice_url to the host name
if(this.voice_url.indexOf('http://') == -1 ){
this.voice_url = this.host_ + this.voice_url;
console.info('Voice url is now ' + this.voice_url);
}
// If 'http://' isn't found in the sms_url, append the sms_url to the host name
if(this.sms_url.indexOf('http://') == -1){
this.sms_url = this.host_ + this.sms_url;
console.info('Sms url is now ' + this.sms_url);
}
// If 'http://' isn't found in the client voice url, append the client_voice_url to the host name
if(this.client_voice_url.indexOf('http://') == -1){
this.client_voice_url = this.host_ + this.client_voice_url;
console.info('Client voice url is now ' + this.client_voice_url);
}
var self = this;
this.configureCallerId(function(number){
self.phone_number = number.phone_number;
if(!self.phone_number){
throw new Error('There was a problem getting the Caller_Id');
}
self.configureApp(function(app){
self.app_sid = app.sid;
if(!self.app_sid){
throw new Error('There was a probem setting up the app')
}
self.printOutLocalEnvironmentVariableCommands();
self.setHerokuEnvironmentVariables(function(stdout){
console.log(stdout + '\n');
console.log('Your hackpack is almost configured! Open up your Heorku app (heroku open)' +
'to configure your Voice and Sms urls and then hack away on app.js!');
process.exit();
});
});
});
}
Configure.prototype.printOutLocalEnvironmentVariableCommands = function(){
// Removes the substring 'http://' so Twilio URL's are not messed up
this.host_ = this.host_.replace('http://', '');
console.log('Please copy and paste these into your shell to test locally: \n' +
'export TWILIO_ACCOUNT_SID=' + process.env.TWILIO_ACCOUNT_SID + '\n' +
'export TWILIO_AUTH_TOKEN=' + process.env.TWILIO_AUTH_TOKEN + '\n' +
'export TWILIO_APP_SID=' + this.app_sid + '\n' +
'export TWILIO_CALLER_ID=' + this.phone_number + '\n' +
'export TWILIO_HOST=' + this.host_ + '\n');
}
Configure.prototype.setHerokuEnvironmentVariables = function(callback){
// Removes the substring 'http://' so Twilio URL's are not messed up
this.host_ = this.host_.replace('http://', '');
exec('heroku config:add TWILIO_ACCOUNT_SID=' + process.env.TWILIO_ACCOUNT_SID +
' TWILIO_AUTH_TOKEN=' + process.env.TWILIO_AUTH_TOKEN +
' TWILIO_CALLER_ID=' + this.phone_number +
' TWILIO_APP_SID=' + this.app_sid +
' TWILIO_HOST=' + this.host_ , function(error, stdout, stderr){
if(stderr){
throw new Error('Could not add environment variables to Heroku: ' + stderr)
} else{
callback(stdout)
}
});
}
Configure.prototype.purchasePhoneNumber = function(purchasedCallback){
var client = this.client;
var number;
var i = 0;
function askToBuyPhoneNumber(question, callback){
i++;
process.stdin.resume();
process.stdin.setEncoding('utf8');
console.log(question);
program.prompt('choice: ', function(choice){
var output = choice.toLowerCase();
if(output=='y'){
callback('y');
}else if(output=='n' || i >= 3){
callback('n');
}else{
askToBuyPhoneNumber(question, callback);
}
});
}
var self = this;
askToBuyPhoneNumber('Your Caller_Id is not configured. Buy a new phone number? (Your account will be charged $1) [y/n]',
function(output){
if(output=='y'){
params = {
VoiceUrl: self.voice_url,
SmsUrl: self.sms_url,
AreaCode: '703'
}
try{
client.purchaseIncomingNumber(params, function(body){
number = body;
purchasedCallback(number);
});
}catch(e){
throw new Error('Purchasing incoming number failed. Exception: ' + e);
}
}else{
console.log('A Caller_Id must be specified');
process.exit();
}
});
}
Configure.prototype.configureCallerId = function(callback){
var phone_number = this.phone_number;
if(!phone_number){
this.purchasePhoneNumber(function(number){
if(number){
phone_number = number.phone_number;
callback(number);
}else{
throw new Error('A phone number could not be retrieved. Are you sure you have a caller_id?');
}
});
}else{
this.retrievePhoneNumber(phone_number, function(number){
if(number){
phone_number = number.phone_number;
callback(number);
}else{
throw new Error('A phone number could not be retrieved. Are you sure you have a caller_id?');
}
});
}
}
Configure.prototype.retrievePhoneNumber = function(number, callback){
var number;
try{
params = {
PhoneNumber: number
}
this.client.getIncomingNumbers(params, function(body){
number = body.incoming_phone_numbers[0];
callback(number);
});
}catch (e) {
throw new Error('Could not retrieve incoming numbers for account. Exception: ' + e);
}
}
Configure.prototype.createNewTwimlApp = function(createdCallback){
console.log('Asking user to create new app sid...');
var client = this.client;
var i = 0;
var app;
function askToCreateTwimlApp(question, callback){
i++;
process.stdin.resume();
process.stdin.setEncoding('utf8');
console.log(question);
program.prompt('choice: ', function(choice){
var output = choice.toLowerCase();
if(output=='y'){
callback(output);
}else if(output=='n' || i >= 3){
callback('n')
}else{
ask(question, callback);
}
});
}
var self = this;
askToCreateTwimlApp('Your APP_SID is not configured. Create a new one? [y/n]', function(output){
if(output=='y'){
console.log('Creating new application...');
params = {
FriendlyName: 'Hackpack for Heroku and Express',
VoiceUrl: self.client_voice_url
}
try{
client.createApplication(params, function(data){
createdCallback(data);
});
}catch(e){
throw new Error('Could not create the application. Exception: ' + e);
}
}else{
throw new Error('Your APP_SID must be configured');
}
});
}
Configure.prototype.configureApp = function(callback){
var app_sid = this.app_sid;
if(!app_sid){
console.log('Creating new TwiML app...');
this.createNewTwimlApp(function(app){
if(app){
app_sid = app.sid;
callback(app);
}else{
throw new Error('Failed creating a new TwiML app');
}
});
}else{
this.setAppRequestUrls(app_sid, function(app){
if(app){
app_sid = app.sid;
callback(app)
}else{
throw new Error('Failed updating app urls');
}
});
}
}
Configure.prototype.setAppRequestUrls = function(appSid, callback){
try{
params = {
VoiceUrl: this.voice_url,
SmsUrl: this.sms_url,
FriendlyName: 'Hackpack for Heroku and Express'
}
this.client.updateApplication(appSid, params, function(body){
callback(body);
});
} catch (e) {
throw new Error('Could not update application');
}
}
Configure.prototype.getHerokuHostName = function(){
var subdomain;
// Try to read the git config file.
try{
var array = fs.readFileSync('./.git/config').toString().split('\n');
} catch (e) {
throw new Error('Could not read ./.git/config file, does it still exist? Failed path: ' + e);
}
for(line in array){
if(array[line].indexOf('[email protected]') != -1){
var splitString = array[line].split(':');
subdomain = splitString[1].replace('.git', '');
console.info('Heroku remote found: '+ subdomain);
}
}
// Return Heroku host name.
if(subdomain){
var host = 'http://' + subdomain + '.herokuapp.com'
console.info('Full host is ' + host);
return host;
}else{
throw new Error('Could not find Heroku remote in ./.git/config. Have you created the Heroku app?');
}
}
Configure.prototype.setAccountSid = function(accSid){
this.account_sid = accSid;
process.env.TWILIO_ACCOUNT_SID = this.account_sid;
}
Configure.prototype.setAuthToken = function(auth){
this.auth_token = auth;
process.env.TWILIO_AUTH_TOKEN = this.auth_token;
}
Configure.prototype.setAppSid = function(appSid){
this.app_sid = appSid
process.env.TWILIO_APP_SID = this.app_sid;
}
Configure.prototype.setPhoneNumber = function(callerId){
this.phone_number = callerId;
process.env.TWILIO_CALLER_ID = this.phone_number;
}
Configure.prototype.setVoiceUrl = function(voiceUrl){
this.voice_url = voiceUrl;
}
Configure.prototype.setSmsUrl = function(smsUrl){
this.sms_url = smsUrl;
}
Configure.prototype.setDomain = function(domain){
this.host_ = domain;
}
var configure = new Configure();
if(program.account_sid){
configure.setAccountSid(program.account_sid);
}
if(program.auth_token){
configure.setAuthToken(program.auth_token);
}
if(program.app_sid){
configure.setAppSid(program.app_sid);
}
if(program.caller_id){
configure.setPhoneNumber(program.caller_id);
}
if(program.voice){
configure.setVoiceUrl(program.voice);
}
if(program.sms){
configure.setSmsUrl(program.sms);
}
if(program.domain){
configure.setDomain(program.domain);
}
configure.start();