How to embed a chat bot in the Android application. Get answers from the bot in the form of a json-array and display? or try to integrate it?
1 answer
Everything is quite simple, but since I have never worked with a bot, I will try to help using my knowledge as much as possible. As far as I know, the essence of the bot, as you most likely know, is that the person communicates with the program. You want to handle the answers of the bot in your application, and for this you should have a few things:
- Api where you send the request - if it is a specific bot, and most likely a specific bot, then you need to receive answers from the bot you need. For this you must have api. Most often, api is represented as a piece or a whole address.
- A query class (the data that is needed to receive a response from your bot) is a class or heap of data that is required by the server for your identification. For example, for your identification, the server needs your logs and password. Next you need to send the bot id or how they are identified. So you will have three fields in your heap of data - login, password and id-bot. And you create a class that holds all these fields, for example:
public class Regest{ private Integer id; private String login; private Integer bot_id;
public Regest(Integer id,String login,Integer bot_id) { this.id= id; this.login= login; this.bot_id= bot_id; } }
- Class response - this class will help you handle the response of your server, that is, the answer of your bot. This class will include an array of your messages and some other data that it will send, without an example of an answer, it is quite difficult to imagine what is there.
- Sending a request from the activity and filling in your view element: After you have done all of the above, it's time for you to send a request. To do this, from the activity you must send the request and the received data to parse into a view:
public APIService apiService() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(APIService.class); } public void getData() { mAPIService.auth(new Regest(login, password, bot_id)).enqueue(new Callback<SpeakWithBot>() { @Override public void onResponse(@NonNull Call<SpeakWithBot> call, @NonNull Response<SpeakWithBot> response) { if (response.isSuccessful()) { здесь обрабатываете то что вам прислал сервер } @Override public void onFailure(@NonNull Call<GetToken> call, @NonNull Throwable t) { } });
for processing your answer, you can use both recyclerView with an adapter tailored to your data, or any other view item. If I am mistaken somewhere, then there will be those who correct me, or correct my answer. But for some reason I am sure that using retrofit, you can process what your bot sends you in return. I hope that helped something in solving your problem. Good luck :)
- Thanks, this is exactly what you need - DragonProd
- if you give a comprehensive answer, mark it as accepted :) - Andrew Goroshko