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"],
"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"});
}