-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
290 lines (236 loc) · 9.64 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'use strict';
// Messenger API integration example
// We assume you have:
// * a Wit.ai bot setup (https://wit.ai/docs/quickstart)
// * a Messenger Platform setup (https://developers.facebook.com/docs/messenger-platform/quickstart)
// You need to `npm install` the following dependencies: body-parser, express, request.
//
const request = require('request');
const bodyParser = require('body-parser');
const express = require('express');
const crypto = require('crypto');
const fetch = require('node-fetch');
const Wit = require('node-wit').Wit;
const log = require('node-wit').log;
// get Bot, const, and Facebook API
//const bot = require('./bot.js');
const Config = require('./const.js');
const FB = require('./facebook.js');
var fs = require("fs");
var https = require('https');
// Setting up our bot
// Webserver parameter
const PORT = process.env.PORT || 8000;
// Wit.ai bot specific code
// This will contain all user sessions.
// Each session has an entry:
// sessionId -> {fbid: facebookUserId, context: sessionState}
const sessions = {};
const findOrCreateSession = (fbid) => {
let sessionId;
// Let's see if we already have a session for the user fbid
Object.keys(sessions).forEach(k => {
if (sessions[k].fbid === fbid) {
// Yep, got it!
sessionId = k;
}
});
if (!sessionId) {
// No session found for user fbid, let's create a new one
sessionId = new Date().toISOString();
sessions[sessionId] = {
fbid: fbid,
context: {
_fbid_: fbid
}
}; // set context, _fid_
}
return sessionId;
};
// Our bot actions
const actions = {
send({sessionId}, response) {
// Our bot has something to say!
// Let's retrieve the Facebook user whose session belongs to
const recipientId = sessions[sessionId].fbid;
if (recipientId) {
if (IsJsonString(response.text)) {
response.attachment = response.text;
delete response.text;
}
if (typeof response.quickreplies != undefined && response.quickreplies) { // Wit.ai wants us to include quickreplies, alright!
response.quick_replies = []; // The quick reply object from Wit.ai needs to be renamed.
for (var i = 0, len = response.quickreplies.length; i < len; i++) { // Loop through quickreplies
response.quick_replies.push({ title: response.quickreplies[i], content_type: 'text', payload: 'CUSTOM_WIT_AI_QUICKREPLY_ID' + i });
}
delete response.quickreplies;
}
console.log("=====================>response", response)
// Yay, we found our recipient!
// Let's forward our bot response to her.
// We return a promise to let our bot know when we're done sending
return FB.fbMessage(recipientId, response)
.then(() => null)
.catch((err) => {
console.error(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err.stack || err
);
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
// Giving the wheel back to our bot
return Promise.resolve()
}
},
getPnrStatus({context, entities}) {
return new Promise(function(resolve, reject) {
//var pnr= entities.pnr[0].value;
console.log("PNR============>",entities);
request('http://api.railwayapi.com/pnr_status/pnr/1234567890/apikey/7qv3y07t/', function (error, response, body) {
if(error) {
context.PnrStatus = "Sorry! something went wrong";
return resolve(context);
}
if (!error && response.statusCode == 200) {
context.PnrStatus = body.train_name;
return resolve(context);
}
else if (!error && response.statusCode == 410) {
context.PnrStatus = "Flushed PNR / PNR not yet generated";
return resolve(context);
}
else if (!error && response.statusCode == 404) {
context.PnrStatus = "Service Down / Source not responding";
return resolve(context);
}
});
});
}
};
const wit = Wit({
accessToken: Config.WIT_TOKEN,
actions,
logger: new log.Logger(log.INFO)
});
// Starting our webserver and putting it all together
const app = express();
app.set('port', PORT);
app.listen(app.get('port'));
app.use(({ method, url }, rsp, next) => {
rsp.on('finish', () => {
console.log(`${rsp.statusCode} ${method} ${url}`);
});
next();
});
app.use(bodyParser.json());
// Webhook setuptext
app.get('/webhook', (req, res) => {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === Config.FB_VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
} else {
res.sendStatus(400);
}
});
// index. Let's say something fun
app.get('/', function(req, res) {
res.send('"Bot is healthy!."');
});
// Message handler
app.post('/webhook', (req, res) => {
// Parse the Messenger payload
// See the Webhook reference
// https://developers.facebook.com/docs/messenger-platform/webhook-reference
const data = req.body;
if (data.object === 'page') {
data.entry.forEach(entry => {
entry.messaging.forEach(event => {
if (event.message || event.postback) {
if (event.postback) {
event.message = {
text: event.postback.payload
};
}
// Yay! We got a new message!
// We retrieve the Facebook user ID of the sender
const sender = event.sender.id;
// We retrieve the user's current session, or create one if it doesn't exist
// This is needed for our bot to figure out the conversation history
const sessionId = findOrCreateSession(sender);
// We retrieve the message content
const { text, attachments } = event.message;
if (attachments) {
// We received an attachment
// Let's reply with an automatic message
fbMessage(sender, { text: 'Sorry I can only process text messages for now.' })
.catch(console.error);
} else if (text) {
// We received a text message
// Let's forward the message to the Wit.ai Bot Engine
// This will run all actions until our bot has nothing left to do
wit.runActions(
sessionId, // the user's current session
text, // the user's message
sessions[sessionId].context // the user's current session state
).then((context) => {
// Our bot did everything it has to do.
// Now it's waiting for further messages to proceed.
console.log('Waiting for next user messages');
// Based on the session state, you might want to reset the session.
// This depends heavily on the business logic of your bot.
// Example:
// if (context['done']) {
// delete sessions[sessionId];
// }
// Updating the user's current session state
sessions[sessionId].context = context;
})
.catch((err) => {
console.error('Oops! Got an error from Wit: ', err.stack || err);
})
}
} else {
console.log('received event', JSON.stringify(event));
}
});
});
}
res.sendStatus(200);
});
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
/*
* Verify that the callback came from Facebook. Using the App Secret from
* the App Dashboard, we can verify the signature that is sent with each
* callback in the x-hub-signature field, located in the header.
*
* https://developers.facebook.com/docs/graph-api/webhooks#setup
*
*/
function verifyRequestSignature(req, res, buf) {
var signature = req.headers["x-hub-signature"];
if (!signature) {
// For testing, let's log an error. In production, you should throw an
// error.
console.error("Couldn't validate the signature.");
} else {
var elements = signature.split('=');
var method = elements[0];
var signatureHash = elements[1];
var expectedHash = crypto.createHmac('sha1', Config.FB_APP_SECRET)
.update(buf)
.digest('hex');
if (signatureHash != expectedHash) {
throw new Error("Couldn't validate the request signature.");
}
}
}