sendMessage static method

Future<void> sendMessage({
  1. required MessageParams messageParams,
  2. required dynamic flyCallback(
    1. FlyResponse response
    ),
})

Sends a message using the Mirrorfly messaging platform.

This method sends a message using the provided messageParams and asynchronously invokes the flyCallback function with the result.

The messageParams parameter contains the details of the message to be sent, using MessageParams class.

The flyCallback parameter is a function that will be called with a FlyResponse object containing information about the success or failure of the message sending operation.

Example usage:

await Mirrorfly.sendMessage(
  messageParams: messageParams,
  flyCallback: (FlyResponse response) {
    if (response.isSuccess) {
      print('Message sent successfully');
    } else {
      print('Failed to send message: ${response.errorMessage}');
    }
  },
);

Implementation

static Future<void> sendMessage({
  required MessageParams messageParams,
  required Function(FlyResponse response) flyCallback,
}) async {
  String messageText = '';
  if (messageParams.messageType == MessageType.text) {
    messageText = messageParams.textMessageParams?.messageText ?? '';
  } else if (messageParams.messageType == MessageType.image ||
      messageParams.messageType == MessageType.video) {
    messageText = messageParams.fileMessageParams?.caption ?? '';
  } else if (messageParams.messageType == MessageType.contact) {
    messageText = (messageParams.contactMessageParams?.name ?? '') +
        (messageParams.contactMessageParams?.numbers.join(' ') ?? '');
  }
  // Perform input validation if enabled
  if (messageParams.messageSecurityMode == MessageSecurityMode.enabled &&
      messageText.isNotEmpty &&
      TextSafety.isUnsafeText(messageText)) {
    return flyCallback(FlyResponse(
        false,
        '',
        'Invalid input: HTML tags and scripts are not allowed',
        FlyException("500",
            "Invalid input: HTML tags and scripts are not allowed", "")));
  }
  // Send the message if no XSS detected
  return FlyChatFlutterPlatform.instance.sendMessage(
    messageParams: messageParams,
    callback: flyCallback,
  );
}