How to use Google Firebase with Alexa Skill

In this tutorial i’m going to show you how you can easily connect and use Google Firebase’ Real-Time Database with your Alexa Skill on GoVocal.AI

Firebase’ Real-Time Database helps to store and sync data with cloud database. Data is synchronized in realtime across all clients, like if you have web, desktop, android or IOS apps, etc connected to firebase then any change you made in cloud database data will instantly reflects on all other clients.

You can control your websites with Alexa, control your android or IOS apps with Alexa, and much much more.

For Example: Suppose you have Alexa Skill which takes a order for a restaurant, so whenever a order is taken by Alexa, Firebase will push the Order data to restaurant’s dashboard or their mobile app in real time.

Let’s get started.

1. Register/Login to your Account

Visit portal.govocal.ai and Login to your account. If you don’t have one, Register a new account.

2. Create a New project

After Login, create a new project and on the Select Template page, Select Custom Skill Advanced template and finally give your new project a name and some description if you want to.

See Getting started with GoVocal.AI

Once your Project is up, then you will see Conversation Flow Designer, here you can visually build your Alexa Skill conersations. See below.

See Conversation Flow Designer.

3. Add the Function, Firebase Query and Firebase Save nodes.

Search the Function Node, Firebase Query and Firebase Save nodes from Upper left in Palette section and Drag & place them just after the Welcome Message Node. (As shown below in red box)

Right now, both Firebase Query and Firebase Save nodes are showing the Invalid Credentials, because till yet we haven’t provided any Firebase credentials.

Firebase Query Node

Firebase Query Node is helpful to search or query for a particular child path in Firebase Real-time database. Like if you search for child path /WelcomCounts, then it will fetch the data from database and poluplate the results in property payload of msg object.

Function Node

Here you can write your own custom Javascript function, this node takes the msg object as input and outputs/passes the modified msg object to the next attached node.

Firebase Query Node

Firebase Save Node is helpful to save/store data on a particular child path in Firebase Real-time database. You can choose to set, push or even remove the data from database.

4. Get Firebase credentials

  • Go to Firebase Console
  • Create a new project
  • Once a new firebase project is created, go to Project Settings by clicking on button in upper left corner.(see below)

  • Copy Project ID and Web API Key, and then open Service Accounts tab

  • Click on Generate New Private Key and save it on your local computer.

5. Add Firebase credentials

  • Open Conversation flow designer
  • Double-click and open Firebase Query Node in Conversation flow designer
  • Click on Edit icon
  • Add the previously copied Firebase Project ID, Web API Key and upload the Service Accounts file
    Select the Auth type as Administrator to upload the Service Account Key.

    Note: Both Web API Key and Service Accounts file, are stored into database in encrypted format, such that you and only you can see them.

  • Click on Update

6. Get database’ child path data with Firebase Query Node

Add the firebase database child path as /WelcomeCounts, as shown below and Click on Done.

So whenever a user invokes your skill, we will fetch the /WelcomeCounts child path data from Firebase database and populate the msg.payload with that data and finally pass this transformed msg object to next attached Function node.

7. Add Custom Javascript code in Function Node

  • Double-click and open Function Node

  • add the below javascript code

    var previousCount = msg.payload;
    var newCount = 1;
    if (previousCount && typeof previousCount == ‘number’) {
        newCount = previousCount + 1;
    }
    msg.payload = newCount;
    return msg;
    

Here, above function block will check the value of previousCount passed from Firebase Query node, and see if this value is a number then increase newCount with one and at the end sets the msg.payload value to the newCount & finally return the modified msg object to next attached Firebase Save Node.

  • Click on Done

8. Save the newCount value to Firebase Database with Firebase Save Node

  • Double-click and open the Firebase Save Node

  • Select the Firebase Config by clicking on select box as shown below

  • Set Child Path as /WelcomeCounts

  • Select Value as msg and add payload

  • Click Done

9. Save project

  • Click on Save Project in upper right corner
  • Once your project is successfully saved and if provided firebase credentials are correct then a green tick with label ready will appear on the Firebase nodes, confirming successful connect with firebase. (see below)

10. Deploy your skill to Alexa

Now its time to deploy your project to Amazon Alexa and create a new Alexa Skill.
See How to create and deploy Alexa skill.

11. Open Firebase Database

  • Open you Firebase Project
  • Open Real-time Database by clicking on Database present in left menu
  • Initially, our Firebase database is empty.

12. Invoke your Alexa Skill

Invoke your skill on Amazon Alexa Test Simultor or on you Alexa Mobile app.
See How to Test Alexa Skill.
Once our Alexa skill is invoked, let’s check the Firebase Database

We see our Database which was initially empty now has child WelcomeCounts and its value 1.

Test: Invoke your skill multiple times, and see whether value of WelcomeCounts increases or not.


This was our simple demostration how you can easily connect your Alexa Skill with Firebase Real time database.

Combining the Alexa and Firebase opens up the whole new level of capabilities, which you can do to make your Alexa Skill super powerful and more useful.

Related