Please be aware that this application / sample is provided as-is for demonstration purposes without any guarantee of support
With the deprecation of the Android Dialogflow SDK and the move from Dialogflow V1 to Dialogflow V2 being forced in March 2020 there is no native option to incorporate Dialogflow into your Android application. There are several API clients available for Dialogflow V2 but note that the Java client does not support Android.
To call Dialogflow V2 Agents from Android it is therefore necessary to either use the REST API or an intermediary system that does have a DF client and can manage the DF communications. Authenticating the REST API on Android does not seem to be supported so this project demonstrates how to use Firebase Functions to communicate with Dialogflow via the DF NodeJS API.
You could use any number of intermediary server or serverless component but since Firebase lives within GCP, as does Dialogflow, all the authentication seems to happen automatically.
There is a reference project implementation for this code which has usage limits as defined by the free tier for Firebase, GCP and Dialogflow:
- Reference project ID: androidv2dialogflow-xspkei
- Reference project Dialogflow Agent: Only Supports a single question, 'How are you?'.
You can compile the sample Android project and run it against this reference backend.
The Firebase function exposes an HTTP onCall endpoint which can be invoked via a POST to https://us-central1-androidv2dialogflow-xspkei.cloudfunctions.net/detectIntent
. The question is provided as a JSON object in the body but bear in mind that this reference implementation only supports a single question
{
"data": {
"question": "How are you today?"
}
}
Postman output is shown below:
You can also run the Android client in this project and ask the same question via speech-to-text:
These prerequisites are only required to modify the existing project
- Node.js and NPM
- Install the Firebase CLI
- Follow the Dialogflow setup instructions to create a project.
- Create an Intent to handle the conversation your GDF instance supports. The reference implementation has only a single Intent
- If you want to test GDF from a Windows or Linux box you will need to set up authentication but once I deployed to Firebase, and everything was part of the same GCP project essentially, I did not need to configure separate authentication.
- Modify the
index.js
file in thefunctions
directory to refer to your GDF project ID- To find your Project ID: In Dialogflow console under Settings ⚙ > General tab > Project ID.
- On your local machine, in the
functions
directory, runnpm install
- Run `firebase deploy --project {PROJECT_ID} to deploy the function
- The GDF Quickstart docs were good to get started with and this example cribs most of the code from there.
- Note: The
index.js
file exposes an HTTP onCall endpoint but an earlier version exposed an onRequest endpoint. I left the latter commented out in case it is useful
The Android client in this project will, by default, communicate with the reference project. To communicate with your own Firebase instance, add the Android application to your Firebase project and configure the Android project with your own google-services.json
file. More info.
The client uses the Firebase Functions SDK and Android Speech Recognition / text to speech APIs which require a GMS device to run.
As shown in the below image, just tap the mic (floating action) button and ask the only supported question by GDF, 'how are you feeling?'. The response will be given on the UI and via text to speech, so ensure your volume is turned up.
Below: Speech recognition has succeeded.
Below: Response has been received from Google Dialogflow