dialog flow
dialog flow
build your integrations with dialogflow easier and faster.
dialogflowtter is a package that helps you to build integrations with dialogflow easier and faster.
- authenticate with google auth json
- get an authenticated http client so you can talk with your dialogflow agent
- get access to all dialogflow responses and models easily
- inspect what values a dialogflow response could have
why dialogflowtter
dialog flow
the problem with the current dialogflow integration packages that are available in pub is that they’re:
- completely abandoned
- not well documented
- lack functionality
- lack flexibility
this one is intended to solve those problems and add more features that i’ve seen the users want. check the to-do section for more info on that.
platform support
dialog flow
this package is fully supported in android, ios and web.
we have plans on testing and adding support for windows, linux and macos as this platforms mature in the flutter sdk.
installation
dialog flow
- add the package to your flutter dependencies in you
pubspec.yaml
:dependencies: dialog_flowtter: ^0.3.1
- make sure you add your dialog_flow_auth.json to the
pubspec.yaml
assets:flutter: uses-material-design: true assets: - assets/dialog_flow_auth.json
- add your dialogflow auth json to the
assets
folder and rename it todialog_flow_auth.json
you can change the name and path of your json later in the code. just make sure to use the same one in the
pubspec.yaml
- get the packages from:
flutter packages get
get your keys
dialog flow
refer to create a service account and download the private key file
how to use
dialog flow
retrieve your keys
you can set your dialogflow authentication keys in various ways.
- from an in-memory json
dialogauthcredentials credentials = dialogauthcredentials.fromjson(json);
- from a json file
dialogauthcredentials credentials = await dialogauthcredentials.fromfile(path);
this method is asynchronous!
- from the network
dialogauthcredentials credentials = await dialogauthcredentials.fromnetwork(url);
this method is asynchronous!
then, pass your credentials to you dialogflowtter
class
dialogflowtter instance = dialogflowtter(
credentials: credentials,
);
you can also use the shorthand expression of these methods when instanciating the
dialogflowtter
class
dialogflowtter jsoninstance = dialogflowtter.fromjson(json);
//! async
dialogflowtter fileinstance = await dialogflowtter.fromfile(path);
//! async
dialogflowtter networkinstance = await dialogflowtter.fromnetwork(url);
detect intent
dialog flow
one of the core features of dialogflow is to detect what a person is trying to say. you can do that by detecting an intent that you have defined in your dialogflow console
- create an instance of
dialogflowtter
and set thesessionid
that will be used to identify the current conversation of the user with dialogflow.
it’s highly recommended that you use a different
sessionid
for every conversation that the user establishes with the assistant
final dialogflowtter dialogflowtter = dialogflowtter(
credentials: credentials,
sessionid: "your_session_id_here",
);
- create a
queryinput
where you can specify what data you want to send to dialogflow.
final queryinput queryinput = queryinput(
text: textinput(
text: "hi. how are you?",
languagecode: "en",
),
);
- send your input to dialogflow through the
detectintent
function.
detectintentresponse response = await dialogflowtter.detectintent(
queryinput: queryinput,
);
you can check the code for more info on what info you can send and receive
get the info from the intent
dialog flow
you can access the info returned by dialogflow from the detectintentresponse
that the detectintent
function returns.
get the text from the response
dialog flow
detectintentresponse response = await dialogflowtter.detectintent(
queryinput: queryinput(text: textinput(text: "hi")),
);
string? textresponse = response.text;
print(textresponse); // hi, how may i help you?
response.text returns null if there’s no text returned by dialogflow or if the first message returned it’s not of type messagetype.text
get the message from the response
see message for more info on what the model properties can be
also, check this issue to see how to create and use rich responses like cards and carousels
detectintentresponse response = await dialogflowtter.detectintent(
queryinput: queryinput(text: textinput(text: "hi")),
);
message? messageresponse = response.message;
get audio from the response
- set the audio configuration in the
detectintent
function
detectintentresponse response = await dialogflowtter.detectintent(
queryinput: queryinput(text: textinput(text: "hi")),
// you can set your own configuration with the outputaudioconfig class
audioconfig: outputaudioconfig(),
);
- retrieve the audio from the response
string? audiobase64 = response.outputaudio;
uint8list? audiobytes = response.outputaudiobytes;
- play the audio response with your favorite plugin!
check soundpool for playing the audio from memory
get the response type of the message
messagetype? messagetype = response.message.type;
print(messagetype); /// messagetype.card
response.message returns null if there’s no messages returned by dialogflow
be sure to dispose the instance when you’re done using it
dialogflowtter.dispose();
change the project id
you can change the project id that dialogflowtter
will use to find your intents in dialogflow.
- create an instance of
dialogflowtter
final dialogflowtter dialogflowtter = dialogflowtter(
credentials: credentials,
);
- change the
projectid
prop of the instance;
dialogflowtter.projectid = "deimos-apps-0905";
- pro tip. you can do the exact same thing as above with the special dart’s cascade notation.
final dialogflowtter dialogflowtter = dialogflowtter( credentials: credentials, )..projectid = "deimos-apps-0905";
make authenticated http requests to your dialogflow project
you can access the authenticated http client generated by the package by calling the client
attribute in your instance.
keep in mind that this can become null
if you have disposed your instance before.
create your own authenticated http client
you can get an authenticated, auto refreshing http client with your custom json data if you call the static function
final credentials = dialogauthcredentials.fromjson(yourjson);
final client = dialogflowtter.getclient(credentials)
keep in mind that this only authenticates with json provided by google.
check googleapis_auth for more info.
further considerations
every time you instanciate dialogflowtter
, the class creates an authenticated http client, with the credentials obtained from the dialogflow auth json. be sure to save this instance and reuse it to avoid memory leaks
memory leaks
make sure to dispose
your dialogflowtter
instance whenever you’re done using it. this makes sure to close the authenticated http client and all its streamsubscription
s to avoid memory leaks.
too many models
we have coded almost every dialog flow model that you may need to use when implementing this package so you don’t have to work with annoying map<string, dynamic>
objects. feel free to ask for any model that is missing to be added to the package.
the models that were not coded are included as annoying
map<string, dynamic>
and are tagged with the//? create model if necessary
.
to-do
- [x] add support for null safety
- [x] add support for cards, images, etc.
- [x] memory, file and remote auth json
- [ ] secure dialogflow auth json
- [ ] support audio queries
- [ ] add a catalog of supported languages
- [ ] add direct access to common used attributes
- [ ] support use of custom http client
starware
dialogflowtter
is starware.
this means you’re free to use the project, as long as you star its github repository.
your appreciation makes us grow and glow up. ⭐
Comments are closed.