Skip to content

Detect text abusing

The Text Abusing Detection API is an API that detects and verifies abuse (prohibited words, spam advertisements) in text.

Preparation

Check if you have access to the Hive Console > AI Services > Abuse Detection > Chat Abusing Detection menu, and if not, refer to the Hive Console Permission Administration Guide to obtain menu access rights. Register a new project in Chat Abusing Detection.

Determining if text is abusive

After sending the text, it determines whether the text is spam advertising or contains prohibited words, and then returns the results.

Request URL

LIVE URL https://tads.withhive.com/cads2/send-text-data
Sandbox URL https://sandbox-tads.withhive.com/cads2/send-text-data
HTTP Method POST
Content-Type application/json

Request body

Field Name Description Type Required
project_key Project Key String Y
text_log Text information Object Y

text_log

Field Name Description Type Required
text_array Array of objects containing the text and the time the text was sent Array(Object) Y
text_count Number of objects in text_array Integer Y

Object in text_array

Field Name Description Type Required
time_stamp Time the text was sent (UTC-based ISO format time string, example: 2024-01-30T10:35:49.95457+09:00) String Y
text_info A string encrypted with AES256 using the encryption key obtained from the Hive Console from a JSON object. The maximum size is 5KB. For more details on the encryption method, refer to the example code. String Y

text_info JSON Object

Create a JSON object according to the format below, and then encrypt it with AES256 to form the text_info string.

Field Name Description Type Required
user_id User's unique ID number String Y
lang_code Language code (ISO 639-1)
* Korean: ko
* English: en
* Japanese: ja
* Simplified Chinese: zh-hans
* Traditional Chinese: zh-hant
* Spanish: es
* Russian: ru
* German: de
* French: fr
* Portuguese: pt
* Italian: it
* Thai: th
* Indonesian: id
* Turkish: tr
* Malaysian: ms
* Vietnamese: vi
String Y
text Text to check for abusing String Y
text_type If the text is chat text, it falls into one of the 3 chat formats below.
* "chat_party": Party chat
* "chat_guild": Guild chat
* "chat_whisper": Whisper
String N
room_num If the text is chat text, this is the chat room number. String N
channel_info If the text is chat text, this is the chat channel information. String N

Response

Field Name Description Type
result_code Response code String
result_text Description of the response code String
result_array Array containing detection results Array(Object)

result_array

Field Name Description Type
tads_result Detection result Object
text_result Result of applying masking to the original text based on the original text and the detection result Object

tads_result

Field Name Description Type
forbidden_yn Whether forbidden words were detected (0: No forbidden words found in the text, 1: Forbidden words found in the text) String
spam_yn Whether the text is detected as spam (0: Not detected as spam, 1: Detected as spam) String
aug_yn Whether the text is processed in a batch (0: Not batch processed, 1: Batch processed) String
long_yn Whether the text is a long string, which means that if it is over 5,000 characters the detection algorithm cannot be run on the the text (0: Less than 5,000 characters, 1: More than 5,000 characters) String
text_result
Field Name Description Type
text The text to check for abusing String
masking_text The text with masking applied (text will be identical if no masking settings have been made in the Hive Console or if tads_result.forbidden_yn value is 0) String

Response code

Code Text Remarks
200 "success" Success
500 "Incorrect request" There is an error in the request. Please check if the Request URL and Request Body are correct.
501 "Incorrect Data Count" The count of the passed text does not match. The value of text_count and the length of text_array must match.
502 "Incorrect Secret Key" The secret key value is incorrect. Please check the project secret key in Project Management.
503 "Unregistered ProjectKey" The project key value is incorrect. Please check the project key of the project in Project Management.
506 "System Error" This is an internal error, such as a network error. Please call the API again after some time.
507 "The size of message is too large" The size of the text is too large. The maximum size of text_array is 5KB. Please reduce the size of the message.

Request example

import json
import base64
import requests
from datetime import datetime
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

def encrypt(text, user_id, lang_code, secret_key, channel_info=None, room_num=None, text_type=None) -> str: ## the codes for encryption 
    aes = AES.new(secret_key, AES.MODE_ECB)
    jsonData =  {"text_type": text_type, ## Use it only when it is required
                "room_num" : room_num, ## Use it only when it is required
                "user_id": user_id,
                "channel_info" : channel_info, ## Use it only when it is required
                "lang_code": lang_code,
                "text": text}
    rawJsonStr = json.dumps(jsonData)
    paddedStr = pad(rawJsonStr.encode("utf-8"),AES.block_size)
    encrypted_data = aes.encrypt(paddedStr)
    encrypted_data_b64 = base64.b64encode(encrypted_data).decode("utf-8")
    return encrypted_data_b64

project_key = <PROJECT_KEY>
secret_key = <SECRET_KEY>
now = datetime.now().astimezone().isoformat()
lang_code = "ko"
user_id = "432414"
channel_info = "4342314" ## Use it only when it is required
room_num = "12312" ## Use it only when it is required
text_type = "chat_whisper" ## Use it only when it is required
url = "https://tads.withhive.com/cads2/send-text-data"

payload =  {"project_key": project_key,
                      "text_log": {
                              "text_count": 2, ## should match the number of elements of text_array
                              "text_array": [
                                            {"time_stamp": now,
                                            "text_info": encrypt(text=str("test1"), 
                                                            user_id=user_id,
                                                            channel_info=channel_info, ## Use it only when it is required
                                                            room_num=room_num, ## Use it only when it is required
                                                            text_type=text_type, ## Use it only when it is required
                                                            lang_code=lang_code,
                                                            secret_key=secret_key)},
                                            {"time_stamp": now,
                                            "text_info": encrypt(text=str("test2"), 
                                                            user_id=user_id,
                                                            channel_info=channel_info, ## Use it only when it is required
                                                            room_num=room_num, ## Use it only when it is required
                                                            text_type=text_type, ## Use it only when it is required
                                                            lang_code=lang_code,
                                                            secret_key=secret_key)}]},}

res = requests.post(url, data=json.dumps(payload), headers={"Content-Type": "application/json"})
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;
import org.jooq.tools.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
public class TadsComponent {

@Autowired
RestTemplate restTemplate;

@Autowired
ObjectMapper objectMapper;


public void sendHttp() throws Exception{
    String projectKey =  <PROJECT_KEY>; // provide the project key
    String secretKey =  <SECRET_KEY>;  // provide the project secret key

    String url = "https://tads.withhive.com/cads2/send-text-data";
    String user_id = "432414";
    String channel_info = "4342314"; // Use it only when it is required
    String room_num = "12312"; // Use it only when it is required
    String text_type = "chat_whisper"; // Use it only when it is required
    String lang_code = "ko";
    String text_1 = "test1";
    String text_2 = "test2";

    DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
    ZonedDateTime now = ZonedDateTime.now();

    Map<String, Object> payload = new HashMap<>();
    payload.put("project_key", projectKey);

    Map<String, Object> textLog = new HashMap<>();
    textLog.put("text_count", 2); // should match the number of elements of text_array

    Map<String, Object> textArray1 = new HashMap<>();
    textArray1.put("time_stamp", now.format(formatter));
    textArray1.put("text_info", encrypt(text_1, user_id, channel_info, room_num, text_type, lang_code, secretKey));


Map<String, Object> textArray2 = new HashMap<>();
   textArray2.put("time_stamp", now.format(formatter));
   textArray2.put("text_info", encrypt(text_2, user_id, channel_info, room_num, text_type, lang_code, secretKey));

    Object[] textArrayObj = {textArray1, textArray2};
    textLog.put("text_array", textArrayObj);
    payload.put("text_log", textLog);
    JsonNode callBodyNode = objectMapper.valueToTree(payload);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json");
    HttpEntity<String> entity = new HttpEntity<>(callBodyNode.toString(), headers);
    ResponseEntity<String> responseEntity =  restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    int responseStatusCode =  responseEntity.getStatusCodeValue();
    String responseBodyString = responseEntity.getBody();
    JsonNode responseBodyNode = objectMapper.readTree(responseBodyString);
    return;
}

public static String encrypt(String text, String UserId, String channelInfo, String roomNum, String textType, String langCode, String secretKey) {
    try {
        Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
        aes.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("room_num", roomNum); // Use it only when it is required
        map.put("channel_info", channelInfo); // Use it only when it is required
        map.put("lang_code", langCode);
        map.put("text", text);
        map.put("text_type", textType); // Use it only when it is required
        map.put("user_id", UserId);
        String encrypt_str = JSONObject.toJSONString(map);
        byte[] paddedData = padString(encrypt_str).getBytes();
        byte[] encryptedData = aes.doFinal(paddedData);
        return Base64.getEncoder().encodeToString(encryptedData);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
        return null;
    }
}
private static String padString(String source) {
    char paddingChar = ' ';
    int size = 16;
    int x = source.length() % size;
    int padLength = size - x;
    StringBuilder paddedString = new StringBuilder(source);
    for (int i = 0; i < padLength; i++) {
        paddedString.append(paddingChar);
    }
    return paddedString.toString();
}} // the codes for encryption

Response example

{
    "result": {
        "result_array": '[
                        {"tads_result":{
                            "forbidden_yn":"0",
                            "spam_yn":"0",
                            "aug_yn":"0",
                            "long_yn":"0"},
                        "text_result":{
                            "text":"test1",
                            "masking_text":"test1"
                        }},
                        {"tads_result":{
                            "forbidden_yn":"0",
                            "spam_yn":"0",
                            "aug_yn":"0",
                            "long_yn":"0"},
                        "text_result":{
                            "text":"test2",
                            "masking_text":"test2"
                        }}
                            ]',
        "result_code": "200",
        "result_text": "success"
    }
}