• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java/Kotlin library for Actions on Google

Actions on Google Java/Kotlin Client Library

The Java/Kotlin library makes it easy to create Actions for the Google Assistant and supports Dialogflow and Actions SDK fulfillment. It does so by handling most of the request processing logic letting you focus on your Action’s business logic.

⚠️ Warning: Conversational Actions will be deprecated on June 13, 2023. For more information, see Conversational Actions Sunset.

⚠️ This library supports Dialogflow and the legacy Actions SDK. We now recommend using Actions Builder or the Actions SDK to develop, test, and deploy Conversational Actions.

Requirements

  • IDE: You can create a project using any IDE of your choice. However we recommend IntelliJ IDEA by JetBrains.
  • Java: The Actions on Google Java fulfillment library requires Java 8 or higher. You can download the JDK from here.
  • Kotlin: This library is written in Kotlin. Kotlin is bundled with IntelliJ IDEA version 15+.
  • Google Cloud SDK (if deploying to Google Cloud App Engine): You can initialize and deploy your project to App Engine using the Google Cloud SDK (provides the gcloud CLI). You can download the Google Cloud SDK here.

Setup Instructions

The Actions on Google Java/Kotlin library is hosted on Maven central. To use the library in your project, add the following to the dependencies section of your project’s build.gradle.

repositories {
   mavenCentral()
}

dependencies {
   compile group: 'com.google.actions', name: 'actions-on-google', version: '1.8.0'
}

If using maven, add the following to your pom.xml file.

<dependency>
	<groupId>com.google.actions</groupId>
	<artifactId>actions-on-google</artifactId>
	<version>1.8.0</version>
</dependency>

Boilerplate

We recommend that you start your project from the library boilerplate. The boilerplate is a self-contained project that sets up all the build dependencies using Gradle and allows you to import it easily into IntelliJ or other IDE of choice. It also includes artifacts that make it easy to deploy your project on Google Cloud Platform or AWS.

git clone https://github.com/actions-on-google/dialogflow-webhook-boilerplate-java.git

Instructions for IntelliJ

  • Start IntelliJ IDEA
  • Click Import Project
  • Select the build.gradle file located in the root directory of the project to import the gradle project.
  • On the Import Project from Gradle dialog box, select Use gradle ‘wrapper’ task configuration, and click OK.

The project should now be setup.

Use the Java/Kotlin library

The Java/Kotlin library makes it easy to implement a fulfillment webhook for your Action. You implement your Action’s logic in intent handlers as explained below.

Intent handler

An intent is a goal or action that the user wants to do, such as listening to a song or ordering coffee. Actions on Google represents the intent as a unique identifier. Your Action webhook provides handlers for intents it wants to handle dynamically. In the Java library, this is implemented as a Java class that extends either DialogflowApp or ActionsSdkApp.

Intent handlers are public methods in this class that are marked with a special annotation - @ForIntent as shown below. Intent handlers accept an ActionRequest as a parameter and return an ActionResponse.

public class MyActionsApp extends DialogflowApp {

 @ForIntent("Default Welcome Intent")
 public ActionResponse welcome(ActionRequest request) {
   // Intent handler implementation.
 }
}

Assemble a response

Intent handlers return a relevant fulfillment response that is sent back to the Google Assistant, which ultimately conveys it to the user as voice and/or visual response. In the simplest form, a response is text spoken back to the user. Actions on Google also supports many other response formats, which include immersive cards with images, carousels, lists, media, and SSML.

Your Action may also respond with one of the helper intents supported by the Assistant. Examples include requesting confirmation from the user or getting permission from the user to get their location.

The ResponseBuilder class provides a variety of helper methods to assemble a response. In the simplest case, your Action responds back with text:

@ForIntent("Default Welcome Intent")
 public ActionResponse welcome(ActionRequest request) {
   ResponseBuilder responseBuilder = getResponseBuilder(request);
   responseBuilder.add("Welcome to my app");
   return responseBuilder.build();
 }

Here is a response that uses BasicCard to render a visual response:

responseBuilder
           .add("This is the first simple response for a basic card.")
           .add(new BasicCard()
                   .setTitle("Title: This is a title")
                   .setSubtitle("This is a subtitle")
                   .setFormattedText(text)
                   .setImage(new Image()
                           .setUrl(IMG_URL)
                           .setAccessibilityText("Image alt text"))
                   .setButtons(buttons))
           .addSuggestions(SUGGESTIONS);

The library generates a JSON response from the ActionResponse returned from the intent handler (see below). The JSON is eventually handled by the Google Assistant to render an audio/visual response to the end user.

The following response uses a helper intent to request the Assistant to get the relevant information from the user:

@ForIntent("askForPermissions")
 Public ActionResponse askForPermission(ActionRequest request) {
   ResponseBuilder responseBuilder = getResponseBuilder(request);
   responseBuilder
           .add("Placeholder for permissions text")
           .add(new Permission()
                   .setPermissions(new String[]{
                           ConstantsKt.PERMISSION_NAME,
                           ConstantsKt.PERMISSION_DEVICE_PRECISE_LOCATION
                   })
                   .setContext("To provide a better experience"));

   return responseBuilder.build());
 }

You can explore more helper methods in ResponseBuilder.

As you can see from the above examples, the Java library provides an intuitive API to assemble responses from your Action. It provides an idiomatic abstraction over the JSON protocol to make it very easy to assemble all supported responses from your Action.

Core API classes

The Java/Kotlin library includes the following core API classes:

  • App: Top level interface to handle the JSON request and return a JSON response. It defines a single method — handleRequest() — to do this.
  • DefaultApp: Subclass of App, implements the request processing logic.
  • DialogflowApp and ActionsSdkApp: These sub-classes of DefaultApp provide specific implementations to handle requests from Dialogflow or directly from Google Assistant (Actions SDK).
  • ActionRequest: ActionRequest parses and encapsulates the JSON request body.
  • ActionResponse: Encapsulates the webhook response.
  • ResponseBuilder: Provides many helper methods to assemble a response.

You can read more about the classes in the library reference.

Test/Debug

This section describes how to test and debug your Java/Kotlin webhook during active development and implement end-end/integration tests.

Unit tests

The boilerplate provides a starting point to unit test your intent handler. Depending on your use case, you can choose one of the following approaches:

  • Create an ActionRequest by reading JSON from a file.
  • Use MockRequestBuilder to build an ActionRequest instance to test your intent handler.

Local debugging

During active development, you may start a local server to run your Action as a RESTful web service. You can send valid POST requests to this server (using tools such as Postman) to debug your intent handlers. Both Dialogflow and the Actions Simulator provide tools to visualize and copy JSON requests between Google Assistant and your webhook.

Integration Tests

You can use the Actions on Google testing library to test your Action end to end. Note that the testing library requires tests to be written in Node.js. The webhook fulfillment can be in Node.js/Java/Kotlin or any other language.

References & Issues

Make Contributions

Please read and follow the steps in the CONTRIBUTING.md.

License

See LICENSE.

Terms

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

More Repositories

1

actions-on-google-nodejs

Node.js client library for Actions on Google
TypeScript
900
star
2

smart-home-nodejs

A sample of the Smart Home device control APIs in Actions on Google
TypeScript
851
star
3

codelabs-nodejs

Actions on Google Codelabs
JavaScript
264
star
4

smart-home-local

Local Home SDK sample
TypeScript
255
star
5

dialogflow-webhook-boilerplate-nodejs

Webhook Boilerplate (using Dialogflow) in Node.js
JavaScript
187
star
6

dialogflow-facts-about-google-nodejs

Custom Entities, Contexts, and Deep Links sample (using Dialogflow) in Node.js
JavaScript
185
star
7

appactions-fitness-kotlin

A sample that shows how to make Android Apps available from the Assistant using App Actions.
Kotlin
173
star
8

assistant-conversation-nodejs

A developer friendly way to fulfill Actions SDK handlers for the Google Assistant
TypeScript
104
star
9

dialogflow-number-genie-nodejs

Localization sample (using Dialogflow) in Node.js
JavaScript
87
star
10

smart-home-java

Java
86
star
11

dialogflow-interactive-canvas-nodejs

A sample that demonstrates how to create an Interactive Canvas experience using Actions on Google for the Google Assistant.
JavaScript
76
star
12

actions-on-google-testing-nodejs

TypeScript
74
star
13

smart-home-dashboard

TypeScript
73
star
14

dialogflow-iosched-nodejs

The Action for the Google Assistant for Google I/O '18
JavaScript
71
star
15

dialogflow-transactions-nodejs

Physical transactions sample (using Dialogflow) in Node.js
JavaScript
67
star
16

local-home-sdk

Local Home SDK
CSS
67
star
17

dialogflow-conversation-components-nodejs

Rich Responses sample (using Dialogflow) in Node.js
JavaScript
66
star
18

dialogflow-updates-nodejs

Daily Updates & Push Notifications Sample (using Dialogflow) in Node.js
JavaScript
59
star
19

dialogflow-silly-name-maker

Parameters sample (using Dialogflow) in Node.js (no webhook)
59
star
20

actionssdk-say-number-nodejs

Say a number Actions SDK sample for Actions on Google
JavaScript
57
star
21

gactions

Go
49
star
22

dialogflow-name-psychic-nodejs

Permissions and Google Maps Static API sample (using Dialogflow) in Node.js
JavaScript
47
star
23

dialogflow-silly-name-maker-webhook-nodejs

Parameters sample (using Dialogflow) in Node.js
JavaScript
46
star
24

appactions-common-biis-kotlin

This sample Android app manages items on to-do lists using Google Assistant implemented via App Actions.
Kotlin
44
star
25

dialogflow-webhook-boilerplate-java

Webhook Boilerplate using the Java client library for Actions on Google
Java
35
star
26

actions-builder-canvas-nodejs

Interactive Canvas sample (using Actions Builder) in Node.js
JavaScript
26
star
27

dialogflow-google-sign-in-nodejs

Google Sign-In sample (using Dialogflow) in Node.js
HTML
25
star
28

smart-home-frontend

JavaScript
24
star
29

dialogflow-quotes-nodejs

External API sample (using Dialogflow) in Node.js
JavaScript
24
star
30

dialogflow-ssml-nodejs

SSML sample (using Dialogflow) in Node.js
JavaScript
23
star
31

assistant-conversation-testing-nodejs

Assistant conversation testing library
TypeScript
21
star
32

dialogflow-snowman-nodejs

Snowman Interactive Canvas Sample
JavaScript
19
star
33

smart-home-schema

19
star
34

dialogflow-gdg-nodejs

Localization, Meetup API & Cloud Translation API sample (using Dialogflow) in Node.js
JavaScript
19
star
35

assistant-conversation-schema

JSON Schema types for fulfilling Actions SDK handlers for the Google Assistant
19
star
36

actions-builder-conversation-components-nodejs

Conversation Components sample (using Actions Builder) in Node.js
TypeScript
15
star
37

dialogflow-quickstart-nodejs

Documentation samples for Actions on Google (Dialogflow/Node.js)
JavaScript
14
star
38

actions-builder-hello-world-nodejs

Hello World sample (using Actions Builder) in Node.js
JavaScript
13
star
39

create-local-home-app

Create a new Local Home SDK app.
TypeScript
13
star
40

dialogflow-helper-intents-nodejs

Helper Intents sample (using Dialogflow) in Node.js
JavaScript
13
star
41

actionssdk-conversation-components-nodejs

Rich Responses sample (using Actions SDK) in Node.js
JavaScript
12
star
42

assistant-actions-nodejs

Node.js Actions API Client Library
JavaScript
12
star
43

actionssdk-updates-nodejs

Daily Updates & Push Notifications Sample (using Actions SDK) in Node.js
JavaScript
12
star
44

actions-builder-codelab-level-1

Actions Builder Codelab (Level 1)
JavaScript
11
star
45

dialogflow-facts-about-google-java

Custom Entities, Contexts, and Deep Links sample (using Dialogflow) in Java
Java
11
star
46

interactive-canvas-devtools

A Chrome DevTools extension for Interactive Canvas development
CSS
10
star
47

dialogflow-digital-goods-nodejs

Digital Purchase API sample (using Dialogflow) in Node.js
JavaScript
9
star
48

dialogflow-google-sign-in-java

Google Sign-In sample (using Dialogflow) in Java
Java
8
star
49

dialogflow-conversation-components-java

Rich Responses sample (using Dialogflow) in Java
Java
8
star
50

dialogflow-name-psychic-java

Permissions, SSML, Surface Transfers, and Google Maps Static API sample (using Dialogflow) in Java
Java
8
star
51

actionssdk-shiritori-ja-nodejs

しりとり AoG サンプルゲーム
JavaScript
8
star
52

actions-builder-account-linking-nodejs

Account Linking sample (using Actions Builder) in Node.js
JavaScript
8
star
53

actions-builder-patterns-nodejs

Common design patterns for Actions Builder
JavaScript
8
star
54

user-engagement-codelab-nodejs

User Engagement Codelab - https://codelabs.developers.google.com/codelabs/actions-user-engagement
JavaScript
7
star
55

actions-builder-custom-nlu-nodejs

Custom NLU sample (using Actions Builder) in Node.js
JavaScript
7
star
56

actions-builder-codelab-level-2

Actions Builder Codelab (Level 2)
JavaScript
7
star
57

actions-builder-canvas-codelab-nodejs

JavaScript
6
star
58

dialogflow-transactions-java

Physical Goods Transactions API sample (using Dialogflow) in Java
Java
6
star
59

actionssdk-updates-java

Daily Updates & Push Notifications Sample (using Actions SDK) in Java
Java
6
star
60

actions-shortcut-convert

Kotlin
6
star
61

dialogflow-silly-name-maker-webhook-java

Parameters sample (using Dialogflow) in Java
Java
5
star
62

actions-builder-transactions-nodejs

Transactions sample (using Actions Builder) in Node.js
JavaScript
5
star
63

dialogflow-quickstart-java

Documentation samples for Actions on Google (Dialogflow/Java)
Java
5
star
64

actionssdk-say-number-java

SSML sample (using Actions SDK) in Java
Java
4
star
65

app-actions-dynamic-shortcuts

A sample app that demonstrates how to push dynamic shortcuts that can be displayed in Google Assistant.
Kotlin
4
star
66

actions-builder-updates-nodejs

Daily Updates and Notifications sample (using Actions Builder) in Node.js
JavaScript
4
star
67

smart-home-error-reporting

HTML
3
star
68

dialogflow-number-genie-java

Localization sample (using Dialogflow) in Java
Java
3
star
69

actionssdk-quickstart-java

Documentation samples for Actions on Google (Actions SDK/Java)
Java
3
star
70

actionssdk-vscode-extension

2
star
71

actions-on-google-linter-nodejs

JavaScript
2
star
72

dialogflow-session-entities-plugin-nodejs

TypeScript
2
star
73

dialogflow-helper-intents-java

Helper Intents sample (using Dialogflow) in Java
Java
2
star
74

dialogflow-interactive-canvas-codelab-nodejs

Interactive Canvas with Dialogflow Codelab
JavaScript
2
star
75

actionssdk-conversation-components-java

Rich Responses sample (using the Actions SDK) in Java
Java
2
star
76

dialogflow-updates-java

Daily Updates & Push Notifications Sample (using Dialogflow) in Java
Java
2
star
77

dialogflow-digital-goods-plugin-nodejs

TypeScript
1
star
78

actions-builder-canvas-angular

TypeScript
1
star
79

dialogflow-ssml-java

SSML sample (using Dialogflow) in Java
Java
1
star
80

actionssdk-quickstart-nodejs

Documentation samples for Actions on Google (Actions SDK/Node.js)
JavaScript
1
star
81

actions-builder-location-nodejs

JavaScript
1
star
82

dialogflow-quotes-java

External API sample (using Dialogflow) in Java
Java
1
star
83

.allstar

1
star
84

actions-builder-facts-about-google-nodejs

Facts About Google sample (using Actions Builder) in Node.js
JavaScript
1
star