-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·144 lines (120 loc) · 4.05 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
const http = require('http'),
fs = require('fs'),
path = require('path'),
contentTypes = require('./utils/content-types'),
sysInfo = require('./utils/sys-info'),
env = process.env,
bodyParser = require('body-parser'),
request = require('request'),
express = require('express');
let fbAppToken = env.FB_APP_TOKEN;
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use('/static', express.static('static'));
app.get('/health', function(req, res){
res.writeHead(200);
res.end();
});
app.get('/info', function(req, res){
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(sysInfo[url.slice(6)]()));
});
app.get('/helloworld', function(req, res){
console.log("helloworld called");
res.end("helloworld");
sendTextMessage(12, "helloworld");
});
// for Authorization
app.get('/facebookwebhook', function (req, res) {
// if (req.query['hub.verify_token'] === fbAppToken) {
res.send(req.query['hub.challenge']);
// }
// res.send('Error, wrong validation token');
})
app.post('/facebookwebhook/', function (req, res) {
console.log("facebookwebhook called");
console.log("req", req);
var messaging_events = req.body.entry[0].messaging;
for (var i = 0; i < messaging_events.length; i++) {
var event = req.body.entry[0].messaging[i];
var sender = event.sender.id;
if (event.message && event.message.text) {
var text = event.message.text;
console.log("message : " + text);
sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200));
}
}
res.sendStatus(200);
});
// for Authorization
app.post('/transaction/complete', function (req, res) {
console.log("body:" + JSON.stringify(req.body));
console.log("comleteTrans called");
var url = 'https://callback.careem.com?PaRes=' + req.body.PaRes
// res.redirect(url);
res.send('<script> window.location.href="' + url +'";</script>')
})
// let server = http.createServer(function (req, res) {
// let url = req.url;
// if (url == '/') {
// url += 'index.html';
// }
// // IMPORTANT: Your application HAS to respond to GET /health with status 200
// // for OpenShift health monitoring
// if (url == '/health') {
// res.writeHead(200);
// res.end();
// } else if (url.indexOf('/info/') == 0) {
// res.setHeader('Content-Type', 'application/json');
// res.setHeader('Cache-Control', 'no-cache, no-store');
// res.end(JSON.stringify(sysInfo[url.slice(6)]()));
// } else if (url.indexOf('helloworld') >= 0) {
// res.end("helloworld");
// }
// else {
// fs.readFile('./static' + url, function (err, data) {
// if (err) {
// res.writeHead(404);
// res.end();
// } else {
// let ext = path.extname(url).slice(1);
// res.setHeader('Content-Type', contentTypes[ext]);
// if (ext === 'html') {
// res.setHeader('Cache-Control', 'no-cache, no-store');
// }
// res.end(data);
// }
// });
// }
// });
// server.listen(env.NODE_PORT || 3000, env.NODE_IP || 'localhost', function () {
// console.log(`Application worker ${process.pid} started...`);
// });
http.createServer(app).listen(env.NODE_PORT || 3000, env.NODE_IP || 'localhost', function () {
console.log(`Application worker ${process.pid} started...`);
});
var token = env.FB_PAGE_TOKEN;
function sendTextMessage(sender, text) {
var messageData = {
text:text
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error FB Fsending message: ', error);
} else if (response.body.error) {
console.log('Error FB : ', response.body.error);
} else {
console.log('Fb Message sent succesfully');
}
});
}