Search code examples
javaspring-bootgoogle-cloud-platformdialogflow-cx

Can somebody help me to get started with Google Dialogflow in spring boot


Searched a lot but there is no precise answer on how to get started with dialogflow in spring boot.

Aim: To detect intent from GDF knowledgebase and return back the response.

What I have done so far:

Tried executing this code https://github.com/googleapis/java-dialogflow/blob/HEAD/samples/snippets/src/main/java/com/example/dialogflow/DetectIntentTexts.java by creating a main app.

App.java

package com.example.dialogflow;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) {

        DetectIntentTexts theDetectIntentTexts = new DetectIntentTexts();

        String projectId = "abc";
        String sessionId = "xyz";
        String lang = "en";

        List<String> myTexts = new ArrayList<>();
        myTexts.add("hi");

        String ans = null;
        try {
            ans = String.valueOf(theDetectIntentTexts.detectIntentTexts(projectId, myTexts, sessionId, lang));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Bot reply:" + ans);

    }

   

}


But it fails to run.

I have my service account GCP set in local machine and export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json is set too

Any help would be appreciated.


Solution

  • Finally was able to solve it. Steps:

    First run

    gcloud auth application-default revoke
    

    This will remove user account & service account credentials.

    Now login into gcloud using service account credentials.

    gcloud auth activate-service-account --key-file=key.json
    

    Wonder why we have to revoke user account credentials because keeping both the accounts and activating service account didn't work.

    Now it works.