sendMessage method

Future<List<ResponseMessage>> sendMessage(
  1. String text,
  2. String sessionId, {
  3. Map<String, dynamic>? payload,
})

Implementation

Future<List<ResponseMessage>> sendMessage(String text,String sessionId, {Map<String, dynamic>? payload}) async {

  try {
    final response = await http.post(
      Uri.parse(_baseUrl),
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode({'message': text ,'session_id': sessionId,'payload': payload}),
    );

    if (response.statusCode == 200) {
      print('Response from server: ${response.body} with sessionId: $sessionId)');
      Map<String, dynamic> responseJson = jsonDecode(response.body);
      List<dynamic> responseMessages = responseJson['responseMessages'];
      print("Parsed response messages: $responseMessages");

      return responseMessages.map((json) => ResponseMessage.fromJson(json)).toList();
    } else {

      return [
        ResponseMessage(
          MessageType.bot, // Provide the type
          DateTime.now(), // Provide the timestamp
       //   text: 'Error: ${response.statusCode} - ${response.body}',
          text:'An error occurred please try again later',
        )
      ];

    }
  } catch (e) {
    print('Exception: $e');
    // Return a list with a single ResponseMessage object
    // indicating the exception message.
    return [
      ResponseMessage(
        MessageType.bot, // Provide the type
        DateTime.now(), // Provide the timestamp
        text: 'error please try again later', // Provide optional named arguments if needed
      )

    ];
  }
}