customComponentHandler method

Future<Map<String, dynamic>> customComponentHandler(
  1. String text
)

Implementation

Future<Map<String, dynamic>> customComponentHandler(String text) async {
  try {
    final response = await http.post(
      Uri.parse(_baseUrl),
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode({'message': text}),
    );

    if (response.statusCode == 200) {
      print('Response from server: ${response.body}');
      Map<String, dynamic> responseJson = jsonDecode(response.body);
      List<dynamic> responseMessages = responseJson['responseMessages'];
      // Iterate through the response messages to find and return the payload
      for (var message in responseMessages) {
        if (message.containsKey('payload')) {
          return message['payload'];
        }
      }
      // If no payload is found, return an empty map or handle as needed
      return {};
    } else {
      print('Error: ${response.statusCode} - ${response.body}');
      return {'error': 'error please try again later'};
    }
  } catch (e) {
    print('Exception: $e');
    return {'error': "error please try again later"};
  }
}