-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline.js
114 lines (91 loc) · 3.6 KB
/
inline.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
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function moveHandler(agent){
let original_position = agent.parameters.original_position;
let destination_position = agent.parameters.destination_position;
agent.add("moving " + original_position + " to " + destination_position + " say yes to confirm");
return writeToDb ({
"origin": original_position,
"destination": destination_position
});
}
function resultHandler(agent) {
console.log("come into result handler");
return readFromDb (agent);
}
function writeToDb (move) {
console.log("come to writeToDb");
// Get parameter from Dialogflow with the string to add to the database
const docRef = db.collection('chessmove').doc('move');
return db.runTransaction(t => {
return t.get(docRef)
.then(doc => {
t.set(docRef, {
origin:move.origin, destination:move.destination
});
});
}).catch(err => {
console.log(`Error writing to Firestore: ${err}`);
});
}
function readFromDb (agent) {
console.log("enter readFromDb");
const docRef =db.collection("moveresult").doc("result");
return db.runTransaction(t => {
return t.get(docRef)
.then(doc => {
if(doc.data().success === true){
agent.add("Valid move");
const checkRef =db.collection("checkmate").doc("check");
return db.runTransaction(t => {
console.log("getting checkmate information");
return t.get(checkRef)
.then(doc => {
if(doc.data().checking === true){
agent.add("Be careful checkmate");
}
const winnerRef =db.collection("checkwinner").doc("winner");
return db.runTransaction(t => {
console.log("getting winner information");
return t.get(winnerRef)
.then(doc => {
if(doc.data().win !== ""){
console.log("enter add winner to agent");
let w = doc.data().win;
console.log(w);
agent.add("game over! " + w + " win the game");
}
});});
});});
}else{
console.log("enter invalid move");
agent.add("Invalid move. Try again");
}
});
}).catch(err => {
console.log(`Error reading from Firestore: ${err}`);
});
}
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('move', moveHandler);
intentMap.set('Result', resultHandler);
agent.handleRequest(intentMap);
});