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
- Java
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}")
}
})
Java
NCEngine.addMessageHandler("BLOCK_HANDLER", new MessageHandler() {
@Override
public void onMessageBlocked(@NonNull MessageBlockedEvent event) {
BlockedMessageInfo info = event.getInfo();
Log.d("Block", "Message blocked: " + info.getMessageId());
Log.d("Block", "Channel: " + info.getChannelIdentifier());
Log.d("Block", "Block reason: " + info.getBlockType());
}
});
BlockedMessageInfo properties
BlockedMessageInfo contains information about the blocked message:
| Property | Type | Description |
|---|---|---|
channelIdentifier | ChannelIdentifier | Channel identifier of the blocked message, containing channel type and channel ID. |
messageId | String | Unique ID of the blocked message. |
blockType | MessageBlockType | Reason the message was blocked. See MessageBlockType below. |
extra | String? | Additional information about the blocked message. |
sourceType | Int | Source 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. |
sourceContent | String? | 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 value | Description |
|---|---|
GLOBAL | Global profanity word: the message matched a built-in global profanity word. |
CUSTOM | Custom profanity word: the message matched a custom profanity word. |
THIRD_PARTY | Blocked by a pre-messaging webhook rule. |
sourceContent values
- When
sourceTypeis0:sourceContentis empty. - When
sourceTypeis1:sourceContentis the message expansion content, e.g.,{"mid":"xxx-xxx-xxx-xxx","put":{"key":"profanity word"}}.midis the notification message ID. - When
sourceTypeis2:sourceContentis 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
- Java
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
}
}
}
})
Java
NCEngine.addMessageHandler("BLOCK_HANDLER", new MessageHandler() {
@Override
public void onMessageBlocked(@NonNull MessageBlockedEvent event) {
BlockedMessageInfo info = event.getInfo();
BaseChannel.getMessageById(new GetMessageByIdParams(info.getMessageId()), (message, error) -> {
if (error == null && message != null) {
message.setSentStatus(SentStatus.FAILED);
// Update UI to show failed status
}
return null;
});
}
});