dialogflow-java-client-v2 icon indicating copy to clipboard operation
dialogflow-java-client-v2 copied to clipboard

how to use in android? the dialogflow api V2

Open yangceng opened this issue 7 years ago • 47 comments

I want use it in android ? Is there a simple of Android?

yangceng avatar Sep 27 '18 07:09 yangceng

Add dependency

    // Java V2
    implementation 'com.google.cloud:google-cloud-dialogflow:0.67.0-alpha'
    implementation 'io.grpc:grpc-okhttp:1.15.1'

Initiate Dialogflow Agent

try {
            InputStream stream = getResources().openRawResource(R.raw.test_agent_credentials);
            GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
            String projectId = ((ServiceAccountCredentials)credentials).getProjectId();

            SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
            SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
            sessionsClient = SessionsClient.create(sessionsSettings);
            session = SessionName.of(projectId, uuid); // uuid = UUID.randomUUID().toString()
        } catch (Exception e) {
            e.printStackTrace();
        }

Make query request

QueryInput queryInput = QueryInput.newBuilder().setText(TextInput.newBuilder().setText("Hi I am Abhinav").setLanguageCode("en-US")).build();
            new RequestJavaV2Task(MainActivity.this, session, sessionsClient, queryInput).execute();

Call Agent in AsyncTask

@Override
    protected DetectIntentResponse doInBackground(Void... voids) {
        try{
            DetectIntentRequest detectIntentRequest =
                    DetectIntentRequest.newBuilder()
                            .setSession(session.toString())
                            .setQueryInput(queryInput)
                            .build();
            return sessionsClient.detectIntent(detectIntentRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

Capture response

@Override
   protected void onPostExecute(DetectIntentResponse response) {
      if (response != null) {
           String botReply = response.getQueryResult().getFulfillmentText();
       } 
   }

abhi007tyagi avatar Oct 23 '18 06:10 abhi007tyagi

@abhi007tyagi I am getting below attached error with above code. I am trying to integrate v2 api in android app. but it seems that lot of changes are done. How to proceed with it??

screenshot_2019-01-06-21-24-33-965_com miui bugreport

myeduapp avatar Jan 06 '19 15:01 myeduapp

will check and revert.

abhi007tyagi avatar Jan 08 '19 10:01 abhi007tyagi

Thanks

On Tue, Jan 8, 2019, 4:06 PM Abhinav Tyagi [email protected] wrote:

will check and revert.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dialogflow/dialogflow-java-client-v2/issues/25#issuecomment-452252288, or mute the thread https://github.com/notifications/unsubscribe-auth/Afbb9FJWenAVCTv8AQZ9dcIqbRwtvZu_ks5vBHTAgaJpZM4W8Ddx .

myeduapp avatar Jan 08 '19 15:01 myeduapp

I'm also having this issue and would really appreciate a solution. Any ideas?

dkeitley avatar Jan 26 '19 20:01 dkeitley

Please check my repo. Check the readme to understand the key you will need for old API.ai way or my medium post for usage with Java API for V2

abhi007tyagi avatar Jan 27 '19 15:01 abhi007tyagi

Hello, I have followed the medium post and my code is identical to the code in the medium post for v2. Initially this worked for me but after adding some intents and additional training I can no longer seem to receive anything with sessionsClient.detectIntent(detectIntentRequest); (returns null). I have webhook enabled rather than using the inline editor (and have in enabled for my intents) as I have a lot of intents. I have rechecked my keys and the only real difference I can find is that i am not using the inline editor for fulfilment.

I am not sure how I can troubleshoot this problem, and its pretty vital that I get this working. Any thought on what the problem might be, or any solutions?

AmberL0uise avatar Feb 13 '19 21:02 AmberL0uise

Please see if you have enabled the individual webhook support for the new intents. Normally, I add new intents one by one and verify if the newly added are working or not rather than adding all the changes at once. Try and see if you could revert back to the working stage and add your changes one step at a time. This will help you to find the root cause.

abhi007tyagi avatar Feb 14 '19 03:02 abhi007tyagi

Thankyou for the fast reply! I have reverted back to an early version using only the default welcome and fallback intents using the inline editor, however this has not resolved my issue. I have double checked my code against the tutorial on medium and the repository and it is identical except for different variable and class names. I have spent the morning going over my authentication settings in my build.gradle, and have generated a new json file placed in the res\raw (android studio) directory to account for any changes which might have happened there.

Given the identical code and simple agent, I can only assume that the issue is with the authentication process or connection which is preventing me from either sending a query or receiving a response.

AmberL0uise avatar Feb 14 '19 11:02 AmberL0uise

The only obvious errors I can find are when I click on "View execution logs in the Firebase console". This shows the errors: ""@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":10,"message":"An operation on function dialogflowFirebaseFulfillment in region us-central1 in project newagent-***** is already in progress. Please try again later."} and, "Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail"

Thanks in advanced.

AmberL0uise avatar Feb 14 '19 11:02 AmberL0uise

I discovered the issue. My "implementation 'io.grpc:grpc-okhttp:1.15.1'", which I had used when creating the project originally from the Medium tutorial had become out of date. I have replaced it with implementation "io.grpc:grpc-okhttp:1.18.0'". It works now.

AmberL0uise avatar Feb 16 '19 18:02 AmberL0uise

@abhi007tyagi hi, thankyou for your code. i've implemented it on my application.

But i wonder how to make double responses on android. it would be great if it can.

Screenshot_20190328-130118 png

ariestwn avatar Mar 28 '19 06:03 ariestwn

In InputStream stream = getResources().openRawResource(R.raw.test_agent_credentials); What is test_agent_credentials file??

Also buildConfigField 'String', "ClientAccessToken", '"abcd"' resValue 'string', "ClientAccessToken", '"abcd"'

how to give to ClientAccessToken to abcd @abhi007tyagi @AmberL0uise @ariestwn

SUBINPTPM avatar Jun 14 '19 05:06 SUBINPTPM

@SUBINPTPM please check my medium post. You will get all the information there.

abhi007tyagi avatar Jun 14 '19 06:06 abhi007tyagi

@abhi007tyagi Hi Sir,

How can I pass parameter to with the QueryInput.

I have succeed in sending parameter with an event call using queryinput.setEvent().setParameter(). But cannot find a way to sent parameter with QueryInput for not an event case .

SUBINPTPM avatar Jun 15 '19 06:06 SUBINPTPM

I think sending parameters from events will be the best options as it will trigger the event only which requires that param. You can try and add the event in the Dialogflow for the context/intent you want to use that parameter and test.

Note: Please do not address me as Sir :)

abhi007tyagi avatar Jun 15 '19 15:06 abhi007tyagi

@abhi007tyagi Hi, I followed your medium tutorial and referred to your code on Github for integrating Dialogflow using the Java client API, however it seems that the response generated from callbackv2() is always null. However in the case of the api.ai:sdk, the response was generated. Is it a linkage issue? I mean tried running the application with service account key saved as .JSON file and once as a .file still the response is always generated as null.

Anubis97 avatar Jul 26 '19 10:07 Anubis97

I am using the current setup in one of my project. Make sure you have first tried my Java V2 code as it is. See if you have commented any code. It should work.

abhi007tyagi avatar Jul 26 '19 15:07 abhi007tyagi

@abhi007tyagi

Hi, I am using Java V2 with exactly the same code and dependencies, while I got this error when running sessionsClient.detectIntent():

com.google.api.gax.rpc.UnavailableException: io.grpc.StatusRuntimeException: UNAVAILABLE: NameResolver returned an empty list

Then I tried tweaking with different dependency versions, such as this one:

implementation 'com.google.cloud:google-cloud-dialogflow:0.102.0-alpha'
implementation 'io.grpc:grpc-okhttp:1.22.1'

which leads to another error when running sessionsClient.detectIntent():

com.google.api.gax.rpc.UnavailableException: io.grpc.StatusRuntimeException: UNAVAILABLE: Unable to resolve host dialogflow.googleapis.com

Seems like dialogflow is no not supported by grpc on Android? I appreciate any suggestions or solutions!

KaihuiLiang avatar Aug 02 '19 18:08 KaihuiLiang

@abhi007tyagi Thank you very much for providing us with the sample code, i have worked with it in an android app taking advantage of Dialogflow V2 API. But how do I use the Java V2 API to implement voice triggers and feedback, do you have any good suggestions? I am looking forward to your reply. Thanks a lot in advance!

YinXiuYu avatar Oct 11 '19 10:10 YinXiuYu

You can implement your own Android's speech to text feature and the converted text can be sent as a query to your agent in Dialogflow.

abhi007tyagi avatar Oct 11 '19 10:10 abhi007tyagi

@abhi007tyagi Thankyou for the fast reply! I want to know that can i use the Dialogflow API V2 to do this directly? And i can receive the voice feedback and not just the text.

YinXiuYu avatar Oct 11 '19 10:10 YinXiuYu

I don't think so.

abhi007tyagi avatar Oct 11 '19 12:10 abhi007tyagi

Does this still work as of 2020?

fanonxr avatar Jan 23 '20 19:01 fanonxr

I've implemented a connection to dialog flow with a similar code that is described in the medium post. I've used only Java V2 API. I had some difficulties with dependencies, but it finally works with this configuration:

    implementation 'com.google.cloud:google-cloud-dialogflow:0.120.2'
    implementation 'io.grpc:grpc-okhttp:1.27.2'
    implementation 'io.grpc:grpc-core:1.27.2'

Wewe12 avatar Mar 04 '20 13:03 Wewe12

Hey @Wewe12, is there a specific reason you used: implementation 'com.google.cloud:google-cloud-dialogflow:0.120.2'?

I was able to make it work with:

implementation 'com.google.cloud:google-cloud-dialogflow:1.0.0'
implementation 'io.grpc:grpc-okhttp:1.27.2'
implementation 'io.grpc:grpc-core:1.27.2'

Corsage avatar Mar 04 '20 15:03 Corsage

@Corsage When I was implementing my solution dialogflow:1.0.0 wasn't released yet. I've updated and it also works for me with 1.0.0.

Wewe12 avatar Mar 10 '20 11:03 Wewe12

I followed the "https://github.com/abhi007tyagi/DialogflowChat" to write a Dialogflow Chatbot app. With using response.getQueryResult().getFulfillmentText(), I can get correct text (both English/ Chinese character) . Now I would like add rich response in app(such as suggestions, basic_card).

I used following command("response.getQueryResult().getFulfillmentMessagesList()") to get all message But all Chinese Characters become a serial of number("\344\270\255\346\226\207" <--- should show "中文") where in the String. A port of response show in the Logcat: fulfillment_messages { platform: ACTIONS_ON_GOOGLE suggestions { suggestions { title: "English" } suggestions { title: "\344\270\255\346\226\207" }

What is the format of the above serial of number? Can anyone give me hint to convert it to Chinese Character? Thank you in advance.

RobotsunRZ avatar Apr 17 '20 04:04 RobotsunRZ

@abhi007tyagi I am following your tutorial on dialogflow api V2 in android, but always getting a null DetectIntentResponse. Can you please help?

Thanks

99ansh avatar Apr 24 '20 14:04 99ansh

Hi, @abhi007tyagi, I've been following your tutorial to create a chatbot, but I keep getting hit with this error: "There was some communication issue. Please Try again!". Any idea why? Thanks!

sebzharshan avatar Jun 15 '20 13:06 sebzharshan