-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·198 lines (175 loc) · 5.54 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
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
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var env = require('./config.js');
var twilio = require('twilio');
var FB = require('fb');
var path = require("path");
// Create Express Webapp
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('public'));
// Basic health check - check environment variables have been configured
// correctly
app.get('/config', function(request, response) {
response.render('index.jade',{
TWILIO_ACCOUNT_SID: env.TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN: env.TWILIO_AUTH_TOKEN,
TWILIO_NOTIFICATION_SERVICE_SID: env.TWILIO_NOTIFICATION_SERVICE_SID,
FACEBOOK_PAGE_ACCESS_TOKEN: env.FACEBOOK_PAGE_ACCESS_TOKEN
});
});
function createBinding(opts){
// Authenticate with Twilio
var client = new twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN);
// Get a reference to the user notification service instance
var service = client.notify.v1.services(env.TWILIO_NOTIFICATION_SERVICE_SID);
service.bindings.create(opts).then(function(binding) {
var message = 'Binding created!';
console.log(message + " " + binding.bindingType);
}).catch(function(error) {
var message = 'Failed to create binding: ' + error;
console.log(message);
});
}
//Create a binding using device properties
app.post('/register', function(request, response) {
createBinding({
identity: request.body.identity,
endpoint: request.body.endpoint,
bindingType: request.body.BindingType,
address: request.body.Address
});
response.send({
message:"Binding created!"
});
});
//Create a facebook-messenger binding based on the authentication webhook from Facebook
app.post('/messenger_auth', function(request, response) {
//Extract the request received from Facebook
var message = request.body.entry[0].messaging[0];
console.log(message);
var identity = message.optin.ref;
var endpoint = 'FBM@' + message.recipient.id + identity;
//Let's create a new facebook-messenger Binding for our user
createBinding({
identity: identity,
endpoint: endpoint,
bindingType: 'facebook-messenger',
address: message.sender.id
});
response.send({
message:"Binding created!"
});
});
//Verification endpoint for Facebook needed to register a webhook.
app.get('/messenger_auth', function(request, response) {
console.log(request.query["hub.challenge"]);
response.send(request.query["hub.challenge"]);
});
function addTag(binding, tag){
var index = binding.tags.indexOf(tag);
if (index > -1){
//Do nothing all is well
}
else {
binding.tags.push(tag);
console.log("Added tag: " + binding.tags);
}
};
function removeTag(binding, tag) {
var index = binding.tags.indexOf(tag);
if (index > -1){
binding.tags.splice(index, 1);
console.log("Removed tag: " + binding.tags);
} else {
//Do nothing
}
}
app.post('/subscribe', function(request,response){
console.log(request.body);
var preferred = request.body.preferred;
var identity = request.body.identity;
var marketingEnabled = request.body.marketingEnabled;
var number = request.body.number;
// Get a reference to the user notification service instance
var service = new twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN).notify.v1.services(env.TWILIO_NOTIFICATION_SERVICE_SID);
service.bindings.each({
identity:identity,
done:function(){
console.log("Done");
response.send({
message:"Ok"
});
}
}, function(binding, onComplete){
console.log("Processing binding: " + binding.endpoint);
console.log("Starting tags: " + binding.tags);
if (marketingEnabled === "true"){
addTag(binding, "marketingEnabled");
} else {
removeTag(binding, "marketingEnabled");
}
if (binding.bindingType === preferred){
addTag(binding, "preferred");
} else {
removeTag(binding, "preferred");
}
console.log("Ending tags: " + binding.tags);
createBinding({
identity: binding.identity,
endpoint: binding.endpoint,
bindingType: binding.bindingType,
address: binding.address,
tag: binding.tags
});
});
if (number) {
console.log("Registering new SMS binding");
var tags =[];
if (marketingEnabled === "true"){
tags.push("marketingEnabled");
}
if (preferred === "sms"){
tags.push("preferred");
}
createBinding({
identity: identity,
endpoint: "SMS@" + number + identity,
bindingType: 'sms',
address: number,
tag: tags
});
}
});
app.get('/bindings', function(request, response) {
var identity = request.query["Identity"];
var service = new twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN).notify.v1.services(env.TWILIO_NOTIFICATION_SERVICE_SID);
service.bindings.list({
identity:identity
}).then(function(bindings){
var result = [];
bindings.forEach(function(binding){
result.push({
endpoint: binding.endpoint,
bindingType:binding.bindingType,
address:binding.address,
tags:binding.tags
});
});
console.log(result);
response.send(result);
}).catch(function(error){
console.log(error);
response.status(500).send({
message:"Could not list bindings: " + error
});
});
});
// Start HTTP server
var server = http.createServer(app);
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Express server running on *:' + port);
});