Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Sample application to show how to call DialogFlow V2 APIs from an Android application using Firebase functions

License

Notifications You must be signed in to change notification settings

darryncampbell/AndroidV2DialogFlow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Please be aware that this application / sample is provided as-is for demonstration purposes without any guarantee of support

AndroidV2Dialogflow

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.

Architecture

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.

Testing the Firebase function

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:

Testing

You can also run the Android client in this project and ask the same question via speech-to-text:

Android Client

Running the code in your own project

Prerequisites

These prerequisites are only required to modify the existing project

Dialogflow

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

Firebase

  • Modify the index.js file in the functions 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, run npm 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

Android Client

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.

Using the client

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.

Android Client

Below: Speech recognition has succeeded.

Android Client

Below: Response has been received from Google Dialogflow

Android Client

About

Sample application to show how to call DialogFlow V2 APIs from an Android application using Firebase functions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published