-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (133 loc) · 4.05 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
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
var Bandwidth = require("node-bandwidth");
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var http = require("http").Server(app);
var myBWNumber = process.env.BANDWIDTH_PHONE_NUMBER;
var myCreds = {
userId : process.env.BANDWIDTH_USER_ID,
apiToken : process.env.BANDWIDTH_API_TOKEN,
apiSecret : process.env.BANDWIDTH_API_SECRET
};
var client = new Bandwidth(myCreds);
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 3000));
app.get("/", function (req, res) {
res.send("Hello World");
});
app.post("/message-callback", function (req, res) {
var body = req.body;
res.sendStatus(200);
if (body.direction === "in") {
var numbers = {
to: body.from,
from: body.to
}
sendMessage(numbers);
}
});
app.post("/outbound-callbacks", function (req, res) {
var body = req.body;
console.log(body);
if (isAnswer(body.eventType)) {
speackSentenceInCall(body.callId, "Hello from Bandwidth")
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
}
else if (isSpeakingDone(body)) {
client.Call.hangup(body.callId)
.then(function () {
console.log("Hanging up call");
})
.catch(function (err) {
console.log("Error hanging up the call, it was probably already over")
console.log(err);
});
}
});
app.post("/calls", function (req, res) {
var callbackUrl = getBaseUrl(req) + "/outbound-callbacks";
var body = req.body;
var phoneNumber = body.phoneNumber;
createCallWithCallback(phoneNumber, myBWNumber, callbackUrl)
.then(function (call) {
console.log(call);
res.send(call).status(201);
})
.catch(function (err) {
console.log(err);
console.log("ERROR CREATING CALL 😢");
});
});
var isAnswer = function (eventType) {
return (eventType === "answer");
}
var isSpeakingDone = function (callBackEvent) {
return (callBackEvent.eventType === "speak" && callBackEvent.state === "PLAYBACK_STOP");
}
var createCallWithCallback = function (toNumber, fromNumber, callbackUrl) {
return client.Call.create({
from: fromNumber,
to: toNumber,
callbackUrl: callbackUrl
});
};
var speackSentenceInCall = function (callId, sentence) {
return client.Call.speakSentence(callId, sentence);
}
app.post("/call-callback", function (req, res) {
var body = req.body;
res.sendStatus(200);
if (body.eventType === "answer"){
client.Call.speakSentence(body.callId, "I still really like dogs better")
.then(function () {
console.log("speakSentence sent");
})
.catch(function (err) {
console.log(err);
});
}
else if (body.eventType === "speak" && body.state === "PLAYBACK_STOP") {
client.Call.hangup(body.callId)
.then(function () {
console.log("Hanging up call");
})
.catch(function (err) {
console.log("Error hanging up the call, it was probably already over")
console.log(err);
});
}
else {
console.log(body);
}
});
var getBaseUrl = function (req) {
return 'http://' + req.hostname;
};
var messagePrinter = function (message) {
console.log('Using the message printer');
console.log(message);
}
var sendMessage = function (params) {
return client.Message.send({
from : params.from,
to : params.to,
text : "404 Cats are Great, but still not as awesome as dogs",
media: "http://s.quickmeme.com/img/a8/a8022006b463b5ed9be5a62f1bdbac43b4f3dbd5c6b3bb44707fe5f5e26635b0.jpg"
})
.then(function(message){
messagePrinter(message);
return client.Message.get(message.id);
})
.then(messagePrinter)
.catch(function (err) {
console.log(err);
});
}
http.listen(app.get('port'), function(){
console.log('listening on *:' + app.get('port'));
});