-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (95 loc) · 3.35 KB
/
Copy pathserver.js
File metadata and controls
109 lines (95 loc) · 3.35 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
/* https://github.com/tropo/tropo-webapi-node */
var tropowebapi = require('tropo-webapi');
/* Express framwork http://expressjs.com */
var express = require('express');
/* http://mongoosejs.com/ */
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/track');
mongoose.connect(URI);
//connect to mongodb
var db = mongoose.connection;
var Tracking;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
//new schema
var trackingSchema = mongoose.Schema({
from: String,
choice: String
});
Tracking = mongoose.model('Tracking', trackingSchema);
});
var app = express();
var from;
/* constants */
var RECRUITER = "1";
var OTHER = "2";
var EMAIL = 'example@gmail.com';
var BIRTHYEAR = '1111';
var TRANSFERNUM = '1111111111';
var FIRSTNAME = "Jon";
var LASTNAME = "Culver";
app.configure(function(){
app.use(express.bodyParser());
});
app.post('/', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new tropowebapi.TropoWebAPI();
from = req.body['session']['from']['id'];
tropo.say("Hi, you have reached "+FIRSTNAME+" "+ LASTNAME +"");
var say = new Say("If you are a recruiter please press 1. Otherwise press 2.");
var choices = new Choices("1,2");
tropo.ask(choices, 3, false, null, "digit", null, true, say, 5, null);
// use the on method https://www.tropo.com/docs/webapi/on.htm
tropo.on("continue", null, "/answer", true);
res.send(tropowebapi.TropoJSON(tropo));
});
app.post('/answer', function(req, res) {
var tropo = new tropowebapi.TropoWebAPI();
var response = req.body['result']['actions']['interpretation'];
/* log response */
var info = new Tracking({ from: from, choice: response });
info.save(function (err, first) {
if (err) {
console.log("error");
} else {
console.log("added");
}
});
/* if recruiter */
if(response == RECRUITER) {
tropo.say(""+FIRSTNAME+"'s email address has been sent via text message.");
var emailSay = new Say(EMAIL);
// use the message method https://www.tropo.com/docs/webapi/message.htm
tropo.message(emailSay, from, null, null, null, "message", "SMS", null, null, null);
tropo.say("Good bye.");
} else if(response == OTHER) {
var say = new Say("Please enter the year "+FIRSTNAME+" was born.");
var choices = new Choices("[4 DIGITS]");
tropo.ask(choices, 3, false, null, "year", null, true, say, 5, null);
// use the on method https://www.tropo.com/docs/webapi/on.htm
tropo.on("continue", null, "/verify", true);
} else {
tropo.say("Good Bye.");
}
res.send(tropowebapi.TropoJSON(tropo));
});
app.post('/verify', function(req, res) {
var tropo = new tropowebapi.TropoWebAPI();
var response = req.body['result']['actions']['interpretation'];
//console.log(response);
if(response == BIRTHYEAR) {
tropo.say("Thank you. Transferring you now, please wait.");
tropo.transfer(TRANSFERNUM);
} else {
tropo.say(""+FIRSTNAME+"'s email address has been sent via text message.");
var emailSay = new Say(EMAIL);
// use the message method https://www.tropo.com/docs/webapi/message.htm
tropo.message(emailSay, from, null, null, null, "message", "SMS", null, null, null);
tropo.say("Good bye.");
}
res.send(tropowebapi.TropoJSON(tropo));
});
//for uploading to heroku
var port = process.env.PORT || 8080;
app.listen(port);
console.log('Server running on http://0.0.0.0:'+port+'/')