PubNub Logo Docs
Support Contact Sales Login Try Our APIs

›MODERATION

Collapse all
Dark mode

Back to Home

Overview

  • In-App Chat

Chat Components

  • Overview
  • REACT

    • React Components

    ANDROID

    • Getting Started
    • UI Components
    • Data Components
    • Chat Provider

    IOS

    • Getting Started
    • UI Components
    • Data Components
    • Chat Provider

SDKs

  • Overview
  • USERS

    • Setup
    • Metadata
    • Permissions
    • Presence
    • Mentions

    CHANNELS

    • Types and Names
    • Metadata
    • Subscriptions
    • Memberships

    MESSAGES

    • Sending Messages
    • Message Storage
    • Unread Counts
    • File Upload
    • Typing Indicators
    • Read Receipts
    • Emoji Reactions
    • Update Messages
    • Delete Messages
    • Message Webhooks

    PUSH NOTIFICATIONS

    • Overview

    MODERATION

    • Profanity Filters
    • Flag Messages
    • Ban Users
    • Mute Users
    • Spam Prevention

    INTEGRATIONS

    • Overview
    • Content Moderation
    • Image Moderation
    • Language Translation
    • Chatbots
    • GIFs
    • Stickers

Moderation Dashboard

  • Overview
  • Getting Started
  • FEATURES

    • Automatic Text Moderation
    • Automatic Image Moderation
    • Manual Message Moderation
    • Manual User Moderation
    • User Management
    • Channel Management
  • Required Configuration

Debug Console
Network Status

Filter profanity from messages

Moderation Dashboard

In addition to the moderation functionality supported by the SDKs, PubNub offers the open-source Moderation Dashboard that is an interactive UI you can use with your chat app to moderate users and messages.

The PubNub Profanity Filter function checks the content of a published message against a dictionary of objectionable words. You can either replace a portion of the text or completely block the message.

Setting up the PubNub Function

  1. Create a new function. Go to your Admin Portal and create a new module, and then create a new Before Publish function. Set up the function to trigger on a specific set of channels (such as chat.*), or on all channels using wildcards (*).

  2. Copy the function code from the gist. Make changes as necessary to the dictionary, and configure the function to either replace the swear words, or block the entire message from being published on the channel.

export default (request) => {
    if (request.channels[0].endsWith('-pnpres') || request.channels[0].startsWith("blocks-output") ) {
        return request.ok();
    }
// The code in the linked gist has the real list of
// profanity to be blocked. Don't use this code as-is!
var badWords =new RegExp(/.*\b(hello|world)\b.*/,"i");

    //Option 1 - Replace profanity text with * and allow the publish
    if(request.message && request.message.text && badWords.test(request.message.text)){
        var newString = request.message.text;
        newString = newString.replace(badWords, "***");
        request.message.text = newString;
        return request.ok(); // Return a promise when you're done
    }

    //Option 2 - Block message and return a publish error if the text includes profanity
    /*if(badWords.test(request["message"]["text"])) {
        console.log('moderated message: ' + request["message"]["text"]);
        return request.abort("moderated message")
        return request.ok();
    }*/

    return request.ok(); // Return a promise when you're done
};
  1. Click Start module on the right to start the function, and test it using the Test Payload field and Publish button on the left.

Testing the Function

Input: Publish a message on any channel with a text field included.

{
    "text": "profanity message here"
}

Output: If profanity is present in the original message text, the output message will have the profanity filtered.

{
    "text": "**** message here"
}
←OverviewFlag Messages→
  • Setting up the PubNub Function
  • Testing the Function
© PubNub Inc. - Privacy Policy