Skip to main content

Overview

AI Moderation in the CometChat SDK helps ensure that your chat application remains safe and compliant by automatically reviewing messages for inappropriate content. This feature leverages AI to moderate messages in real-time, reducing manual intervention and improving user experience.
For a broader understanding of moderation features, configuring rules, and managing flagged messages, see the Moderation Overview.

Prerequisites

Before using AI Moderation, ensure the following:
  1. Moderation is enabled for your app in the CometChat Dashboard
  2. Moderation rules are configured under Moderation > Rules
  3. You’re using CometChat SDK version that supports moderation

How It Works

StepDescription
1. Send MessageApp sends a text, image, or video message
2. Pending StatusMessage is sent with pending moderation status
3. AI ProcessingModeration service analyzes the content
4. Result EventonMessageModerated event fires with final status

Supported Message Types

Moderation is triggered only for the following message types:
Message TypeModeratedNotes
Text MessagesContent analyzed for inappropriate text
Image MessagesImages scanned for unsafe content
Video MessagesVideos analyzed for prohibited content
Custom MessagesNot subject to AI moderation
Action MessagesNot subject to AI moderation

Moderation Status

The getModerationStatus() method returns one of the following string values:
StatusValueDescription
Pending"pending"Message is being processed by moderation
Approved"approved"Message passed moderation and is visible
Disapproved"disapproved"Message violated rules and was blocked

Implementation

Step 1: Send a Message and Check Initial Status

When you send a text, image, or video message, check the initial moderation status:
let textMessage = TextMessage(receiverUid: receiverUID, text: "Hello, how are you?", receiverType: .user)

CometChat.sendTextMessage(message: textMessage) { sentMessage in
    // Check moderation status
    if let message = sentMessage as? TextMessage {
        if message.getModerationStatus() == "pending" {
            print("Message is under moderation review")
            // Show pending indicator in UI
        }
    }
} onError: { error in
    print("Message sending failed: \(error?.errorDescription ?? "")")
}

Step 2: Listen for Moderation Results

Implement the onMessageModerated delegate method to receive moderation results in real-time:
extension YourViewController: CometChatMessageDelegate {
    
    func onMessageModerated(moderatedMessage: BaseMessage) {
        if let message = moderatedMessage as? TextMessage {
            switch message.getModerationStatus() {
            case "approved":
                print("Message \(message.id) approved")
                // Update UI to show message normally
                
            case "disapproved":
                print("Message \(message.id) blocked")
                // Handle blocked message (hide or show warning)
                handleDisapprovedMessage(message)
                
            default:
                break
            }
        } else if let message = moderatedMessage as? MediaMessage {
            switch message.getModerationStatus() {
            case "approved":
                print("Media message \(message.id) approved")
                
            case "disapproved":
                print("Media message \(message.id) blocked")
                handleDisapprovedMessage(message)
                
            default:
                break
            }
        }
    }
}

// Register the delegate
CometChat.addMessageListener("MODERATION_LISTENER", self)

// Don't forget to remove the listener when done
// CometChat.removeMessageListener("MODERATION_LISTENER")

Step 3: Handle Disapproved Messages

When a message is disapproved, handle it appropriately in your UI:
func handleDisapprovedMessage(_ message: BaseMessage) {
    let messageId = message.id
    
    // Option 1: Hide the message completely
    hideMessageFromUI(messageId)
    
    // Option 2: Show a placeholder message
    showBlockedPlaceholder(messageId, text: "This message was blocked by moderation")
    
    // Option 3: Notify the sender (if it's their message)
    if message.sender?.uid == currentUserUID {
        showNotification("Your message was blocked due to policy violation")
    }
}