如何在Android应用中集成对话框流程

时间:2019-06-22 18:14:03

标签: android dialogflow recycler-adapter

我想将我的chatbot(由dialogflow创建)集成到我的应用程序中,并且已经创建了xml文件,但是找不到如何在它们之间进行匹配的方法。 我已经使用会议室库持久性来存储数据库。而且,我正在使用Java进行编码。

如何将我的代理整合到我的应用中?如何为我的聊天消息添加recylerViewAdapter。 谢谢

msg_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/leftText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_alignParentStart="true"
    android:text="Hello this is me!!"
    android:padding="8dp"
    android:textColor="#212121"
    android:background="@drawable/left_background"
    android:elevation="2dp"
    android:layout_alignParentLeft="true" />


  <TextView
    android:id="@+id/rightText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_alignParentEnd="true"
    android:text="Hi!! How are you!!"
    android:background="@drawable/right_background"
    android:textColor="#fff"
    android:padding="8dp"
    android:elevation="2dp"
    android:layout_alignParentRight="true" />


   </RelativeLayout>

Chatbot.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Chatbot">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        android:paddingBottom="50dp"
        android:clipToPadding="false"
        android:background="#f4f6f7"
        tools:listitem="@layout/msglist"
        />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="5dp"
            android:layout_marginBottom="10dp"
            android:elevation="2dp"
            android:layout_toStartOf="@+id/addBtn"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/addBtn">

            <EditText
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:background="#fff"
                android:hint="Type a Message"
                android:minHeight="50dp"
                android:textSize="18sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/addBtn"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentEnd="true"
            android:background="@drawable/back_fab"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="5dp"
            android:elevation="4dp"
            android:layout_centerInParent="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp">
            <ImageView
                android:id="@+id/fab_img"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerInParent="true"
                android:src="@drawable/ic_send_white_24dp"
                android:tint="#fff"/>
        </RelativeLayout>

    </RelativeLayout>

    </RelativeLayout>

ChatbotActivity

public class Chatbot extends AppCompatActivity implements AIListener {
RecyclerView recyclerView;
EditText message;
RelativeLayout addBtn;
ChatbotAdapter adapter;
Boolean flagFab = true;
private AIService aiService;
private AIServiceContext customAIServiceContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chatbot);

    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
    message = (EditText)findViewById(R.id.message);
    addBtn = (RelativeLayout)findViewById(R.id.addBtn);

    recyclerView.setHasFixedSize(true);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(linearLayoutManager);



    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String msg = message.getText().toString().trim();

            if (!message.equals("")) {

                ChatMessage chatMessage = new ChatMessage(msg, "user");

            }

            message.setText("");

        }
    });


         adapter = new ChatbotAdapter();


    recyclerView.setAdapter(adapter);

    final AIConfiguration config = new AIConfiguration("Acces", 
     AIConfiguration.SupportedLanguages.French,
            AIConfiguration.RecognitionEngine.System);

    aiService = AIService.getService(this, config);
    customAIServiceContext = AIServiceContextBuilder.buildFromSessionId("ID");
    aiService.setListener(this);

    final AIDataService aiDataService = new AIDataService(config);

    final AIRequest aiRequest = new AIRequest();
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String msg = message.getText().toString().trim();

            if (!msg.equals("")) {

                aiRequest.setQuery(msg);
                new AsyncTask<AIRequest,Void, AIResponse>(){

                    @Override
                    protected AIResponse doInBackground(AIRequest... aiRequests) {
                        final AIRequest request = aiRequests[0];
                        try {
                            final AIResponse response = 
                       aiDataService.request(aiRequest);
                            return response;
                        } catch (AIServiceException e) {
                        }
                        return null;
                    }
                    @Override
                    protected void onPostExecute(AIResponse response) {
                        if (response != null) {

                            Result result = response.getResult();
                            String reply = result.getFulfillment().getSpeech();
                        }
                    }
                }.execute(aiRequest);
            }
            else {
                aiService.startListening();
            }

            message.setText("");

        }
    });

}

@Override
public void onResult(AIResponse response) {

}

@Override
public void onError(AIError error) {

}

@Override
public void onAudioLevel(float level) {

}

@Override
public void onListeningStarted() {

}

@Override
public void onListeningCanceled() {

}

@Override
public void onListeningFinished() {
 }
}

聊天消息

public class ChatMessage {

private String msgText;
private String msgUser;



public ChatMessage(String msgText, String msgUser){
    this.msgText = msgText;
    this.msgUser = msgUser;

}


public ChatMessage(){

}

//+getter & setter
}

ChatbotAdapter //我不知道如何编写此类

 public class ChatbotAdapter  extends RecyclerView.Adapter<ChatbotAdapter.ChatHolder> 
 {
@NonNull
@Override
public ChatHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.msglist, viewGroup, false);
    return new ChatHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull ChatHolder chatHolder, int i) {
}

@Override
public int getItemCount() {
    return 0;
}

public class ChatHolder extends RecyclerView.ViewHolder {
    TextView leftText, rightText;

    public ChatHolder(View itemView) {
        super(itemView);

        leftText = (TextView) itemView.findViewById(R.id.leftText);
        rightText = (TextView) itemView.findViewById(R.id.rightText);
    }
}
  }

1 个答案:

答案 0 :(得分:1)

您的问题不清楚。请添加更多详细信息,例如您想要什么,已实现什么以及即将出现什么问题。

您也可以尝试我的实现。我没有在实现中使用Recycler视图,但是可以遵循我为将Dialogflow与Android集成而编写的以下article。请尝试遵循V2版本,因为V1将于今年10月逐步淘汰。

另外,请勿将片段用于Chatbot,因为它有时无法正常工作。尝试使用我的实现创建一个简单的聊天机器人,然后您可以朝着自己的实现努力。 希望它能起作用。