-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
96 lines (88 loc) · 2.43 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
const SPREADSHEET_KEY = 'FILL_ME_PLEASE!';
const CLIENT_EMAIL = 'FILL_ME_PLEASE!';
const PRIVATE_KEY = 'FILL_ME_PLEASE!';
var GoogleSpreadsheet = require("google-spreadsheet");
var async = require('async');
const {
dialogflow,
} = require('actions-on-google');
const functions = require('firebase-functions');
// Create an app instance
const app = dialogflow();
// Register handlers for Dialogflow intents
var doc = new GoogleSpreadsheet(SPREADSHEET_KEY);
var creds_json = {
client_email: CLIENT_EMAIL,
private_key: PRIVATE_KEY
};
var sheet;
var questions = [];
var question_texts = {};
function get_questions(step){
sheet.getRows({
offset: 1,
limit: 1,
}, function( err, rows ){
console.log('Error' + err);
console.log('Read '+rows.length+' rows');
qrow = rows[0];
idx = 1;
questions = [];
question_texts = {};
while(true){
var k = 'q'+idx;
if(k in qrow){
questions.push(k);
question_texts[k] = qrow[k];
}
else
break;
idx++;
}
console.log(question_texts);
step();
});
}
async.series([
function auth(step){
doc.useServiceAccountAuth(creds_json,step);
},
function get_sheet(step){
doc.getInfo(function(err, info) {
console.log("Error is ");
console.log(err);
console.log('Loaded doc: '+info.title+' by '+info.author.email);
sheet = info.worksheets[0];
console.log('sheet 1: '+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
step();
});
},
get_questions
]);
app.intent('dynamic', conv => {
if(!('resps' in conv.data)){
conv.data.resps={};
}
console.log(JSON.stringify(conv.data));
if ('lastQ' in conv.data){
conv.data.resps[conv.data.lastQ]=conv.parameters.answer;
}
for(var q in questions){
k = questions[q];
if(!(k in conv.data.resps))
{
conv.data.resps[k]="?";
conv.data.lastQ = k;
conv.ask(question_texts[k]);
return;
}
}
var t =(new Date()).getTime();
t-= 6*60*60*1000; // Sometimes I sleep after midnight, so I subtract 6 hours from current time in order to record the habits in the correct date
conv.data.resps.date=(new Date(t)).toDateString();
doc.addRow(1,conv.data.resps,function(){});
console.log("Refreshing questions");
get_questions(function(){});
conv.close("I asked all my questions");
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);