-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
228 lines (177 loc) · 5.93 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
'use strict';
var q = require('q');
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var fs = require('fs');
var path = require('path');
var chat = require('./chat_utils.js')
var array_utils = require('./utils.js').arrays;
/// import relevant plugins
var minerva_search = require('./plugins').minerva_search;
var help = require('./plugins').help;
var conversation = require('./plugins').conversation;
var showmore = require('./plugins').showmore;
var catalog_search = require('./plugins').catalog_search;
var major_search = require('./plugins').major_search;
var building_search = require('./plugins').building_search;
var uprint_search = require('./plugins').uprint_search;
// set up web server
var app = express();
app.set('port', ( process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 5000 ) );
app.set('ip', ( process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1') );
app.use(function(req,res,next){
var _send = res.send;
var sent = false;
res.send = function(data){
if(sent) return;
_send.bind(res)(data);
sent = true;
};
next();
});
// Process application/x-www-form-urlencoded
app.use( bodyParser.urlencoded( { extended:false} ) );
app.use( bodyParser.json() );
// context management:
var contexts;
try{
contexts = JSON.parse(fs.readFileSync(path.resolve( ( process.env.OPENSHIFT_DATA_DIR || './' ), 'history.json'), 'utf8')).contexts || {};
}catch(err){
contexts = {};
}
function get_or_create_context(user){
if(!contexts[user]){
contexts[user] = {extracted:[], fails:0};
}
return contexts[user];
}
chat.menu([
['postback', 'Help', 'help@'],
['web_url', 'Source Code (V0.2.1)', 'https://github.com/zafarali/minerva-bot/']
])
app.get('/', function (req, res) {
// storage.push(req.query['to_push']);
res.send({message:'minerva bot backend!'});
});
app.get('/webhook', function (req, res) {
// USED FOR SET UP:
if(req.query['hub.verify_token'] == process.env['VERIFYTOKEN']) {
res.send(req.query['hub.challenge']);
}
res.send('Error, wrong token');
});
app.post('/webhook/', function (req, res) {
// handles messages and postbacks
var messaging_events = req.body.entry[0].messaging;
for(var i=0; i < messaging_events.length; i++){
var event = messaging_events[i];
var sender = event.sender.id;
// handle explicit text messaging events or postbacks
if( (event.message && event.message.text )
|| (event.postback && event.postback.payload ) ){
// set a welcome message
try{
chat.welcome();
}catch(e){
console.log('Error setting welcome message.')
}
chat.set_state(sender, 'mark_seen');
var query, postback;
if(event.postback){
query = '' // if postback is defined, then query must be blank
postback = event.postback.payload;
}else{
query = event.message.text;
postback = '' // message is defined, then postback must be blank
if(event.message.quick_reply){
postback = event.message.quick_reply.payload // handle new payload type
}
}
console.log('query recieved:', query);
console.log('postback recieved:', postback);
internals(query, sender, postback).then(function(ctx){
console.log('response computed.');
chat.set_state(sender, 'typing_on')
if(ctx.replies.length === 0){
ctx.history.fails = ctx.history.fails + 1;
ctx.replies.push(
array_utils.random_choice(
[
'I don\'t understand what you\'re asking me :(. I\'m always improving.',
'Didn\'t get that.',
'What was that?',
'Hmm.. not sure what you are asking me about...',
'Ask me something more descriptive?' ]
)
);
}else{
ctx.history.fails = 0;
}
if(ctx.history.fails>1){
console.log('Person needs help!!')
ctx.replies.push(
chat.builders.quick_reply(
array_utils.random_choice(
['You seem confused. ', 'Are you confused? ', 'Seems like we aren\'t on the same page. ']
) +
array_utils.random_choice(
['Try asking me for HELP?', 'Ask me to HELP you?', 'Would you like me to HELP you?', 'Type HELP to see what I can do!']
),
[['Help', 'help@'],['Nah', 'negative@']]
));
}
// schedule replies every 200ms
chat.reply2(sender,ctx.replies)
}).then(function(){
chat.set_state(sender, 'typing_off')
console.log('reply chain complete');
}).catch(function(err){
chat.set_state(sender, 'typing_off')
console.log('error occured:',err)
chat.reply2(sender, ["Something went wrong... Try again!"]);
// res.send({error:err});
});
}
}
res.sendStatus(200);
});
// internal chain logic for the purposes of abstraction
function internals(query, user, postback){
// plugins to be executed
var to_execute = [ conversation, help, uprint_search, building_search, showmore, minerva_search, catalog_search, major_search ];
// obtain a context
var history = get_or_create_context(user);
var context = {
history: history,
current_query: query,
postback:postback,
completed: false,
replies:[]
}
return to_execute.reduce(q.when, q(context));
}
app.get('/dump_history/', function(req, res){
// dumps all user contexts to the file system and out
// only works if the token query is correct
// for "security"
if(req.query['token'] == process.env['VERIFYTOKEN']) {
// dumps all user contexts to the file system and out
var contexts_string = JSON.stringify({contexts:contexts});
try{
fs.writeFile( ( process.env.OPENSHIFT_DATA_DIR || './' )+'history.json', contexts_string, function (err) {
if (err) throw err;
console.log('Saved 1');
});
}catch(err){
fs.writeFile( ( process.env.OPENSHIFT_DATA_DIR +'/' || './' )+'history.json', contexts_string, function (err) {
if (err) throw err;
console.log('Saved 2');
});
}
res.send({contexts:contexts});
}
});
app.listen( app.get('port'), app.get('ip'), function() {
console.log('listening on', app.get('ip'), ' on port ', app.get('port') );
});