diff --git a/README.md b/README.md index 8564d65..2301388 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ Only users who pass a valid Firebase ID token as a Bearer token in the Authoriza Checking the ID token is done with an ExpressJs middleware that also passes the decoded ID token in the Express request object. Uses an HTTP trigger. +### [Google Assistant says ordinal of given number](/assistant-say-number) + +This sample shows how to create an action for the Google Home/Assistant using the Actions SDK hosted on Cloud Functions. The sample action asks users to say a number and reads out the ordinal of that number. +Uses an HTTP trigger. + ### [Send FCM notifications](fcm-notifications) This sample demonstrates how to send a Firebase Cloud Messaging (FCM) notification from a Realtime Database triggered Function when users get new followers. The sample also features a Web UI to experience the FCM notification. diff --git a/assistant-say-number/README.md b/assistant-say-number/README.md new file mode 100644 index 0000000..4480331 --- /dev/null +++ b/assistant-say-number/README.md @@ -0,0 +1,29 @@ +# Google Assistant action that reads the ordinal of a number. + +This sample shows how to create an action for the Google Home/Assistant using the Actions SDK hosted on Cloud Functions. The sample action asks users to say a number and reads out the ordinal of that number. +e.g. If the user says "Twelve" the action will say "The ordinal of twelve is twelfth". + +Further reading: + - Actions SDK: https://developers.google.com/actions/develop/sdk/getting-started#getting-started. + - Firebase SDK: firebase.google.com/docs/functions + +## Functions Code + +See file [functions/index.js](functions/index.js) for the code. + +Sending the notification is performed using the [request-promise-native](https://www.npmjs.com/package/request-promise-native) package to send a REST request to the FCM API. The Web client writes the individual device tokens to the realtime database which the Function uses to send the notification. + +The dependencies are listed in [functions/package.json](functions/package.json). + + +## Deploy and test + +To test this sample action: + + - Create a Firebase Project using the [Firebase Developer Console](https://console.firebase.google.com) + - Configure this sample to use your project using `firebase --use add` and select your project. + - Deploy your project using `firebase deploy` + - In the `action.json` file, update the two `` placeholders with your Firebase project ID. The URL should match the `Function URL (sayNumber):` that was printed out by `firebase deploy`. + - [Download](https://developers.google.com/actions/tools/gactions-cli) the `gaction` CLI + - Make your action available for testing using the `gactions preview action.json` + - Test your Action on the [Google Home Web Simulator](https://g.co/actionswebsim) by saying "Talk to My Action" diff --git a/actionssdk-say-number/action.json b/assistant-say-number/action.json similarity index 67% rename from actionssdk-say-number/action.json rename to assistant-say-number/action.json index 13b9b88..f7d044f 100644 --- a/actionssdk-say-number/action.json +++ b/assistant-say-number/action.json @@ -2,7 +2,7 @@ "versionLabel": "1.0.0", "agentInfo": { "languageCode": "en-US", - "projectId": "hello", + "projectId": "", "voiceName": "male_1" }, "actions": [ @@ -11,7 +11,7 @@ "intent": "assistant.intent.action.MAIN" }, "httpExecution": { - "url": "YOUR_ENDPOINT_URL" + "url": "https://us-central1-.cloudfunctions.net/sayNumber" } } ] diff --git a/actionssdk-say-number/firebase.json b/assistant-say-number/firebase.json similarity index 100% rename from actionssdk-say-number/firebase.json rename to assistant-say-number/firebase.json diff --git a/actionssdk-say-number/functions/index.js b/assistant-say-number/functions/index.js similarity index 62% rename from actionssdk-say-number/functions/index.js rename to assistant-say-number/functions/index.js index 514d87e..3a1a115 100644 --- a/actionssdk-say-number/functions/index.js +++ b/assistant-say-number/functions/index.js @@ -11,29 +11,32 @@ // 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. - -// [START app] 'use strict'; const functions = require('firebase-functions'); +const ActionsSdkAssistant = require('actions-on-google').ActionsSdkAssistant; -exports.helloWorld = functions.https.onRequest((request, response) => { - - const ActionsSdkAssistant = require('actions-on-google').ActionsSdkAssistant; - const assistant = new ActionsSdkAssistant({request, response}); +/** + * Endpoint which handles requests for a Google Assistant action which asks users to say a number + * and read out the ordinal of that number. + * e.g. If the user says "Twelve" the action will say "The ordinal of twelve is twelfth". + */ +exports.sayNumber = functions.https.onRequest((req, res) => { + const assistant = new ActionsSdkAssistant({request: req, response: res}); + // List of re-prompts that are used when we did not understand a number from the user. const reprompts = [ - "I didn't hear a number", - "If you're still there, what's the number?", + 'I didn\'t hear a number', + 'If you\'re still there, what\'s the number?', 'What is the number?' ]; - let actionMap = new Map(); + const actionMap = new Map(); actionMap.set(assistant.StandardIntents.MAIN, assistant => { const inputPrompt = assistant.buildInputPrompt(true, ` Hi! - I can read out an ordinal like 123. + I can read out an ordinal number like 123. Say a number. `, reprompts ); @@ -44,9 +47,8 @@ exports.helloWorld = functions.https.onRequest((request, response) => { const rawInput = assistant.getRawInput(); if (rawInput === 'bye') { assistant.tell('Goodbye!'); - } else if (isNaN(parseInt(rawInput))) { - const inputPrompt = assistant.buildInputPrompt(false, `I didn't quite get that, what was the - number?`, reprompts); + } else if (isNaN(parseInt(rawInput, 10))) { + const inputPrompt = assistant.buildInputPrompt(false, 'I didn\'t quite get that, what was the number?', reprompts); assistant.ask(inputPrompt); } else { const inputPrompt = assistant.buildInputPrompt(true, ` @@ -59,6 +61,4 @@ exports.helloWorld = functions.https.onRequest((request, response) => { }); assistant.handleRequest(actionMap); - -}) -// [END app] +}); diff --git a/actionssdk-say-number/functions/package.json b/assistant-say-number/functions/package.json similarity index 100% rename from actionssdk-say-number/functions/package.json rename to assistant-say-number/functions/package.json