askBot static method

Future<void> askBot(
  1. String receiverId,
  2. String receiverType,
  3. String botId,
  4. String question, {
  5. Map? configuration,
  6. required dynamic onSuccess(
    1. String conversationSummary
    ),
  7. required dynamic onError(
    1. CometChatException e
    ),
})

Ask an AI bot a question in the context of a conversation.

Migration Note: Migrated from platform channels to native Dart implementation. Uses AiRepository for AI bot interactions. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.askBot(String receiverId, String receiverType, String botId, String question, CallbackListener<String>)

Implementation

static Future<void> askBot(
    String receiverId, String receiverType, String botId, String question,
    {Map? configuration,
    required Function(String conversationSummary) onSuccess,
    required Function(CometChatException e) onError}) async {
  try {
    // Validate parameters
    if (receiverId.isEmpty) {
      onError(CometChatException(
          ErrorCode.errorInvalidReceiverId,
          ErrorMessage.errorMessageInvalidReceiverId,
          ErrorMessage.errorMessageInvalidReceiverId));
      return;
    }
    if (receiverType.isEmpty) {
      onError(CometChatException(
          ErrorCode.errorInvalidReceiverType,
          ErrorMessage.errorMessageInvalidReceiverType,
          ErrorMessage.errorMessageInvalidReceiverType));
      return;
    }
    if (botId.isEmpty) {
      onError(CometChatException(ErrorCode.errorEmptyBotID,
          ErrorMessage.errorEmptyBotID, ErrorMessage.errorEmptyBotID));
      return;
    }
    if (question.isEmpty) {
      onError(CometChatException(
          ErrorCode.errorEmptyAskBotQuestion,
          ErrorMessage.errorEmptyAskBotQuestion,
          ErrorMessage.errorEmptyAskBotQuestion));
      return;
    }

    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart AI repository
    final answer =
        await sdk.ai.askBot(receiverId, receiverType, botId, question);

    // Call success callback
    onSuccess(answer);
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    onError(cometChatEx);
  } catch (e) {
    onError(CometChatException(
      ErrorCode.errorUnhandledException,
      e.toString(),
      e.toString(),
    ));
  }
}