dialogflow / dialogflow-java-client-v2

Java client for Dialogflow: Design and integrate a conversational user interface into your applications and devices.
Apache License 2.0
59 stars 58 forks source link

how to use in android? the dialogflow api V2 #25

Open yangceng opened 5 years ago

yangceng commented 5 years ago

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

abhi007tyagi commented 5 years ago

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();
        } 
    }
myeduapp commented 5 years ago

@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

abhi007tyagi commented 5 years ago

will check and revert.

myeduapp commented 5 years ago

Thanks

On Tue, Jan 8, 2019, 4:06 PM Abhinav Tyagi notifications@github.com 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 .

dkeitley commented 5 years ago

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

abhi007tyagi commented 5 years ago

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

AmberL0uise commented 5 years ago

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?

abhi007tyagi commented 5 years ago

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.

AmberL0uise commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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.

ariestwn commented 5 years ago

@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

SUBINPTPM commented 5 years ago

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

abhi007tyagi commented 5 years ago

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

SUBINPTPM commented 5 years ago

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

abhi007tyagi commented 5 years ago

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 :)

Anubis97 commented 4 years ago

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

abhi007tyagi commented 4 years ago

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.

KaihuiLiang commented 4 years ago

@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!

YinXiuYu commented 4 years ago

@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!

abhi007tyagi commented 4 years ago

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.

YinXiuYu commented 4 years ago

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

abhi007tyagi commented 4 years ago

I don't think so.

fanonxr commented 4 years ago

Does this still work as of 2020?

Wewe12 commented 4 years ago

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'
Corsage commented 4 years ago

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'
Wewe12 commented 4 years ago

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

RobotsunRZ commented 4 years ago

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.

99ansh commented 4 years ago

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

Thanks

sebzharshan commented 4 years ago

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!

abhi007tyagi commented 4 years ago

@sebzharshan if you are building a signed release build you need to add proguard rules. Check my GitHub page and see latest solved issue to get the exact proguard rules.

sebzharshan commented 4 years ago

Will do. Thanks for the quick reply, @abhi007tyagi!

abhi007tyagi commented 4 years ago

@sebzharshan try this -keep public class com.google.api.gax.core.** { public protected *; } -keep public class com.google.auth.oauth2.** { public protected *; }

sebzharshan commented 4 years ago

So, I added these to the 'proguard-rules.pro', but it still seems to be giving the same error. Just to make sure, all I'm supposed to do is enter these in the 'proguard-rules.pro', right? @abhi007tyagi

abhi007tyagi commented 4 years ago

@sebzharshan yes

sebzharshan commented 4 years ago

Thanks for the reply, @abhi007tyagi. I see. In the 'proguard-rules.pro', when I type in the two rules you mentioned, I get "Unresolved class name". Could it be an issue with the dependencies?

sebzharshan commented 4 years ago

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

Thanks

I don't know if you figured it out or not, in case you didn't, or if anyone else is facing the same issue: I found out that the 'null DetectIntentResponse' issue had something to do with the API Key (.json file). Here's how to resolve it;

  1. Create a new API Key (for cloud services) from the GCP.
  2. Download the JSON file, copy the content in it, and save it in a .json file in the res/raw.

Hope this helps!

abhi007tyagi commented 4 years ago

@sebzharshan Well in my article I have already mentioned that you will need to create your own JSON. In case, you or anyone want an easy way, I have created an Android library to easily integrate a chatbot into your app. Just follow my new medium post for details Android Dialogflow Chatbot Library

sebzharshan commented 4 years ago

@sebzharshan Well in my article I have already mentioned that you will need to create your own JSON. In case, you or anyone want an easy way, I have created an Android library to easily integrate a chatbot into your app. Just follow my new medium post for details Android Dialogflow Chatbot Library

Oh, I see. I'll have a read. Thanks for the reply!

Gurjeet-Ujjainwar commented 3 years ago

@abhi007tyagi sir I am new to this domain can you please help me in it. I am unable to get where to put the projectid, and other keys in the code given by you.

abhi007tyagi commented 3 years ago

@Guru09 It is assumed that you know the basic knowledge of creating Chatbots using Dialogflow and Android apps. If you are completely new to either of the platform, you should first start looking into the tutorials available on the website for each platform. If you know basics of Android and Dialogflow and wanted to quickly add a chatbot to your android app then I have created a library to integrate chatbot that you may use. It is still in development but will work with most of the functionalities you need. If you still want to follow my previous work, then I would say, you will not need project ID only Google credentials generated in a JSON form which is clearly explained the article.

pratikjoshi999 commented 3 years ago

Hi @abhi007tyagi, how the bot will listen, if the message passed through speech. Like for v1 we had AIListener which gives a predefined method startListening(). Is there any replacement of this.

abhi007tyagi commented 3 years ago

@pratikjoshi999 V2 is not currently designed to support Android fully. Try to use your imagination. Think about how you can do audio support. This is what I say to all the new developers. Android provided STT and TTS features. Use them. Just use basic Speech to text from Android SDK and convert speech into text and use this text as input for Dialogflow. If you don't want to code, I have a library created just use it and have a similar mic icon available in your chat as V1.

tianfatt commented 3 years ago

Hi @abhi007tyagi, I have followed your tutorial to apply on Android application. It worked for the first few days, but it experiences this error on this line of code, "return sessionsClient.detectIntent(detectIntentRequest);" since 19/8/2020. com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

I have checked through the Google Helpdesk and try to set up this Authentication Credentials but GCP informs me that I do not need to set up for this. Can you please assist me on this?

Dependencies I am using are as below: implementation 'com.google.cloud:google-cloud-dialogflow:2.2.0' implementation 'io.grpc:grpc-okhttp:1.31.1'

I tried to change to the dependencies u mentioned here, but same error occurs.

tianfatt commented 3 years ago

Hi @abhi007tyagi, I have followed your tutorial to apply on Android application. It worked for the first few days, but it experiences this error on this line of code, "return sessionsClient.detectIntent(detectIntentRequest);" since 19/8/2020. com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

I have checked through the Google Helpdesk and try to set up this Authentication Credentials but GCP informs me that I do not need to set up for this. Can you please assist me on this?

Dependencies I am using are as below: implementation 'com.google.cloud:google-cloud-dialogflow:2.2.0' implementation 'io.grpc:grpc-okhttp:1.31.1'

I tried to change to the dependencies u mentioned here, but same error occurs.

@abhi007tyagi Sir, is there any update so far?

abhi007tyagi commented 3 years ago

@tianfatt try to create new credential JSON and use it. see if you have any setting that expires it after some time. If you want, you could try the library I created to easily add chatbot into your app. https://levelup.gitconnected.com/android-dialogflow-chatbot-library-6b7b3822e7bc

tianfatt commented 3 years ago

@tianfatt try to create new credential JSON and use it. see if you have any setting that expires it after some time. If you want, you could try the library I created to easily add chatbot into your app. https://levelup.gitconnected.com/android-dialogflow-chatbot-library-6b7b3822e7bc Hi @abhi007tyagi, I have actually tried to generate the credential JSON for multiple times, yet it still fails. I managed to come across another solution on Youtube, https://www.youtube.com/watch?v=zVxDBBCdpfY&ab_channel=BAE-BeingAverageEngineer. Credit to his tutorial, I found out that your first tutorial has some missing code causing this error "UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential." This is solved by adding ".createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));" in the line of "GoogleCredentials credentials = GoogleCredentials.fromStream(stream).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));". Perhaps Sir can update your post and first Github repository. Anyway thanks a lot for the guidance, really appreciate that.