Я нуб !! Я сделал android чат-бот с диалоговым потоком, но приложение перестает работать после того, как я вхожу в hello, и не отвечает
MainActivity. java
package com.tyagiabhinav.dialogflowchat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.dialogflow.v2beta1.DetectIntentResponse;
import com.google.cloud.dialogflow.v2beta1.QueryInput;
import com.google.cloud.dialogflow.v2beta1.SessionName;
import com.google.cloud.dialogflow.v2beta1.SessionsClient;
import com.google.cloud.dialogflow.v2beta1.SessionsSettings;
import com.google.cloud.dialogflow.v2beta1.TextInput;
import java.io.InputStream;
import java.util.UUID;
import ai.api.AIServiceContext;
import ai.api.AIServiceContextBuilder;
import ai.api.android.AIConfiguration;
import ai.api.android.AIDataService;
import ai.api.model.AIRequest;
import ai.api.model.AIResponse;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int USER = 10001;
private static final int BOT = 10002;
private String uuid = UUID.randomUUID().toString();
private LinearLayout chatLayout;
private EditText queryEditText;
// Android client
private AIRequest aiRequest;
private AIDataService aiDataService;
private AIServiceContext customAIServiceContext;
// Java V2
private SessionsClient sessionsClient;
private SessionName session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ScrollView scrollview = findViewById(R.id.chatScrollView);
scrollview.post(() -> scrollview.fullScroll(ScrollView.FOCUS_DOWN));
chatLayout = findViewById(R.id.chatLayout);
ImageView sendBtn = findViewById(R.id.sendBtn);
sendBtn.setOnClickListener(this::sendMessage);
queryEditText = findViewById(R.id.queryEditText);
queryEditText.setOnKeyListener((view, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
sendMessage(sendBtn);
return true;
default:
break;
}
}
return false;
});
// Android client
initChatbot();
// Java V2
initV2Chatbot();
}
private void initChatbot() {
final AIConfiguration config = new AIConfiguration(BuildConfig.ClientAccessToken,
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
aiDataService = new AIDataService(this, config);
customAIServiceContext = AIServiceContextBuilder.buildFromSessionId(uuid);// helps to create new session whenever app restarts
aiRequest = new AIRequest();
}
private void initV2Chatbot() {
try {
InputStream stream = getResources().openRawResource(R.raw.smalltalkdaiaxs981db5c2e2a8);
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);
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMessage(View view) {
String msg = queryEditText.getText().toString();
if (msg.trim().isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter your query!", Toast.LENGTH_LONG).show();
} else {
showTextView(msg, USER);
queryEditText.setText("");
// Android client
aiRequest.setQuery(msg);
RequestTask requestTask = new RequestTask(MainActivity.this, aiDataService, customAIServiceContext);
requestTask.execute(aiRequest);
// Java V2
QueryInput queryInput = QueryInput.newBuilder().setText(TextInput.newBuilder().setText(msg).setLanguageCode("en-US")).build();
new RequestJavaV2Task(MainActivity.this, session, sessionsClient, queryInput).execute();
}
}
public void callback(AIResponse aiResponse) {
if (aiResponse != null) {
// process aiResponse here
String botReply = aiResponse.getResult().getFulfillment().getSpeech();
Log.d(TAG, "Bot Reply: " + botReply);
showTextView(botReply, BOT);
} else {
Log.d(TAG, "Bot Reply: Null");
showTextView("There was some communication issue. Please Try again!", BOT);
}
}
public void callbackV2(DetectIntentResponse response) {
if (response != null) {
// process aiResponse here
String botReply = response.getQueryResult().getFulfillmentText();
Log.d(TAG, "V2 Bot Reply: " + botReply);
showTextView(botReply, BOT);
} else {
Log.d(TAG, "Bot Reply: Null");
showTextView("There was some communication issue. Please Try again!", BOT);
}
}
private void showTextView(String message, int type) {
FrameLayout layout;
switch (type) {
case USER:
layout = getUserLayout();
break;
case BOT:
layout = getBotLayout();
break;
default:
layout = getBotLayout();
break;
}
layout.setFocusableInTouchMode(true);
chatLayout.addView(layout); // move focus to text view to automatically make it scroll up if softfocus
TextView tv = layout.findViewById(R.id.chatMsg);
tv.setText(message);
layout.requestFocus();
queryEditText.requestFocus(); // change focus back to edit text to continue typing
}
FrameLayout getUserLayout() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
return (FrameLayout) inflater.inflate(R.layout.user_msg_layout, null);
}
FrameLayout getBotLayout() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
return (FrameLayout) inflater.inflate(R.layout.bot_msg_layout, null);
}
}
В файлах журнала коллизии пожарной базы сказано, что это получил приветственный запрос, но я не мог найти причину закрытия приложения, не возвращая никакого ответа.
Если вам нужен build.gradle или запрос java файлов, дайте мне знать.
Кто-то, пожалуйста, помогите.