UNPKG

9.57 kBMarkdownView Raw
1# Actions on Google Client Library
2
3This client library makes it easy to create Actions for the Google Assistant and
4supports Dialogflow, Actions SDK (v2), and Smart Home fulfillment.
5
6:warning: We now recommend using [Actions Builder or the Actions SDK](https://developers.google.com/assistant/conversational/overview) to develop, test, and deploy Conversational Actions. For [Conversational Actions](https://developers.google.com/assistant/conversational/actions) built using [Actions Builder](https://developers.google.com/assistant/console/builder) see the [@assistant/conversation](https://github.com/actions-on-google/assistant-conversation-nodejs) library.
7
8* [Client Library GitHub repo](https://github.com/actions-on-google/actions-on-google-nodejs)
9* [Client Library reference docs](https://actions-on-google.github.io/actions-on-google-nodejs/)
10* [Actions on Google docs](https://developers.google.com/assistant)
11* [Actions on Google samples](https://developers.google.com/assistant/actions/samples)
12
13[![NPM Version](https://img.shields.io/npm/v/actions-on-google.svg)](https://www.npmjs.org/package/actions-on-google)
14[![Build Status](https://travis-ci.org/actions-on-google/actions-on-google-nodejs.svg?branch=master)](https://travis-ci.org/actions-on-google/actions-on-google-nodejs)
15
16## Setup Instructions
17
18Install the library with either `npm install actions-on-google` or `yarn add actions-on-google` if you use yarn.
19
20### Developer Preview
21
22To support features under Developer Preview, the library has a special [`preview`](https://github.com/actions-on-google/actions-on-google-nodejs/tree/preview) branch which can be installed using the `@preview` tag.
23
24This is installed with either `npm install actions-on-google@preview` or `yarn add actions-on-google@preview`.
25
26The `preview` tag will be kept up to date with every new version of the library.
27
28You can use the Developer Preview version to experience exciting new features that we’re still testing to make sure we have the best developer experience, and help us providing feedback on the API design and feature set.
29
30The APIs offered in Developer Preview have not matured to General Availability yet, which means:
31 - **You can’t publish Actions** that use features in Developer Preview.
32 - The APIs are **potentially subject to backwards incompatible changes**.
33
34### Conversational Services
35
36#### Dialogflow
37```javascript
38// Import the appropriate service and chosen wrappers
39const {
40 dialogflow,
41 Image,
42} = require('actions-on-google')
43
44// Create an app instance
45const app = dialogflow()
46
47// Register handlers for Dialogflow intents
48
49app.intent('Default Welcome Intent', conv => {
50 conv.ask('Hi, how is it going?')
51 conv.ask(`Here's a picture of a cat`)
52 conv.ask(new Image({
53 url: 'https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/imgs/160204193356-01-cat-500.jpg',
54 alt: 'A cat',
55 }))
56})
57
58// Intent in Dialogflow called `Goodbye`
59app.intent('Goodbye', conv => {
60 conv.close('See you later!')
61})
62
63app.intent('Default Fallback Intent', conv => {
64 conv.ask(`I didn't understand. Can you tell me something else?`)
65})
66```
67
68#### Actions SDK
69```javascript
70// Import the appropriate service and chosen wrappers
71const {
72 actionssdk,
73 Image,
74} = require('actions-on-google')
75
76// Create an app instance
77const app = actionssdk()
78
79// Register handlers for Actions SDK intents
80
81app.intent('actions.intent.MAIN', conv => {
82 conv.ask('Hi, how is it going?')
83 conv.ask(`Here's a picture of a cat`)
84 conv.ask(new Image({
85 url: 'https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/imgs/160204193356-01-cat-500.jpg',
86 alt: 'A cat',
87 }))
88})
89
90app.intent('actions.intent.TEXT', (conv, input) => {
91 if (input === 'bye' || input === 'goodbye') {
92 return conv.close('See you later!')
93 }
94 conv.ask(`I didn't understand. Can you tell me something else?`)
95})
96```
97
98#### Notes about the code snippet
99* [`conv.ask`](https://actions-on-google.github.io/actions-on-google-nodejs/classes/conversation.conversation-1.html#ask)/[`conv.close`](https://actions-on-google.github.io/actions-on-google-nodejs/classes/conversation.conversation-1.html#close)
100can be called with any of the [`Response`](https://actions-on-google.github.io/actions-on-google-nodejs/modules/conversation.html#response) types.
101* All [`Helper`](https://actions-on-google.github.io/actions-on-google-nodejs/modules/conversation_helper.html) classes are of the `Response` type.
102
103##### Dialogflow
104* `app` is an instance of type [`DialogflowApp`](https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/dialogflow.dialogflowapp.html#catch).
105* `app` accepts options of type [`DialogflowOptions`](https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/dialogflow.dialogflowoptions.html#clientid).
106* `conv` is an instance of type [`DialogflowConversation`](https://actions-on-google.github.io/actions-on-google-nodejs/classes/dialogflow.dialogflowconversation.html).
107
108##### Actions SDK
109* `app` is an instance of type [`ActionsSdkApp`](https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/actionssdk.actionssdkapp.html#catch).
110* `app` accepts options of type [`ActionsSdkOptions`](https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/actionssdk.actionssdkoptions.html#clientid).
111* `conv` is an instance of type [`ActionsSdkConversation`](https://actions-on-google.github.io/actions-on-google-nodejs/classes/actionssdk.actionssdkconversation.html).
112
113### Smart Home
114```javascript
115// Import the appropriate service
116const { smarthome } = require('actions-on-google')
117
118// Create an app instance
119const app = smarthome()
120
121// Register handlers for Smart Home intents
122
123app.onExecute((body, headers) => {
124 return {
125 requestId: 'ff36...',
126 payload: {
127 // ...
128 },
129 }
130})
131
132app.onQuery((body, headers) => {
133 return {
134 requestId: 'ff36...',
135 payload: {
136 // ...
137 },
138 }
139})
140
141app.onSync((body, headers) => {
142 return {
143 requestId: 'ff36...',
144 payload: {
145 // ...
146 },
147 }
148})
149```
150
151#### Notes about the code snippet
152* `app` is an instance of type [`SmartHomeApp`](https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/smarthome.smarthomeapp.html#onexecute).
153* `app` accepts options of type [`SmartHomeOptions`](https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/smarthome.smarthomeoptions.html#debug).
154
155### Frameworks
156
157Export or run for your appropriate framework:
158
159#### Firebase Functions
160``` javascript
161const functions = require('firebase-functions')
162
163// ... app code here
164
165exports.fulfillment = functions.https.onRequest(app)
166```
167
168#### Dialogflow Inline Editor
169```javascript
170const functions = require('firebase-functions')
171
172// ... app code here
173
174// name has to be `dialogflowFirebaseFulfillment`
175exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)
176```
177
178#### Self Hosted Express Server
179```javascript
180const express = require('express')
181const bodyParser = require('body-parser')
182
183// ... app code here
184
185const expressApp = express().use(bodyParser.json())
186
187expressApp.post('/fulfillment', app)
188
189expressApp.listen(3000)
190```
191
192#### AWS Lambda API Gateway
193```javascript
194// ... app code here
195
196exports.fulfillment = app
197```
198
199### Next Steps
200
201Take a look at the docs and samples linked at the top to get to know the platform and supported functionalities.
202
203## Library Development Instructions
204This library uses `yarn` to run commands. Install yarn using instructions from https://yarnpkg.com/en/docs/install or with npm: `npm i -g yarn`.
205
206Install the library dependencies with `yarn`. If you want to run any of the sample apps, follow the instructions in the sample README.
207
208## Functionality
209
210Public interfaces, classes, functions, objects, and properties are labeled with the JSDoc `@public` tag and exported at the top level. Everything else that is not labeled `@public` and exported at the top level is considered internal and may be changed.
211
212This library supports the following Services:
213* [Dialogflow](https://dialogflow.com/docs/fulfillment) v1 and v2
214* [Actions SDK](https://developers.google.com/assistant/actions/actions-sdk/fulfillment) **v2 only**
215* [Smart Home](https://developers.google.com/assistant/smarthome/develop/create#provide-fulfillment)
216
217### Actions SDK
218This library supports only Actions SDK fulfillment version 2.
219
220To ensure that your fulfillment uses version 2, set the [`"fulfillmentApiVersion": 2`](https://github.com/actions-on-google/actionssdk-eliza-nodejs/blob/a44a1b0ef0026ce2b0e525ce38bebbf8540ce344/eliza.json#L41) property in your action package.
221
222## References & Issues
223+ Questions? Go to [StackOverflow](https://stackoverflow.com/questions/tagged/actions-on-google), [Assistant Developer Community on Reddit](https://www.reddit.com/r/GoogleAssistantDev/) or [Support](https://developers.google.com/assistant/support).
224+ For bugs, please report an issue on Github.
225+ Actions on Google [Documentation](https://developers.google.com/assistant)
226+ Actions on Google [Codelabs](https://codelabs.developers.google.com/?cat=Assistant).
227+ [Webhook Boilerplate Template](https://github.com/actions-on-google/dialogflow-webhook-boilerplate-nodejs) for Actions on Google.
228
229## Make Contributions
230Please read and follow the steps in the [CONTRIBUTING.md](CONTRIBUTING.md).
231
232## License
233See [LICENSE](LICENSE).
234
235## Terms
236Your use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the [Google APIs Terms of Service](https://developers.google.com/terms/).