Make chatbot with Dialogflow for Android app

Kristina Simakova
2 min readApr 24, 2019

--

Chatbots got very popular in the last couple of years. Many businesses have started using chatbots to automate customer service, place new orders or engage with followers.

In this short tutorial I want to show how to integrate Dialogflow to your Android app.

Note: Dialogflow is currently migrating to V2. Dialogflow client library and Dialogflow API V1 have been deprecated and will be shut down on October 23th, 2019. As for today, V2 client library is still in alpha.

  1. Add dependencies:
implementation 'ai.api:sdk:2.0.7@aar'
implementation 'ai.api:libai:1.6.12'

2. Add AIConfiguration with your “Client access token” from Dialogflow console:

Screenshot of Dialogflow console
config = new AIConfiguration("ACCESS_TOKEN",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);

3. Add AIService and make your activity implement AIListener:

aiService = AIService.getService(this, config);
aiService.setListener(this);

4. As your activity now implements AIListener, it will get an onResult callback where you can handle your request results:

@Override
public void onResult(AIResponse response) {
Result r = response.getResult();
inputTextView.setText(r.getResolvedQuery()));
fulfillmentTextView.setText(r.getFulfillment().getSpeech());
actionTextView.setText(r.getMetadata().getIntentName()));
}

5. Start listening when button is clicked:

aiService.startListening();

6. It is also possible to send text instead of voice with:

AIResponse response = aiService.textRequest(new AIRequest("text"));

Tip: Do not forget to ask for RECORD_AUDIO permission

I hope this short tutorial can help some developers experimenting with Dialogflow in android apps. It seems like the API works today, but it will be removed at the end of October. Stay tuned for new tutorials & check out a link to a demo app below.

--

--