DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Alexa Skill With Node.js
  • Alexa Skill With TypeScript
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Building a Voice-Powered Smart Kitchen App Using LLaMA 3.1, Firebase, and Node.js

Trending

  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  • Simpler Data Transfer Objects With Java Records
  • Implementing Explainable AI in CRM Using Stream Processing
  • How to Introduce a New API Quickly Using Micronaut
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Google Action With Node.js

Google Action With Node.js

In this tutorial, you will learn how to create a Google Action using Google actions CLI and NodeJS. Read on to find out how to do this yourself!

By 
Xavier Portilla Edo user avatar
Xavier Portilla Edo
DZone Core CORE ·
Dec. 28, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

Google Actions can be developed using Firebase Cloud functions or a REST API endpoint. Firebase Cloud Function function is Googles's implementation of serverless functions available in Firebase. Google recommends using Firebase Cloud Function functions for Google Action development.

In this post, we will implement a Google Action for Google Assistant by using Node.js, yarn, and Firebase Cloud Functions. This Google Action is basically a Hello World example.

This post contains materials from different resources that can be seen in the Resources section.

Prerequisites

Here, you have the technologies used in this project:

  1. Google Action Developer Account - How to get it
  2. Google Cloud Account - Sign up here for free
  3. Firebase Account - Sign up here for free
  4. Google actions CLI - Install and configure Google actions CLI
  5. Firebase CLI - Install and configure Firebase CLI
  6. Node.js v10.x
  7. Visual Studio Code
  8. yarn Package Manager
  9. Google Action SDK for Node.js (Version >3.0.0)
  10. Ngrok

The Google Actions Command Line Interface (Google actions CLI) is a tool for you to manage your Google Actions and related resources, such as Firebase Cloud functions. Google actions CLI allows you to manage Google Actions programmatically from the command line. We will use this powerful tool to create, build, deploy and manage our Hello World Google Action. Let's start!

Creating the Google Action With Google Actions CLI

For creating the Google Action, we will use de Google actions CLI previously configured. First of all, we have to execute this command:

    Google actions init hello-world


This command has to be executed on an empty directory.

Once you have your Google Action code initialized, you have to go to the Google Action Console and create a new project:

Create a new project.

Once you have created your Google Action project on the console, you have to get the project ID and put that value in the property projectId file sdk/settings/settings.yaml:

category: GAMES_AND_TRIVIA
defaultLocale: en
localizedSettings:
  displayName: Hello world sample
  pronunciation: Hello world sample
projectId: action-helloworld # Your Project ID here


NOTE: You can find the Project ID on the Google Actions Console URL while accessing your Google Action. The URL will have this format: https://console.actions.google.com/u/0/project/<YOUR-PROJECT-ID>/scenes/Start. In my case will be: https://console.actions.google.com/u/0/project/action-helloworld/scenes/Start

And that's it. With these steps, you have created your first Google Action and linked to your source code locally using the Google actions CLI. You only have to deploy your local changes. We are going to explain it in the next steps.

Project Files

These are the main files of the project:

└── sdk
    ├── actions
    │   └── actions.yaml
    ├── custom
    │   ├── global
    │   ├── intents
    │   ├── scenes
    │   └── types
    ├── manifest.yaml
    ├── settings
    │   └── settings.yaml
    └── webhooks
        ├── ActionsOnGoogleFulfillment
        │   ├── index.js
        │   └── package.json
        └── ActionsOnGoogleFulfillment.yaml


  • sdk: The main folder of the project. Here we will execute all the Google actions CLI commands.
    • actions:
      • actions.yaml: file that contains some metadata of the Google Action such as the Assistant Link.
    • custom: In this folder, we are going to specify our interaction model and the conversation flow.
      • global: In this folder, you will find all the intents that can be triggered at any time because they are global.
      • intents: Intents defined by us. Each intent will have a list of utterances that Google Assistant will use to train its AI.
      • scenes: The different Scenes of our Google Actions. We will use Scenes to manage the flow of our conversation.
      • types: the types/entities we are going to use on our intents/utterances.
    • settings:
      • settings.yaml: This file will contain all the metadata of our Google Action like description, logo, Privacy Policy, Project ID, etc.
    • webhooks: A folder that contains the source code of the Firebase Cloud function:
      • ActionsOnGoogleFulfillment: Folder with all the Firebase Cloud Function code.
        • index.js: the Firebase Cloud Function main entry point.
        • package.json: this file is core to the Node.js ecosystem and is a basic part of understanding and working with Node.js, npm, and even modern JavaScript
      • ActionsOnGoogleFulfillment.yaml: File that specifies the handlers and the folder of the source code.

Firebase Cloud Function in Javascript

The Google Action SDK for Node.js makes it easier for you to build highly engaging google actions by allowing you to spend more time implementing features and less time writing boilerplate code.

You can find documentation, samples, and helpful links in their official GitHub repository

The main Javascript file in our Firebase Cloud Function project is index.js located in sdk/webhooks/ActionsOnGoogleFulfillment folder. This file contains all handlers and exports the Google Action handler in exports.ActionsOnGoogleFulfillment.

The exports.ActionsOnGoogleFulfillment function is executed every time Firebase is initiated for this particular function.

JavaScript
 
    const {conversation} = require('@assistant/conversation');
    const functions = require('firebase-functions');

    const app = conversation({debug: true});

    app.handle('start_scene_initial_prompt', (conv) => {
        console.log('Start scene: initial prompt');
        conv.overwrite = false;
        conv.scene.next.name = 'actions.scene.END_CONVERSATION';
        conv.add('Hello world from fulfillment');
    });

    exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);


It is important to take a look into the start_scene_initial_prompt as an example of Google Action handler written in Node.js:

JavaScript
 
    app.handle('start_scene_initial_prompt', (conv) => {
        console.log('Start scene: initial prompt');
        conv.overwrite = false;
        conv.scene.next.name = 'actions.scene.END_CONVERSATION';
        conv.add('Hello world from fulfillment');
    });


NOTE: Every time you add a new handler you have to specify it on sdk/webhooks/ActionsOnGoogleFulfillment.yaml file.

Deploying Your Google Action

With the code ready to go, we need to deploy it on Firebase and the Google Actin console so it can be connected to Google Assistant.

Now we can deploy our Google Action with Google actions CLI on the sdk folder:

    gactions push


This command will do the following actions:

  1. Push all the Google Action metadata, interaction model, settings, and configurations To the Google Actions Console.
  2. Push the code of our Cloud Function to Firebase.

After that, we have to execute this command:

    gactions deploy preview


This command will deploy everything in the preview stage of our Google Action. Ready to be tested.

Testing Your Google Action

Once you have deployed your changes, you can go to your Google Action project on the Google Action console and test it:

Test version.

Uploading Your Code from the Google Action Console

Every time you do a change on the google Action Console you can download those changes by running this command:

    gactions pull


Or, if you want to overwrite your local changes you can execute this command:

    gactions pull --force


Resources

  • Official Google Assistant Node.js SDK - Official Google Assistant Node.js SDK
  • Official Google Assistant Documentation - Official Google Assistant Documentation

Conclusion

This was a basic tutorial to learn Google Actions using Node.js. As you have seen in this example, the Google Action SDK for Node.js and the Google Actions Tools like Google actions CLI can help us a lot and also they give us the possibility to create Google Actions easily. I hope this example project is useful to you.

You can find the code here.

That's all folks! Happy coding!

Google Assistant Node.js Command-line interface Firebase Cloud Command (computing) Console (video game CLI)

Opinions expressed by DZone contributors are their own.

Related

  • Alexa Skill With Node.js
  • Alexa Skill With TypeScript
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Building a Voice-Powered Smart Kitchen App Using LLaMA 3.1, Firebase, and Node.js

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!