Libraries for integration of CX agent into Android app

72 views
Skip to first unread message

Katharina Linden

unread,
Jul 13, 2022, 12:43:46 PM7/13/22
to Dialogflow CX Edition users
Hi everyone! 

I was trying to integrate a Dialogflow CX agent into an Android App by using REST calls to the API. Unfortunately, the libraries are not supported by Android yet and I can't get the authentification to work, so I keep getting a 401 (not athenticated) back from the server. 

Actions tried: 
- Authentification via firebase 
- Authentification with OAuth2 / OpenID 
 
With ES, I was able to use the support libs, in combination with the credentials and I was good to go. Does anyone know, if there are plans for an Android compatible library for CX soon? 


Josh Chestang

unread,
Jul 13, 2022, 1:02:13 PM7/13/22
to Katharina Linden, Dialogflow CX Edition users
Hi Katharina,

Have you established a client to authenticate with? 

It should consist of a credentials json file or Application Default Credentials set on your machine. 

To get the CX agent to work locally I used clientViaServiceAccount which uses a credentials file to authenticate with OAuth 2.0. 

Also, make sure you are using the Dialogflow scope.

Here's an example in Dart because I'm building a Flutter App:

Please note: Best practices says to not pass in credentials in your app like this. You should create a microservice on Google Cloud Compute Engine or similar and then use your credentials on Compute Engine to then authenticate with the Dialogflow API.

I hope this helps!

final String response = await rootBundle
.loadString('assets/chatbot/******************.json');
final data = await json.decode(response);

final accountCredentials = ServiceAccountCredentials.fromJson({
"private_key_id": data["private_key_id"],
"private_key": data["private_key"],
"client_email": data["client_email"],
"client_id": "${data["client_id"]}.apps.googleusercontent.com",
"type": "service_account"
});


List<String> scopes = [
];

try {
final client =
await clientViaServiceAccount(accountCredentials, scopes);
// try {
// Remember to close the client when you are finished with it.
var sessionId = Uuid().v1();
String session =
'projects/******/locations/******/agents/********************************/sessions/$sessionId';
final request = GoogleCloudDialogflowCxV3DetectIntentRequest(
queryInput: GoogleCloudDialogflowCxV3QueryInput(
languageCode: 'en',
text: GoogleCloudDialogflowCxV3TextInput(text: query)));
// create a session if one is not already available - WE GENERATE THE UNIQUE SESSION ID

GoogleCloudDialogflowCxV3DetectIntentResponse response =
await DialogflowApi(client, servicePath: ''
// add the path to the bot here
)
.projects
.locations
.agents
.sessions
.detectIntent(request, session);
print(response);
if (response.queryResult != null) {
// get the custom payload and format it as a Map
List suggestionChips = [];
String action = "";
final payload = GoogleCloudDialogflowCxV3ResponseMessage.fromJson(
response.queryResult!.responseMessages!.asMap())
.payload;

// if (payload.containsKey("suggestionChips") == true) {
if (payload!.entries.isNotEmpty) {
suggestionChips = payload['suggestionChips'] as List;
action = payload['action'] as String;
}

BotResponse intentResponse = BotResponse(
queryResult: response.queryResult,
suggestionChips: suggestionChips,
action: action,
responseId: response.responseId,
timestamp: botResponse!.timestamp,
userImageUrl: botResponse.userImageUrl,
botImageUrl: botResponse.botImageUrl);
AngelinaApp.analytics.logEvent(
name: "response_received_from_dialog_flow",
parameters: {
"fulfillmentText": "${response.queryResult?.responseMessages}"
});
onSend(botResponse: intentResponse);
}
} finally {
client.close();
}

} catch (error) {
print(error);
AngelinaApp.analytics
.logEvent(name: "dialog_flow_error", parameters: {"error": "$error"});
}

--
© 2022 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
 
Email preferences: You received this email because you signed up for the Dialogflow CX Edition Discussion Google Group (dialogflow-cx...@googlegroups.com) to participate in discussions with other members of the Dialogflow community and the Dialogflow Team.
---
You received this message because you are subscribed to the Google Groups "Dialogflow CX Edition users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dialogflow-cx-editi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dialogflow-cx-edition-users/952edef5-b55b-4952-8511-3db81299f3f6n%40googlegroups.com.

Devashish Mamgain

unread,
Jul 14, 2022, 9:58:05 AM7/14/22
to Josh Chestang, Katharina Linden, Dialogflow CX Edition users
There is risk involved in directly adding Dialogflow credentials to the mobile app codebase directly.

What if tomorrow, due to security reasons, you have to generate a new access key? or lets say change the chatbot to another bot?

I would suggest the following approach:
- Host the Dialogflow credentials at the server side. Make Dialogflow API call from the server, and let Mobile App call the server API.

- Flow will be:

Mobile app ------------------------>     Server     ------------------------->    Dialogflow APIs

This way, your web, android, iOS, WhatsApp or any other channel in future, everything will just connect with your common server that have Dialogflow credentials, latest API version connection, etc. And you will have to write code to connect to Dialogflow APIs only once.

We have followed the same for building chatbot SDKs across Android, iOS, Flutter and this approach works seamlessly. 

We have the flexibility to change Dialogflow credential API, upgrade to the latest Dialogflow API whenever there is a new change.



Reply all
Reply to author
Forward
0 new messages