|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START app] |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const functions = require('firebase-functions'); |
| 19 | + |
| 20 | +exports.helloWorld = functions.https.onRequest((request, response) => { |
| 21 | + |
| 22 | + const ActionsSdkAssistant = require('actions-on-google').ActionsSdkAssistant; |
| 23 | + const assistant = new ActionsSdkAssistant({request, response}); |
| 24 | + |
| 25 | + const reprompts = [ |
| 26 | + "I didn't hear a number", |
| 27 | + "If you're still there, what's the number?", |
| 28 | + 'What is the number?' |
| 29 | + ]; |
| 30 | + |
| 31 | + let actionMap = new Map(); |
| 32 | + |
| 33 | + actionMap.set(assistant.StandardIntents.MAIN, assistant => { |
| 34 | + const inputPrompt = assistant.buildInputPrompt(true, `<speak> |
| 35 | + Hi! <break time="1"/> |
| 36 | + I can read out an ordinal like <say-as interpret-as="ordinal">123</say-as>. |
| 37 | + Say a number. |
| 38 | + </speak>`, reprompts |
| 39 | + ); |
| 40 | + assistant.ask(inputPrompt); |
| 41 | + }); |
| 42 | + |
| 43 | + actionMap.set(assistant.StandardIntents.TEXT, assistant => { |
| 44 | + const rawInput = assistant.getRawInput(); |
| 45 | + if (rawInput === 'bye') { |
| 46 | + assistant.tell('Goodbye!'); |
| 47 | + } else if (isNaN(parseInt(rawInput))) { |
| 48 | + const inputPrompt = assistant.buildInputPrompt(false, `I didn't quite get that, what was the |
| 49 | + number?`, reprompts); |
| 50 | + assistant.ask(inputPrompt); |
| 51 | + } else { |
| 52 | + const inputPrompt = assistant.buildInputPrompt(true, `<speak> |
| 53 | + The ordinal of <say-as interpret-as="cardinal">${rawInput}</say-as> is |
| 54 | + <say-as interpret-as="ordinal">${rawInput}</say-as> |
| 55 | + </speak>`, reprompts |
| 56 | + ); |
| 57 | + assistant.ask(inputPrompt); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + assistant.handleRequest(actionMap); |
| 62 | + |
| 63 | +}) |
| 64 | +// [END app] |
0 commit comments