-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
216 lines (169 loc) · 4.92 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
var ejs = require('ejs');
var settings = require('./settings.json');
var twilio = require('twilio');
var client = twilio(settings.accountSid, settings.authToken);
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'public'));
app.set('view engine', 'ejs');
app.engine('.html', ejs.__express);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res) {
res.render('index.html');
});
app.get('/messages', function(req, res) {
res.render('message.html');
});
app.get('/voice', function(req, res) {
res.render('voice.html');
});
app.get('/contact', function(req, res) {
res.render('contact.html');
});
app.post('/messages/send', function(req, res) {
console.log('/messages/send', req.body);
// check password
var crypto = require('crypto');
var hash = crypto.createHash('sha256');
hash.update(req.body.password);
hash = hash.digest('hex');
if (hash !== settings.password) {
res.send(200, {
message: 'wrong password',
status: 400
});
res.end();
return;
}
// send message
client.sendMessage(req.body, function(err, data) {
if (err) {
console.error('/messages/send error:', err, data);
res.send(200, err);
res.end();
return;
}
console.log('/messages/send success:', err, data);
res.send(200, {
sid: data.sid,
date_created: data.date_created,
from: data.from,
to: data.to,
body: data.body,
price: data.price,
status: data.status
});
res.end();
});
});
app.post('/messages/receive', function(req, res) {
console.log('/messages/receive body', req.body);
//Create TwiML response
var twiml = new twilio.TwimlResponse();
// TODO see: https://www.twilio.com/docs/api/twiml/sms/twilio_request req.body.Body
twiml.message('Really? Well what do you mean with: ' + req.body.Body);
res.type('text/xml');
res.send(200, twiml.toString());
});
app.post('/voice/combox', function(req, res) {
console.log('/voice/combox body', req.body);
//Create TwiML response
var twiml = new twilio.TwimlResponse();
// TODO see: https://www.twilio.com/docs/api/twiml/sms/twilio_request req.body.Body
twiml.say('Leave your message after the beep', {
voice: 'woman',
language: 'en-gb'
}).record({
transcribe: 'true',
timeout: '6',
playBeep: 'true',
action: '/voice/thankyou',
method: 'POST'
});
res.type('text/xml');
res.send(200, twiml.toString());
});
app.post('/voice/receive', function(req, res) {
console.log('/voice/receive body', req.body);
//Create TwiML response
var twiml = new twilio.TwimlResponse();
// TODO see: https://www.twilio.com/docs/api/twiml/sms/twilio_request req.body.Body
twiml.say('Pinkepank der Schmid ist krank, wo soll er wohnen unten oder oben?', {
voice: 'woman',
language: 'de'
}).pause({
length: '1'
}).gather({
timeout: '8',
finishOnKey: '1234567890',
action: '/voice/receive/gather',
method: 'POST'
}, function() {
this.say('To repeat press 1, for oben press 2, for unten press 3', {
voice: 'men',
language: 'en'
})
});
res.type('text/xml');
res.send(200, twiml.toString());
});
app.post('/voice/receive/gather', function(req, res) {
console.log('/voice/receive/gather body', req.body.Digits, req.body);
//Create TwiML response
var twiml = new twilio.TwimlResponse();
var digits = req.body.Digits;
if (digits === 1) twiml.redirect('/voice/receive', {
method: 'POST'
});
var answer = Math.round(Math.random());
console.log('/voice/receive/gather answer', answer);
if (digits === answer) {
twiml.say('you cracked Pinkepank, congratulations. cheers.', {
voice: 'woman',
language: 'en-gb'
}).pause({
length: '1'
}).hangup();
} else {
twiml.say('sorry, no luck this time. cheers.', {
voice: 'woman',
language: 'en-gb'
}).pause({
length: '1'
}).hangup();
}
res.type('text/xml');
res.send(200, twiml.toString());
});
app.post('/voice/thankyou', function(req, res) {
console.log('/voice/combox body', req.body);
//Create TwiML response
var twiml = new twilio.TwimlResponse();
// TODO see: https://www.twilio.com/docs/api/twiml/sms/twilio_request req.body.Body
twiml.say('Thank you', {
voice: 'woman',
language: 'en-gb'
}).hangup();
res.type('text/xml');
res.send(200, twiml.toString());
});
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});