Google Assistant & Alexa chatbot on Firebase Cloud Functions
In this post, I will explain how to make a middleware capable of responding in Google Assistant and Alexa to the same questions. In this case, we will do it with Firebase Functions. We will avoid duplicated code putting all the logic in one place.
Requirements
It’s important to know a little bit of Dialogflow, Alexa Skills and Nodejs. It doesn’t have to be advanced, as I will explain every step of the way.
Let’s start
First, create your Google project with Firebase.
We will go to the Firebase console and we will create a new project
We create a directory on our pc/mac where we will develop the middleware. It will consist of 2 functions that will be the fulfillment for Dialogflow and Alexa Skills. On the terminal, we’ll go to this directory and we’ll write:
npm install firebase-functions@latest firebase-admin@latest --savenpm install -g firebase-toolsfirebase loginfirebase init functions
Choose the project ID that you just created, JavaScript, not debuggable (optional) and install dependencies with npm(If you do not have it installed, you can do it here)
We already have our base project, now we are going to install the library of Google Assistant and Dialogflow and you will see that inside the folder functions you will have a file called package.json, replace it with the following one:
On your terminal, in [yourProject] / functions directory execute:
npm install
Second, create the responses generator
The example of this tutorial will be a bot that answers the question: What time is it?. So the logic that gets our response for this intent will be the same to both platforms, then all we have to do is format this response for each channel and return it, as you can see below
Replace your index.js by this
On your terminal, in [yourProject] / functions directory execute:
firebase deploy
Now in your firebase console in the Functions section, you can see the two functions that have been generated and the URLs we’ll use for the fulfillment of Dialogflow and Alexa Skills
Third, connect your answer generator as a fulfillment to Dialogflow and Alexa Skills
As you may have seen before in the index.js, the name of the intent that answers what we want is called ‘GetCurrentTimeIntet’. In order to do it you have to create this intent with the same name, as seen in the following captures.
In Dialogflow, in the intent(GetCurrentTimeIntent), activate the option that says “Enable webhook call for this intent”
In the fulfillment section, enable Webhook, and put in the URL the endpoint that we generated before for Dialogflow
In Alexa Skills add an intent with the same name as the intent of Dialogflow, if you don’t have it you could replace you JSON by the following in the JSON Editor.
{
"interactionModel": {
"languageModel": {
"invocationName": "timebot",
"intents": [
{
"name": "AMAZON.FallbackIntent",
"samples": []
},
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "GetCurrentTimeIntent",
"slots": [],
"samples": [
"What time is it"
]
}
],
"types": []
}
}
}
In the Alexa Skill, select the HTTPS endpoint type, paste the alexa url from Firebase and choose the second option “My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority"
Don’t forget to save the changes on both platforms before submitting
Result
In Alexa
In Google Assistant
Any questions you can write me in comments, in Spanish too