-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (52 loc) · 2.31 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
// [START app]
'use strict';
process.env.DEBUG = 'actions-on-google:*';
let ActionsSdkAssistant = require('actions-on-google').ActionsSdkAssistant;
let express = require('express');
let bodyParser = require('body-parser');
let GreatAdventure = require('./adventure');
var gadv = new GreatAdventure();
let app = express();
app.set('port', (process.env.PORT || 8080));
app.use(bodyParser.json({type: 'application/json'}));
app.post('/', function (request, response) {
const assistant = new ActionsSdkAssistant({request: request, response: response});
function mainIntent(assistant) {
let res = gadv.begin();
assistant.data = res.data;
let inputPrompt = assistant.buildInputPrompt(false, res.prompt,
['I didn\'t hear you. Are you still there?', 'Wanna go somewhere?', 'Where do you want to go?']);
assistant.ask(inputPrompt);
}
function rawInput(assistant) {
if (assistant.getRawInput() === 'bye') {
assistant.tell('Thanks for playing Text Adventure! Goodbye!');
} else {
let res = gadv.setParams(assistant.data).setCommand(assistant.getRawInput()).parse();
assistant.data = res.data;
if(res.data.doorUnlocked == true) {
assistant.tell(res.prompt + " .... Thank you for playing Text Adventure! Please check back again soon for new features and games!");
return;
}
let inputPrompt = assistant.buildInputPrompt(false, res.prompt,
['I didn\'t hear you. Are you still there?', 'Wanna go somewhere?', 'Where do you want to go?']);
assistant.ask(inputPrompt);
}
}
let actionMap = new Map();
actionMap.set(assistant.StandardIntents.MAIN, mainIntent);
actionMap.set(assistant.StandardIntents.TEXT, rawInput);
assistant.handleRequest(actionMap);
});
app.post('/test', function (request, response) {
let command = request.body.command;
let params = request.body.data;
let res = gadv.setParams(params).setCommand(command).parse();
response.json({res});
});
// Start the server
let server = app.listen(app.get('port'), function () {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
// [END app]