Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Chatbot with response options in flutter?

See the image below: example

How Can I implement auto responses using DialogFlow or any other chatbot frameworks in flutter.

I just want to know the method to get the desired result highlighted in the red region.

like image 693
Maadhav Sharma Avatar asked Oct 11 '19 12:10

Maadhav Sharma


People also ask

What is Dialogflow in flutter?

Dialogflow is a development suite that incorporates Google's machine learning expertise to build end-to-end,deploy-everywhere interfaces for websites, mobile applications, and IoT devices. Build natural and rich conversational experiences.

What is Dialogflow chatbot?

Dialogflow is a natural language understanding platform that makes it easy to design and integrate a conversational user interface into your mobile app, web application, device, bot, interactive voice response system, and so on.


1 Answers

Edit: Using dialogflow_v2 it seems you could do something like this to get custom suggestions:

In the Dialogflow console you could add a custom payload to your messages, like this:

{"suggestions": ["Reply 1", "Reply 2"]}

Create a BotSuggestions class:

class BotSuggestions {
  List<String> suggestions = [];

  BotSuggestions(List<dynamic> messages) {
    messages.forEach((message) {
      if (message['payload'] != null) {
        List<dynamic> suggestionList = message['payload']['suggestions'];
        suggestionList.forEach((suggestion) => suggestions.add(suggestion));
      }
    });
  }
}

Then, you could use it like this:

var botSuggestions = BotSuggestions(response.getListMessage());
print(botSuggestions.suggestions);

Here is a complete example of how to use it:

var userMessage = "Hi!!!";
print('User: $userMessage');
response = await dialogflow.detectIntent(userMessage);
var botSuggestions = BotSuggestions(response.getListMessage());
print('Bot: ${response.getMessage()}');
print('Suggestions: ${botSuggestions.suggestions}');

userMessage = botSuggestions.suggestions.first;
print('User: $userMessage');
response = await dialogflow.detectIntent(userMessage);
botSuggestions = BotSuggestions(response.getListMessage());
print('Bot: ${response.getMessage()}');
print('Suggestions: ${botSuggestions.suggestions}');

And this would be the output:

I/flutter ( 5917): User: Hi!!!

I/flutter ( 5917): Bot: Hi! How are you doing?

I/flutter ( 5917): Suggestions: [Reply 1, Reply 2]

I/flutter ( 5917): User: Reply 1

I/flutter ( 5917): Bot: Sorry, what was that?

I/flutter ( 5917): Suggestions: []


I asked about this in the package repository to see if there's another way to do it, here you can follow the thread: How to get suggestions in v2?.

like image 67
Pablo Barrera Avatar answered Sep 29 '22 21:09

Pablo Barrera