This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
/
index.js
110 lines (98 loc) · 3.36 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
// 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';
// [START du_imports_asdk_js]
const {
actionssdk,
RegisterUpdate,
Suggestions,
} = require('actions-on-google');
// [END du_imports_asdk_js]
const functions = require('firebase-functions');
const lowestTemperatures = {
Monday: '75°F',
Tuesday: '75°F',
Wednesday: '75°F',
Thursday: '75°F',
Friday: '75°F',
Saturday: '75°F',
Sunday: '75°F',
};
const app = actionssdk({debug: true});
const DAYS = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
app.intent('actions.intent.MAIN', (conv) => {
if (!conv.screen) {
return conv.close(`Hi! Welcome to Lowest Temperature Updates! To learn ` +
`about user engagement you will need to switch to a screened device.`);
}
if (conv.user.verification !== 'VERIFIED') {
return conv.close('Hi! Welcome to Lowest Temperature Updates! To learn ' +
`about user engagement you'll need to be a verified user.`);
}
conv.ask('Hi! Welcome to Lowest Temperature Updates! I can ' +
'tell you the lowest temperature each day.');
conv.ask(new Suggestions('Hear lowest temperature'));
});
app.intent('actions.intent.TEXT', (conv, input) => {
if (input === 'Send daily updates') {
return subscribeToDailyUpdates(conv);
}
if (input === 'Hear lowest temperature') {
return dailyLowestTemperature(conv);
}
});
app.intent('Daily Lowest Temperature', (conv) => {
return dailyLowestTemperature;
});
const dailyLowestTemperature = (conv) => {
const today = DAYS[new Date().getDay()];
const lowestTemperature = lowestTemperatures[today];
conv.ask(`The lowest temperature for today is ${lowestTemperature}`);
/**
* Best practices: Ask for daily updates in a limited capacity for optimal
* user experience. In a production-ready app, you should be more
* sophisticated about this, i.e. re-ask after a certain period of time
* or number of interactions.
*/
// [START suggest_daily_updates_asdk]
conv.ask('I can send you daily updates with the lowest ' +
'temperature of the day. Would you like that?');
conv.ask(new Suggestions('Send daily updates'));
// [END suggest_daily_updates_asdk]
};
const subscribeToDailyUpdates = (conv) => {
// [START subscribe_to_daily_updates_asdk]
conv.ask(new RegisterUpdate({
intent: 'Daily Lowest Temperature',
frequency: 'DAILY',
}));
// [END subscribe_to_daily_updates_asdk]
};
// [START confirm_daily_updates_subscription_asdk]
app.intent('actions.intent.REGISTER_UPDATE', (conv, params, registered) => {
if (registered && registered.status === 'OK') {
conv.close(`Ok, I'll start giving you daily updates.`);
} else {
conv.close(`Ok, I won't give you daily updates.`);
}
});
// [END confirm_daily_updates_subscription_asdk]
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);