Introduction to the chat filter
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
- Go
- Python
The 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 IsProfane(...)
detects certain words, we will Censor(...)
it and return the results back.
Here is a code example:
func (s FilterServiceServer) FilterBulk(ctx context.Context, chatMessages *pb.ChatMessageBulk) (*pb.MessageBatchResult, error) {
result := &pb.MessageBatchResult{
Data: make([]*pb.MessageResult, len(chatMessages.GetMessages())),
}
for index, chatMessage := range chatMessages.GetMessages() {
action := pb.MessageResult_PASS
timestamp := chatMessage.GetTimestamp()
message := chatMessage.GetMessage()
if s.detector.IsProfane(chatMessage.GetMessage()) {
action = pb.MessageResult_CENSORED
timestamp = time.Now().Unix()
message = s.detector.Censor(message)
}
result.GetData()[index] = &pb.MessageResult{
Id: chatMessage.GetId(),
Timestamp: timestamp,
Action: action,
Classification: []pb.MessageResult_Classification{},
Message: message,
}
}
return result, nil
}
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,
)