This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
180 lines (166 loc) · 5.18 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2019, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const {
actionssdk,
Permission,
Suggestions,
DateTime,
Place,
Confirmation,
} = require('actions-on-google');
const functions = require('firebase-functions');
const app = actionssdk({debug: true});
app.intent('actions.intent.MAIN', (conv) => {
conv.ask(`Hi! I can show you different types of helpers.`);
conv.ask(`Which would you like to see?`);
conv.ask(new Suggestions([
'Permission',
'Confirmation',
'Date/Time',
'Place',
]));
});
app.intent('actions.intent.TEXT', (conv, input) => {
const query = input.toLowerCase();
if (query === 'permission') {
return permission(conv);
} else if (query === 'confirmation') {
return confirmation(conv);
} else if (query === 'date time' ||
query === 'date/time' ||
query === 'datetime') {
return datetime(conv);
} else if (query === 'place') {
return place(conv);
} else {
conv.ask(`You didn't name a supported helper!`);
conv.ask(`Which one would you like to see?`);
return;
}
});
const permission = (conv) => {
// [START asdk_js_permission_reason]
const permissions = ['NAME'];
let context = 'To address you by name';
// Location permissions only work for verified users
// https://developers.google.com/actions/assistant/guest-users
if (conv.user.verification === 'VERIFIED') {
// Could use DEVICE_COARSE_LOCATION instead for city, zip code
permissions.push('DEVICE_PRECISE_LOCATION');
context += ' and know your location';
}
const options = {
context,
permissions,
};
conv.ask(new Permission(options));
// [END asdk_js_permission_reason]
};
// [START asdk_js_permission_accepted]
app.intent('actions.intent.PERMISSION', (conv, confirmationGranted) => {
// Also, can access latitude and longitude
// const { latitude, longitude } = location.coordinates;
const {location} = conv.device;
const {name} = conv.user;
if (confirmationGranted && name && location) {
conv.ask(`Okay ${name.display}, I see you're at ` +
`${location.formattedAddress}`);
} else {
conv.ask(`Looks like I can't get your information.`);
}
conv.ask(`Would you like to try another helper?`);
conv.ask(new Suggestions([
'Confirmation',
'DateTime',
'Place',
]));
});
// [END asdk_js_permission_accepted]
const datetime = (conv) => {
// [START asdk_js_datetime_reason]
const options = {
prompts: {
initial: 'When would you like to schedule the appointment?',
date: 'What day was that?',
time: 'What time works for you?',
},
};
conv.ask(new DateTime(options));
// [END asdk_js_datetime_reason]
};
// [START asdk_js_datetime_accepted]
app.intent('actions.intent.DATETIME', (conv) => {
const datetime = conv.arguments.get('DATETIME');
const {month, day} = datetime.date;
const {hours, minutes} = datetime.time;
conv.ask(`<speak>` +
`Great, we will see you on ` +
`<say-as interpret-as="date" format="dm">${day}-${month}</say-as>` +
`<say-as interpret-as="time" format="hms12" detail="2">${hours}` +
`:${minutes || '00'}</say-as>` +
`</speak>`);
conv.ask('Would you like to try another helper?');
conv.ask(new Suggestions([
'Confirmation',
'Permission',
'Place',
]));
});
// [END asdk_js_datetime_accepted]
const place = (conv) => {
// [START asdk_js_place_reason]
const options = {
context: 'To find a location',
prompt: 'Where would you like to go?',
};
conv.ask(new Place(options));
// [END asdk_js_place_reason]
};
// [START asdk_js_place_accepted]
app.intent('actions.intent.PLACE', (conv) => {
const place = conv.arguments.get('PLACE');
if (!place) {
conv.ask(`Sorry, I couldn't get a location from you.`);
} else {
// Place also contains formattedAddress and coordinates
const {name} = place;
conv.ask(`${name} sounds like a great place to go!`);
}
conv.ask('Would you like to try another helper?');
conv.ask(new Suggestions([
'Confirmation',
'DateTime',
'Permission',
]));
});
// [END asdk_js_place_accepted]
const confirmation = (conv) => {
// [START asdk_js_confirmation_reason]
conv.ask(new Confirmation('Can you confirm?'));
// [END asdk_js_confirmation_reason]
};
// [START asdk_js_confirmation_accepted]
app.intent('actions.intent.CONFIRMATION', (conv, confirmationGranted) => {
conv.ask(confirmationGranted
? 'Thank you for confirming'
: 'No problem, you have not confirmed');
conv.ask('Would you like to try another helper?');
conv.ask(new Suggestions([
'DateTime',
'Permission',
'Place',
]));
});
// [END asdk_js_confirmation_accepted]
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);