-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
291 lines (233 loc) · 10.4 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
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
291
var restify = require('restify');
var builder = require('botbuilder');
var http = require('http');
var request = require('request');
const mdb = require('moviedb')('363f8477734870bd23272ace51dc0756');
// https://api.themoviedb.org/3/movie/550?api_key=363f8477734870bd23272ace51dc0756&language=pt-US
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
/******************** global dialog **********************/
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
// session.send("You said: %s", session.message.text);
session.send("Hello! My name is Stanley, your movie recommender.");
session.beginDialog('/menu');
});
bot.dialog('/menu', function(session){
var msg = "What type of movies are you looking for? \
Do you want 1) New movies or 2) Top rated movies?";
session.send(msg);
});
bot.dialog('/end', function(session){
var msg = "Glad to help you. See you next time!";
delete session.userData;
session.endConversation(msg);
})
.triggerAction({ matches: /^.*bye.*|.*goodbye.*/i });
/******* global var *************/
// var movies = []
// var titles = []
// var des = []
/************************* best of all time ******************************/
bot.dialog("bestMovies", function(session){
var request = require("request");
// discover
// var options = { method: 'GET',
// url: 'https://api.themoviedb.org/3/discover/movie',
// qs: {api_key: '363f8477734870bd23272ace51dc0756',
// language: 'en-US',
// page: '5',
// sort_by: 'vote_count.desc',
// include_adult: 'false',
// include_video: 'false'},
// body: '{}'};
// top_rated
var options = { method: 'GET',
url: 'https://api.themoviedb.org/3/movie/top_rated',
qs: {api_key: '363f8477734870bd23272ace51dc0756',
language: 'en-US',
page: '5'
},
body: '{}'
};
request(options, function(error, response, body){
if (error) throw new Error(error);
var data = JSON.parse(body);
console.log(data['results'].length);
session.userData.movies = [];
session.userData.titles = [];
session.userData.des = [];
var movies = session.userData.movies;
for (i=0; i< data['results'].length; i++){
session.userData.movies.push(data["results"][i]);
}
session.userData.titles = movies.map(function(a) {return a.title;});
session.userData.des = movies.map(function(a) {return a.overview});
/*** alias ***/
var titles = session.userData.titles;
session.send("Here are the 10 top rated movies right now: <br>"
+ '  1. ' + titles[0].toString() + '<br>'
+ '  2. ' + titles[1].toString() + '<br>'
+ '  3. ' + titles[2].toString() + '<br>'
+ '  4. ' + titles[3].toString() + '<br>'
+ '  5. ' + titles[4].toString() + '<br>'
+ '  6. ' + titles[5].toString() + '<br>'
+ '  7. ' + titles[6].toString() + '<br>'
+ '  8. ' + titles[7].toString() + '<br>'
+ '  9. ' + titles[8].toString() + '<br>'
+ '  10. ' + titles[9].toString() + '<br>'
+ "Respond 'description'(D) for detailed description, 'more'(M) for more movies, \
'restart'( R) to search again, 'end'(E) if you are set. "
);
});
})
.triggerAction({ matches: /^.*best.*$|^.*top.*rated$/i })
.beginDialogAction('showDesAction', 'topMovieDes', { matches: /^description$|D|des/i, })
.beginDialogAction('moreAction', 'showAll', { matches: /^more$|^more.*movies$|M/i })
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i })
.beginDialogAction('endAction', '/end', {matches: /end|E/i });
bot.dialog('topMovieDes', function(session){
var titles = session.userData.titles;
var des = session.userData.des;
var result = 'Here are the movies and their description: <br>';
for (var i=1; i<11; i++){
result += ' ' + i + '. ' + titles[i-1].toString() + '<br>'
+ des[i-1].toString() + '<br>';
}
result += "Respond 'back'(B) to go back to previous step, 'restart'( R) for a new search, \
'end'(E) if you are all set.";
session.send(result);
})
.beginDialogAction('returnAction', 'bestMovies', { matches: /^back$|B/i })
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i })
.beginDialogAction('endAction', '/end', {matches: /end|E/i });
bot.dialog('showAll', function(session){
var titles = session.userData.titles;
var result = 'Here are some more top rated movies: <br>';
count = 1;
for (var i=0; i< titles.length; i++) {
result += ' ' + count + '. ' + titles[i].toString() + '<br>';
count += 1;
}
result += "Respond 'description'(D) for detailed description, 'back'(B) to previous step, \
'restart'( R) for a new search, 'end'(E) if you are all set.";
console.log(result);
session.send(result);
})
.beginDialogAction('descriptionLongAction', 'topDesLong', {matches: /^description$|^des$|D$/i})
.beginDialogAction('newMoviesAction', 'bestMovies', { matches: /^back$|B/i })
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i });
bot.dialog('topDesLong', function(session){
var titles = session.userData.titles;
var des = session.userData.des;
var result = 'Here is the description for all the top rated movies: <br>';
count = 1;
for (var i=0; i< titles.length; i++) {
result += ' ' + count + '. ' + titles[i].toString() + '<br>'
+ des[i].toString() + '<br>';
count += 1;
}
result += "Respond 'back'(B) to go back to previous step, 'restart'( R) for a new search, \
'end'(E) if you are all set."
session.send(result);
}).beginDialogAction('moreMoviesAction', 'showAll', {matches: /back|B/i})
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i })
.beginDialogAction('endAction', '/end', {matches: /end|E/i })
/***************************** new movies ********************************/
bot.dialog('newMovies', function(session){
var request = require("request");
var options = { method: 'GET',
url: 'https://api.themoviedb.org/3/movie/now_playing',
qs:
{ page: '1',
language: 'en-US',
api_key: '363f8477734870bd23272ace51dc0756' },
body: '{}' };
request(options, function (error, response, body) {
if (error) throw new Error(error);
var data = JSON.parse(body);
/****** global var *******/
session.userData.movies = [];
session.userData.titles = [];
session.userData.des = [];
var movies = session.userData.movies;
for (i=0; i<10; i++){
session.userData.movies.push(data["results"][i]);
}
session.userData.titles = movies.map(function(a) {return a.title;});
session.userData.des = movies.map(function(a) {return a.overview});
/*** alias ***/
var titles = session.userData.titles;
session.send("Here are the top 5 now playing movies right now: <br>"
+ '  1. ' + titles[0].toString() + '<br>'
+ '  2. ' + titles[1].toString() + '<br>'
+ '  3. ' + titles[2].toString() + '<br>'
+ '  4. ' + titles[3].toString() + '<br>'
+ '  5. ' + titles[4].toString() + '<br>'
+ "Respond 'description'(D) for detailed description, 'more'(M) for more movies, \
'restart'( R) to search again, 'end'(E) if you are set. "
);
});
})
.triggerAction({
matches: /new.*$|^new.*movies$/i,
})
.beginDialogAction('showDesAction', 'showDescription', { matches: /^description$|D|des/i, })
.beginDialogAction('moreMoviesAction', 'moreMovies', { matches: /^more$|^more.*movies$|M/i })
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i })
.beginDialogAction('endAction', '/end', {matches: /end|E/i });
bot.dialog('showDescription', function(session){
var titles = session.userData.titles;
var des = session.userData.des;
session.send("The movie description: <br>"
+ '  1. ' + titles[0].toString() + des[0].toString() + '<br>'
+ '  2. ' + titles[1].toString() + des[1].toString() + '<br>'
+ '  3. ' + titles[2].toString() + des[2].toString() + '<br>'
+ '  4. ' + titles[3].toString() + des[3].toString() + '<br>'
+ '  5. ' + titles[4].toString() + des[4].toString() + '<br>'
+ "Respond 'back'(B) to go back to previous step, 'restart'( R) for a new search, \
'end'(E) if you are all set.");
})
.beginDialogAction('newMoviesAction', 'newMovies', { matches: /^back$|B/i })
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i })
.beginDialogAction('endAction', '/end', {matches: /end|E/i });
/******************** more movies ************************/
bot.dialog('moreMovies', function(session){
var titles = session.userData.titles;
session.send("Here are some more movies on air: <br>"
+ '  1. ' + titles[5].toString() + '<br>'
+ '  2. ' + titles[6].toString() + '<br>'
+ '  3. ' + titles[7].toString() + '<br>'
+ '  4. ' + titles[8].toString() + '<br>'
+ '  5. ' + titles[9].toString() + '<br>'
+ "Respond 'description'(D) for detailed description, 'back'(B) to previous step, \
'restart'( R) for a new search, 'end'(E) if you are all set.");
})
.beginDialogAction('descriptionLongAction', 'descriptionLong', {matches: /^description$|^des$|D$/i})
.beginDialogAction('newMoviesAction', 'newMovies', { matches: /^back$|B/i })
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i });
bot.dialog('descriptionLong', function(session){
var titles = session.userData.titles;
var des = session.userData.des;
session.send("Here is the description for the movies: <br> "
+ '  1. ' + titles[5].toString() + des[5].toString() + '<br>'
+ '  2. ' + titles[6].toString() + des[6].toString() + '<br>'
+ '  3. ' + titles[7].toString() + des[7].toString() + '<br>'
+ '  4. ' + titles[8].toString() + des[8].toString() + '<br>'
+ '  5. ' + titles[9].toString() + des[9].toString() + '<br>'
+ "Respond 'back'(B) to go back to previous step, 'restart'( R) for a new search, \
'end'(E) if you are all set.");
})
.beginDialogAction('moreMoviesAction', 'moreMovies', {matches: /back|B/i})
.beginDialogAction('restartAction', '/menu', {matches: /restart|R/i })
.beginDialogAction('endAction', '/end', {matches: /end|E/i });