-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
138 lines (70 loc) · 2.74 KB
/
server.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
var request = require('request');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var helpers = require('ejs-helper');
var recievedPostback = require('./recievedPostback.js');
var sendMessage = require('./sendMessage.js');
var sendMessage = require('./sendMessage.js');
var sendTextMessage = require('./sendMessage.js').sendTextMessage;
var recievedMessage = require('./recievedMessage.js').recievedMessage;
bodyParser.urlencoded();
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (req, res) {
res.render('index', {title: "Home"});
});
app.get('/webhook', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' && req.query['hub.verify_token'] === "dogs_are_cools") {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
app.post('/webhook', function (req, res) {
var data = req.body;
// Make sure this is a page subscription
if (data.object === 'page') {
// Iterate over each entry - there may be multiple if batched
data.entry.forEach(function(entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
// Iterate over each messaging event
entry.messaging.forEach(function(event) {
sendMessage.sendTypingIndicator(event.sender.id, "mark_seen");
if (event.message) {
recievedMessage(event);
} else if (event.postback) {
recievedPostback.recievedPostback(event);
} else {
console.log("Webhook received unknown event: ", event);
}
});
});
res.sendStatus(200);
}
});
app.get('/privacy-policy', function(req, res) {
res.render('privacy-policy', {title: "Privacy Policy"});
});
app.get('/send', function(req, res) {
var message = "GOOD MORNING! RISE AND SHINE FOR A BEAUTIFUL DAY 1/2!!! \nKAI CHEN REQUESTS THAT YOU GET OUT OF BED!!!";
// Steven's ID
sendTextMessage("1211307408984374", message);
sendTextMessage("1211307408984374", message);
// Below is my ID
sendTextMessage("1219226384779598", message);
sendTextMessage("1219226384779598", message);
res.render("send", {title: "Daily Message Sender"});
});
app.get('/feedback', function(req, res) {
res.render('feedback', {title: "Feedback"} );
});
// listen for requests
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});