This repository was archived by the owner on May 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbots_base.py
More file actions
65 lines (49 loc) · 1.73 KB
/
bots_base.py
File metadata and controls
65 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import logging
import requests
API_URL = "http://api.hateflow.de/"
def warning_text(properties: list) -> str:
"""
Generate a warning message for the given properties.
"""
assert properties
if "toxic" in properties and "severe_toxic" in properties:
properties.remove("toxic")
names = {
'identity_hate': "identity hate",
'insult': "insult",
'obscene': "obscenity",
'severe_toxic': "severe toxicity",
'threat': "threat",
'toxic': "toxicity",
}
properties = [names.get(i) or i for i in properties]
detected_properties = " and ".join(properties[-2:])
if len(properties) > 2:
detected_properties = f"{', '.join(properties[:-2])}, {detected_properties}"
text = f"I detected {detected_properties}.\n\n" \
f"I am a bot developed by HateFlow. This action was performed automatically."
return text
def check_comment(comment) -> (str, dict):
"""Check a comment using the HateFlow API."""
response = requests.get(f"{API_URL}simpleCheck?text={comment}")
if response.status_code != 200:
return f"Connection to {API_URL} failed with status code {response.status_code}.", dict()
else:
data = json.loads(response.text)
return "", data['results']
def process_message(text: str) -> str:
"""Runs every time a message is sent in chat."""
print("---------new comment---------")
print(text)
error, result = check_comment(text)
if error:
logging.error(error)
else:
properties = []
for key, value in result.items():
if value == 1:
properties.append(key)
if properties:
return warning_text(properties)
return ""