问题描述
当前是从Dialogflow开始的,是否可以按照API将聊天机器人中的消息显示到我的android应用中?
Currently started my journey with Dialogflow,is it possible to display the message from the chatbot into my android app per API?
推荐答案
有3种方法可以将Dialogflow集成到您的Android应用中:
There are 3 ways to integrate Dialogflow into your Android App:
- 使用Rest API并非一件容易的事,并且在创建请求有效负载时经常遇到问题.
- 使用由Dialogflow提供的Android客户端,该功能目前最稳定,功能最强大,但目前还没有一年内进行了更新,以提供V2中的新Beta版功能.
- 使用Java API客户端不断发展,但支持Beta功能并可以定期更新.
- 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.
您将创建Chatbot的UI,因为目前不支持.
The UI for the Chatbot will be created by you as there is no support currently.
要集成Android客户端,请遵循以下代码:
依赖性:
// Dialogflow SDK dependencies
implementation 'ai.api:sdk:2.0.7@aar'
implementation 'ai.api:libai:1.6.12'
在onCreate中启动聊天机器人:
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);
在异步线程中调用Dialogflow:
Call Dialogflow in Asynchronous thread:
try {
return aiDataService.request(request, customAIServiceContext);
} catch (AIServiceException e) {
e.printStackTrace();
}
从漫游器获取响应:
String botReply = aiResponse.getResult().getFulfillment().getSpeech();
要集成Java API Client,请遵循以下代码:
依赖性:
// Java V2
implementation 'com.google.cloud:google-cloud-dialogflow:0.67.0-alpha'
implementation 'io.grpc:grpc-okhttp:1.15.1'
在onCreate中启动聊天机器人:
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();
在异步线程中调用Dialogflow:
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();
}
从漫游器获取响应:
String botReply = response.getQueryResult().getFulfillmentText();
希望这对您有所帮助:)
Hope this helps you :)
---更新---
使用Java V2 API客户端的Dialogflow Android演示.
这篇关于是否可以按照API将Dialogflow chatbot显示到android应用中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!