flowMessage method
Implementation
Future<ResponseMessage> flowMessage(String question, String agentId, { bool? isMarkdown, String? threadId, String? sessionId, String? language,}) async {
try {
final response = await http.post(
Uri.parse(_flowUrl),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'userInput': question,
'agentId': agentId,
'sessionId': sessionId ?? _sessionId,
'language':language ?? 'en-US'
}),
);
print('Response status code: ${response.statusCode}');
print('Response body: ${response.body}');
if (response.statusCode == 200) {
final responseJson = jsonDecode(response.body) as Map<String, dynamic>;
final responseAssistantJson = responseJson['assistant'] as Map<String, dynamic>;
// final responseHaivaJson = responseJson['haivaMessage'] as Map<String , dynamic>;
print('Response assistant JSON: $responseAssistantJson');
// final assistantValue = responseJson['assistant'] as Map<String, dynamic>;
print("responseAssistantJson.isNotEmpty ${responseAssistantJson.isNotEmpty}");
if (responseAssistantJson.isEmpty) {
final newSessionId = responseJson['sessionId'] as String?;
final haivaMessage = responseJson['haivaMessage'] as Map<String, dynamic>?;
final messageText = haivaMessage?['response'] as String?;
// Update the global sessionId
if (newSessionId != null) {
_sessionId = newSessionId;
}
return ResponseMessage(
MessageType.bot,
DateTime.now(),
text: haivaMessage!.containsKey('error')
? haivaMessage['error']
: haivaMessage.containsKey('response') && haivaMessage['response']?.isNotEmpty == true
? messageText
: haivaMessage.containsKey('prompt') ? null: null,
customComponent: haivaMessage, // Assuming haivaMessage contains the data for customComponent
sessionId: _sessionId, // Use the updated global sessionId
responseTypesAgent: [], // Update based on your response format
wholeResponsePayload: responseJson,
haivaMessage: haivaMessage,
statusCode:response.statusCode
);
}
else if (responseAssistantJson.isNotEmpty)
{
final newSessionId = responseJson['sessionId'] as String;
final responseTypes = List<String>.from(responseAssistantJson['response_type']);
final messageText = responseAssistantJson['response']['message'] as String? ?? '';
final errorText = responseAssistantJson['response']['error'] as String? ?? '';
final chartData = responseAssistantJson['response']['chartData'] as Map<String, dynamic>?;
final labelConfig = responseAssistantJson['label']?['config'] as Map<String, dynamic>?;
print('Label Config: $labelConfig');
_sessionId = newSessionId;
return ResponseMessage(
MessageType.bot,
DateTime.now(),
text: responseTypes.contains('message') ? messageText : responseTypes.contains('error') ? errorText : null,
customComponent: responseTypes.contains('chart')
? chartData
: labelConfig,
sessionId: newSessionId,
responseTypesAgent: responseTypes,
wholeResponsePayload: responseAssistantJson,
statusCode:response.statusCode
);
}
else {
print('Error: Response JSON does not contain expected keys.');
return ResponseMessage(
MessageType.bot,
DateTime.now(),
text: 'Sorry, an error occurred. Please try again later.',
);
}
}
else {
print('Error: ${response.statusCode} - ${response.body}');
return ResponseMessage(
MessageType.bot,
DateTime.now(),
text: 'Sorry, an error occurred. Please try again later.',
);
}
} catch (e, stackTrace) {
print('Exception: $e');
print('StackTrace: $stackTrace');
return ResponseMessage(
MessageType.bot,
DateTime.now(),
text: 'Sorry, an error occurred. Please try again later.',
);
}
}