问题
Currently started my journey with Dialogflow,is it possible to display the message from the chatbot into my android app per API?
回答1:
There are 3 ways to integrate Dialogflow into your Android App:
- using Rest API which is not an easy job and frequent issues while creating the request payloads.
- using Android Client by Dialogflow which most stable and featureful as of now but not updated in a year for new Beta features coming in V2.
- using Java API client which is still evolving but supports Beta features and in getting updated on regular basis.
The UI for the Chatbot will be created by you as there is no support currently.
To integrate Android Client follow the below code:
Dependency:
// Dialogflow SDK dependencies
implementation 'ai.api:sdk:2.0.7@aar'
implementation 'ai.api:libai:1.6.12'
Initiate chatbot in onCreate:
final AIConfiguration config = new AIConfiguration("<Client Access Code>",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
aiDataService = new AIDataService(this, config);
customAIServiceContext = AIServiceContextBuilder.buildFromSessionId(uuid);
aiRequest = new AIRequest();
aiRequest.setQuery(msg);
Call Dialogflow in Asynchronous thread:
try {
return aiDataService.request(request, customAIServiceContext);
} catch (AIServiceException e) {
e.printStackTrace();
}
Fetch response from bot:
String botReply = aiResponse.getResult().getFulfillment().getSpeech();
To integrate Java API Client follow the below code:
Dependency:
// Java V2
implementation 'com.google.cloud:google-cloud-dialogflow:0.67.0-alpha'
implementation 'io.grpc:grpc-okhttp:1.15.1'
Initiate chatbot in onCreate:
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);
QueryInput queryInput = QueryInput.newBuilder().setText(TextInput.newBuilder().setText(msg).setLanguageCode("en-US")).build();
Call Dialogflow in Asynchronous thread:
try{
DetectIntentRequest detectIntentRequest = DetectIntentRequest.newBuilder()
.setSession(session.toString())
.setQueryInput(queryInput)
.build();
return sessionsClient.detectIntent(detectIntentRequest);
} catch (Exception e) {
e.printStackTrace();
}
Fetch response from bot:
String botReply = response.getQueryResult().getFulfillmentText();
Hope this helps you :)
--- UPDATE ---
Dialogflow Android Demo using Java V2 API client.
回答2:
if you're looking to make a Detect Intent Request from an Android app, there is no current direct support for using the DialogFlow libraries, but an alternative to that is to use Firebase Functions, in which you can setup Auth to then make a call to your Firebase Function which can then make a Detect Intent Request to Dialogflow and then send back that request from the Firebase Function.
Let me know if that helps or your use case is different from what I had above. :)
*Updated: Android still isn't officially supported by the libraries, but you can follow the guide here that uses a Token Service to get credentials to the android app which can be used to talk to the API directly. https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/dialogflow
回答3:
You can, it's just a lot harder than on a computer. You make the chatbot on the computer, and then download it to your phone. Have a great day!!!
来源:https://stackoverflow.com/questions/53087264/is-it-possible-to-display-dialogflow-chatbot-into-android-app-per-api