-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
74 lines (62 loc) · 2.13 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
const express = require('express');
const app = express();
const socketIO = require("socket.io");
const dialogflow = require("@google-cloud/dialogflow");
const fetch = require("node-fetch");
const sessionClient = new dialogflow.SessionsClient({
keyFilename: process.env.KEYFILENAME
});
const projectId = "jarvis-ai-oxwx";
app.use(express.static("public"));
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});
const io = socketIO(server);
io.on("connection", (socket) => {
console.log("new user: ", socket.id);
// receive what the person said from the browser
socket.on("send to dialogflow", (data) => {
console.log(data);
sessionClient
.detectIntent({
session: sessionClient.projectAgentSessionPath(projectId, "1"),
queryInput: {
text: {
text: data.query,
languageCode: "en-US"
}
},
})
.then((response) => {
const result = response[0].queryResult;
console.log(result);
let params = result.parameters.fields;
let text = result.fulfillmentText;
let intent = result.intent.displayName;
socket.emit("stuff from df", {
params,
text,
intent
});
});
});
});
app.get("/image/:phrase", async (req, res) => {
let phrase = req.params.phrase;
phrase = phrase.replace(/\s+/g, '+').toLowerCase();
console.log(phrase);
let url = `https://pixabay.com/api/?key=${process.env.PIXABAYKEY}&q=${phrase}&image_type=photo&pretty=true`;
fetch(url, {
mode: "cors"
})
.then((response) => response.json())
.then((result) => {
console.log(result);
let imgUrl = result.hits[randomNumber(0,3)].webformatURL;
res.send(imgUrl);
});
});
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}