Skip to main content

Introduction to the chat filter

Last updated on March 3, 2025

Overview

The chat filter in AccelByte Gaming Services (AGS) can be customized via Extend. In this guide, we will present the contract of the customization with example code for how to create a custom chat filter that takes chat messages and returns them with certain words filtered. We do this using the FilterBulk function in the contract as shown below.

service FilterService {
...
rpc FilterBulk(ChatMessageBulk) returns (MessageBatchResult);
}

FilterBulk

FilterBulk function will be called when there are chats from players. A single invocation of this function may contain more than one chat message. In this example, we will loop through all chat messages received and, if is_profane detects certain words, we will do_censor it and return the results back.

async def FilterBulk(self, request, context):
self.log_payload(f'{self.FilterBulk.__name__} request: %s', request)
data = [self.do_censor(message) for message in request.messages]
response = MessageBatchResult(data=data)
self.log_payload(f'{self.FilterBulk.__name__} response: %s', response)
return response


def do_censor(self, chat_message: ChatMessage) -> MessageResult:
action: MessageResult.Action = MessageResult.Action.PASS
classification: List[MessageResult.Classification] = []
censored_words: List[str] = []
message: str = chat_message.message
reference_id: str = uuid4().hex
censored_message = message
if self.filter.is_profane(message):
censored_message = self.filter.censor(message)
# action
action = MessageResult.Action.CENSORED
# classification
classification.append(MessageResult.Classification.OTHER)
# censored_words
words = message.split()
cwords = censored_message.split()
if len(words) == len(cwords):
censored_words = [words[i] for i in range(len(words)) if words[i] != cwords[i]]
return MessageResult(
id=chat_message.id,
timestamp=chat_message.timestamp,
action=action,
classification=classification,
censoredWords=censored_words,
message=censored_message,
referenceId=reference_id,
)

On this page