Skip to main content

Profanity filter callback

By default, the message sender is not notified when a message is blocked by moderation. To notify senders when their messages are blocked due to moderation rules, enable the profanity filter callback service.

Messages may be blocked in the following scenarios:

  • The text message content matches a built-in profanity word, preventing delivery to the recipient.
  • The text message content matches a custom profanity word (block rule), preventing delivery to the recipient.
  • A pre-messaging webhook rule blocks the message before delivery.

Set up the blocked message listener

Use NCEngine.addMessageHandler to register a message handler that listens for message block events. When a sent message is blocked, the SDK triggers the onMessageBlocked callback in MessageHandler.

kotlin
NCEngine.addMessageHandler("BLOCK_HANDLER", object : MessageHandler {
override fun onMessageBlocked(event: MessageBlockedEvent) {
val info = event.info
println("Message blocked: ${info.messageId}")
println("Channel: ${info.channelIdentifier}")
println("Block reason: ${info.blockType}")
}
})

BlockedMessageInfo properties

BlockedMessageInfo contains information about the blocked message:

PropertyTypeDescription
channelIdentifierChannelIdentifierChannel identifier of the blocked message, containing channel type and channel ID.
messageIdStringUnique ID of the blocked message.
blockTypeMessageBlockTypeReason the message was blocked. See MessageBlockType below.
extraString?Additional information about the blocked message.
sourceTypeIntSource type of the blocked message. 0: The original message triggered the block (default). 1: A message expansion triggered the block. 2: An edited message triggered the block.
sourceContentString?JSON string of the blocked message or expansion content. When sourceType is 1, this contains the expansion content. When sourceType is 2, this contains the edited message content. See sourceContent below.

MessageBlockType values

Enum valueDescription
GLOBALGlobal profanity word: the message matched a built-in global profanity word.
CUSTOMCustom profanity word: the message matched a custom profanity word.
THIRD_PARTYBlocked by a pre-messaging webhook rule.

sourceContent values

  • When sourceType is 0: sourceContent is empty.
  • When sourceType is 1: sourceContent is the message expansion content, e.g., {"mid":"xxx-xxx-xxx-xxx","put":{"key":"profanity word"}}. mid is the notification message ID.
  • When sourceType is 2: sourceContent is the edited message content, e.g., {"content":"text containing profanity"}.

Retrieve the original blocked message

After a message is blocked, use BaseChannel.getMessageById to retrieve the original message and update its send status:

kotlin
NCEngine.addMessageHandler("BLOCK_HANDLER", object : MessageHandler {
override fun onMessageBlocked(event: MessageBlockedEvent) {
val info = event.info
BaseChannel.getMessageById(GetMessageByIdParams(messageId = info.messageId)) { message, error ->
if (error == null && message != null) {
message.sentStatus = SentStatus.FAILED
// Update UI to show failed status
}
}
}
})