sendCustomInteractiveMessage static method

Future<InteractiveMessage?> sendCustomInteractiveMessage(
  1. InteractiveMessage message, {
  2. dynamic onSuccess(
    1. InteractiveMessage
    )?,
  3. dynamic onError(
    1. CometChatException
    )?,
})

sendCustomInteractiveMessage can be used to send a custom interactive message

Implementation

static Future<InteractiveMessage?> sendCustomInteractiveMessage(
    InteractiveMessage message, {
      dynamic Function(InteractiveMessage)? onSuccess,
      dynamic Function(CometChatException)? onError,
    }) async {
  if (message.parentMessageId == -1) {
    message.parentMessageId = 0;
  }

  message.sender ??= loggedInUser;
  if (message.muid.trim().isEmpty) {
    message.muid = DateTime.now().microsecondsSinceEpoch.toString();
  }

  CometChatMessageEvents.ccMessageSent(message, MessageStatus.inProgress);

  InteractiveMessage? result = await CometChat.sendInteractiveMessage(message,
      onSuccess: (InteractiveMessage sentMessage) {
        //executing the custom onSuccess handler
        if (onSuccess != null) {
          try {
            onSuccess(sentMessage);
          } catch (e) {
            if (kDebugMode) {
              debugPrint(
                  "message sent successfully but failed to execute onSuccess callback");
            }
          }
        }
        //the ccMessageSent event is emitted to update the message receipt shown
        //in the footer of the message bubble from in progress to sent
        CometChatMessageEvents.ccMessageSent(sentMessage, MessageStatus.sent);
      }, onError: (error) {
        //executing the custom onError handler
        if (onError != null) {
          try {
            onError(error);
          } catch (e) {
            if (kDebugMode) {
              debugPrint(
                  "message could not be sent and failed to execute onError callback");
            }
          }
        }
        //a error property is added to the metadata of the message
        //because of which a error message receipt will be shown in the
        //footer of the message bubble in the message list
        if (message.metadata != null) {
          message.metadata!["error"] = error;
        } else {
          message.metadata = {"error": error};
        }
        CometChatMessageEvents.ccMessageSent(message, MessageStatus.error);
      });
  return result;
}