-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (66 loc) · 2.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
// STARTER CODE FOR A CLOUD FUNCTION THAT IS THE FULFILLMENT FOR AN ACTION ON GOOGLE
// Init
'use strict';
const {dialogflow, BasicCard, SimpleResponse} = require('actions-on-google');
const functions = require('firebase-functions');
const swapi = require('swapi-node');
const app = dialogflow({debug:true});
let request = require('request');
// ROOTS that reference swapi
const URL_ROOT = "https://swapi.co/api/";
const FILMS_ROOT = "films/1";
const LUKE_ROOT = "people/1";
//const { Logging } = require('@google-cloud/logging');
// SAMPLE INTENT HANDLER
app.intent("test", (conv) => {
console.log("inside first test intent");
return swapi.getPerson(1).then((result) => {
console.log(result);
console.log(result.name);
console.log(result.height);
conv.ask(new SimpleResponse({
speech: "The jedi is " + result.name + " his height is " + result.height,
text: "The jedi is " + result.name + " his height is " + result.height,
}))
});
});
// MOVIE INTENT HANDLER
app.intent("userRequestsFilms", (conv) => {
console.log("inside first test intent");
let url = URL_ROOT + FILMS_ROOT;
console.log(url);
let getFilms = (url) => {
console.log("inside getter");
return new Promise(
(resolve, reject) => {
console.log("inside promise");
request.get(url, function(error, response, data){
console.log(data);
if (error) reject(error);
// .parse does the opposite of .stringify
let content = JSON.parse(data);
let title = content.title;
let director = content.director
console.log(content);
console.log(title);
console.log(director);
//breaks
resolve(title, director);
});
}
);
};
// google function thinks everything is done and want decipher promise unless you use return here
return getFilms(url).then((param, param2) => {
console.log("title set "+ param, param2);
conv.ask(new SimpleResponse({
speech: param + " the director is " + param2,
text: param + " the director is " + param2,
}))
})
});
// TO RETURN TO DIALOGFLOW AND CONTINUE THE CONVERSATION, USE conv.ask()
// conv.ask(`Let's chat some more.`);
// TO RETURN TO DIALOGFLOW AND END THE CONVERSATION, USE conv.close()
// conv.close(`Goodbye.`);
exports.generateStarWarsUniverse = functions.https.onRequest(app);