Browse Source

Cleaned up and added README for the assistant sample.

Change-Id: Ie1dc937064ee4f65aaca1449a3aba76a9b1e7043
katowulf-pr-tpl
Nicolas Garnier 8 years ago
parent
commit
177ed8e006
  1. 5
      README.md
  2. 29
      assistant-say-number/README.md
  3. 4
      assistant-say-number/action.json
  4. 0
      assistant-say-number/firebase.json
  5. 32
      assistant-say-number/functions/index.js
  6. 0
      assistant-say-number/functions/package.json

5
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.

29
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 `<YOUR_PROJECT_ID>` 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"

4
actionssdk-say-number/action.json → assistant-say-number/action.json

@ -2,7 +2,7 @@
"versionLabel": "1.0.0",
"agentInfo": {
"languageCode": "en-US",
"projectId": "hello",
"projectId": "<YOUR_PROJECT_ID>",
"voiceName": "male_1"
},
"actions": [
@ -11,7 +11,7 @@
"intent": "assistant.intent.action.MAIN"
},
"httpExecution": {
"url": "YOUR_ENDPOINT_URL"
"url": "https://us-central1-<YOUR_PROJECT_ID>.cloudfunctions.net/sayNumber"
}
}
]

0
actionssdk-say-number/firebase.json → assistant-say-number/firebase.json

32
actionssdk-say-number/functions/index.js → 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, `<speak>
Hi! <break time="1"/>
I can read out an ordinal like <say-as interpret-as="ordinal">123</say-as>.
I can read out an ordinal number like <say-as interpret-as="ordinal">123</say-as>.
Say a number.
</speak>`, 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, `<speak>
@ -59,6 +61,4 @@ exports.helloWorld = functions.https.onRequest((request, response) => {
});
assistant.handleRequest(actionMap);
})
// [END app]
});

0
actionssdk-say-number/functions/package.json → assistant-say-number/functions/package.json

Loading…
Cancel
Save