-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
286 lines (254 loc) · 8.28 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
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
// Wit.ai imports
const Wit = require('node-wit').Wit;
const log = require('node-wit').log;
// Webserver parameter
const PORT = process.env.PORT || 8445;
// Wit.ai parameters
const WIT_TOKEN = process.env.WIT_TOKEN;
app.set('port', (process.env.PORT || 8000));
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// Process application/json
app.use(bodyParser.json());
// Index route
app.get('/', function (req, res) {
res.send('Hello World, this is chatbot');
})
// for Facebook verification
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'test') {
res.send(req.query['hub.challenge']);
}
res.send('Error, wrong token');
});
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'));
});
// WIT.AI specific code
// ------------------------------------------------
/**
* These are the actions for the Wit.ai bot to use
* They are mapped to certain inputs using wits NLP awesomeness
*/
const actions = {
send({sessionId}, {text}) {
// Our bot has something to say!
// Giving the wheel back to our bot
// We return a promise to let our bot know when we're done sending
return Promise.resolve();
},
// This is the action that is called when a request
// for stories is recognized by wit.ai...we can modify
// the context object here
getTopStories({context, entities}) {
return Promise.resolve(context);
}
};
// Setting up our bot
const wit = new Wit({
accessToken: WIT_TOKEN,
actions,
logger: new log.Logger(log.INFO)
});
//main chron url
var homeUrl = "http://www.dukechronicle.com/";
/**
* Recieve and handle webhook post request
* We then make a call to the Wit.ai api to interpret the
* message and send back structured data about its intent
*/
app.post('/webhook/', function(req, res) {
// Parse the Messenger payload
// See the Webhook reference
// https://developers.facebook.com/docs/messenger-platform/webhook-reference
const messaging_events = req.body.entry[0].messaging
// Iterate over each messaging event, send the message to wit.ai and
// deal with the resonse accordingly
for (let i = 0; i < messaging_events.length; i++) {
const event = req.body.entry[0].messaging[i];
const sender = event.sender.id;
if (event.message && event.message.text) {
// 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
// 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't currently process attachments - please send me a message"
});
} 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.message(
text, // the user's message
{}
).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');
getTopStories(context, webhook_callback, sender, res);
})
.catch((err) => {
console.error('Oops! Got an error from Wit: ', err.stack || err);
})
} else {
console.log('received event', JSON.stringify(event));
}
}
}
// send an ok status
res.sendStatus(200);
});
/**
* Function called when the chronicle JSON response has been
* recieved
* @param {Object} chron_json - The articles in JSON
* @param {String} sender - The id of the sender - who will be the
* recipient of the response
* @param {Object} res - The res object from the /webhook/ post
*/
function webhook_callback (chron_json, sender, res) {
if (chron_json) {
console.log("chron_json");
} else {
console.log("chron_json is null");
}
sendNewsCards(sender, chron_json[0]['articles']);
}
// Helper functions
// ----------------------------------------------
//facebook app token
const token = process.env.token;
/**
* News card to be sent to the user showing the top news
* for a certain section.
*/
class Card {
constructor(title, subtitle, image_url, article_url) {
this.title = title;
this.subtitle = subtitle;
this.image_url = image_url;
this.buttons = [{type: "web_url", url: article_url, title: "View article" }];
}
}
/**
* Function to send a generic message to a facebook user
* @param { String } recipientId - The id of the recipient
* @param { Object } message - The structured message to send
*/
function fbMessage(recipientId, message) {
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: {
recipient: { id: recipientId },
message,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
})
}
/**
* Template for messenger json response used in the
* sendNewsCards fuction
* @param {Array} elements - The card elements to be sent
* @return {Object} - The template for a fb card message
*/
function buildJsonReponse(elements) {
return {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements,
}
}
}
}
/**
* Send news cards with the top chronicle stories in a
* certain topic back to the user
* @param {String} sender - The id of the messenger recipient
* @param {Object} items - The news items
*/
function sendNewsCards(sender, items) {
let elems = [];
let i = 0;
for (var key in items) {
if (i > 5) { break }
const elem = items[key];
const title = finishDecode(decodeURIComponent(elem['headline']));
const subtitle = finishDecode(decodeURIComponent(elem['subhead']));
const url = finishDecode(decodeURIComponent(elem['getURL']));
// Make the image URL either the image in the article of a default
const image_url = '0' in elem['media'] ?
finishDecode(decodeURIComponent(elem['media']['0']['urlPreview'])) :
"http://ds4q8c259towh.cloudfront.net/20160415XJlq9-dXcb/dist/img/dtc-ph.png";
elems.push(new Card(title, subtitle, image_url, url));
i++;
}
// Send the message
fbMessage(sender, buildJsonReponse(elems))
}
/**
* Function that returns the top stories of a given category back to
* a user.
*
* @param {Object} context - The wit.ai context object for the message
* reponse - contains WIT's interpretation of the message, including
* which category it thinks the user wants to access
* @param {Function} callback - callback function on completion
* @param {String} sender - The id of the sender - who will be the
* recipient of the response
* @param {Object} webhook_response - The res object from the /webhook/
* post
* @return {Object} - JSON of the first page of stories in the given
* category
*/
function getTopStories(context, callback, sender, webhook_response) {
/** If we dont actually have a message */
if (!context['entities']) { return }
// show the user that you are getting the stories
const { value, confidence } = context.entities.intent[0];
const url = homeUrl + 'section/' + value + ".json";
let chron_json = "";
fbMessage(sender, {
text: `Finding the top ${value} stories`
});
request(url, function (error, response, body) {
if (error) {
console.log(error);
} else if (response.body.error) {
console.log(response.body.error);
} else {
chron_json = JSON.parse(body);
}
callback(chron_json, sender, webhook_response);
});
}
/**
* Safely decode the chron article text information recieved
* from the API
* @param {String} safe - Encoded URI
*/
function finishDecode(safe) {
return safe.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
}