This repository has been archived by the owner on Jul 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
145 lines (132 loc) · 4.36 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
'use strict'
//start by requiring the following packages
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express();
const cheerio = require("cheerio");
app.set('port', (process.env.PORT || 5000))
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}))
// Process application/json
app.use(bodyParser.json())
// setup a route
app.get('/', function (req, res) {
res.send('Stop creeping around ._.')
});
const pageToken = process.env.TOKEN;
app.get('/webhook', function (req, res) {
if (req.query['hub.verify_token'] === FACEBOOK_VERIFY_CODE) {
res.send(req.query['hub.challenge'])
}
res.send('Error : wrong token');
})
app.post('/webhook/', async function (req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = messaging_events[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
var pnrRes;
if (!isNaN(text)) {
let url = "https://www.railyatri.in/pnr-status/" + text;
let chartStatus, trainName, dayOfBoarding;
let bookingStatus = [];
let currentStatus = [];
let pnrResult;
request(url, function (err, resp, body) {
if (err) {
throw err
};
var $ = cheerio.load(body);
if ($("#status").length != 0) {
$(".pnr-search-result-title .chart-status-txt").each(function (
index,
element
) {
chartStatus = element.children[0].data;
});
$(".pnr-search-result-info .train-info .pnr-bold-txt").each(function (
index,
element
) {
trainName = element.children[0].data;
});
$(".pnr-search-result-info .boarding-detls .pnr-bold-txt").each(function (
index,
element
) {
if (index === 0) {
dayOfBoarding = element.children[0].data;
}
});
$("#status .col-xs-4 .pnr-bold-txt").each(function (index, element) {
if (index % 2 != 0) {
let temp = element.children[0].data;
temp = temp.trim();
currentStatus.push(temp);
} else {
let temp = element.children[0].data;
temp = temp.trim();
bookingStatus.push(temp);
}
});
console.log(chartStatus, trainName, dayOfBoarding, currentStatus);
pnrResult = trainName + "\n```PNR Status:*"
for (let i = 0; i < currentStatus.length; i++) {
pnrResult = pnrResult + "\nPassenger " + (i+1) + ": \n" + "Booking Status: " + bookingStatus[i] + " , Current Status: " + currentStatus[i];
}
pnrResult = pnrResult + "```\n*" + "CHART " + chartStatus + "*";
sendText(sender, pnrResult);
} else {
pnrResult = "PNR Flushed/ PNR Doesn't Exists or Maybe the Railways Server is down. Who knows? ;_;";
sendText(sender, pnrResult);
}
});
} else {
let message;
text = text.toLowerCase();
if (text === 'hi' || text === 'hello' || text === 'hi.' || text === 'hello.' || text === 'yo') {
message = "Hello. Please enter a valid 10 digit pnr to get started.";
} else if (text === 'bye' || text === 'bye.') {
message = "Bye.";
} else {
message = "I am not sure I understood that. Please enter a valid 10 digit PNR to get started";
}
sendText(sender, message);
}
}
}
res.sendStatus(200)
})
function sendText(sender, text) {
let messageData = {
text: text
}
request({
url: "https://graph.facebook.com/v2.6/me/messages",
qs: {
access_token: pageToken
},
method: "POST",
json: {
recipient: {
id: sender
},
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log("sending error")
} else if (response.body.error) {
console.log(error)
console.log("response body error")
}
})
}
app.listen(app.get('port'), function () {
console.log('server running at : ', app.get('port'))
});