-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg-bot.py
115 lines (94 loc) · 3.46 KB
/
tg-bot.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import asyncio
from dataclasses import dataclass
import datetime
import os
import logging
import telethon
import telethon.sessions
import transformers
import torch
import json
from detoxify import Detoxify
@dataclass
class State:
last_message_at: dict[int, datetime.datetime]
lock: asyncio.Lock
async def amain():
config = json.load(open("settings.json"))
assert torch.cuda.is_available()
toxicity_clf = Detoxify("multilingual", device="cuda")
sentiment_clf = transformers.pipeline(
model="blanchefort/rubert-base-cased-sentiment", device="cuda"
)
def negative_predict(x) -> float:
return next(
row["score"]
for row in sentiment_clf(x, top_k=None)
if row["label"] == "NEGATIVE"
)
state = State(last_message_at={}, lock=asyncio.Lock())
async with telethon.TelegramClient(
telethon.sessions.StringSession(os.environ["TELEGRAM_SESSION_TELETHON"]),
int(os.environ["TELEGRAM_API_ID"]),
os.environ["TELEGRAM_API_HASH"],
sequential_updates=True, # no GPU races
) as tg:
@tg.on(
telethon.events.NewMessage(chats=config["bot"]["toxicity"]["monitor-chats"])
)
async def set_diamonds(event):
toxicity = toxicity_clf.predict(event.raw_text)["toxicity"]
negativity = negative_predict(event.raw_text)
reactions = []
if toxicity >= 0.60:
reactions.append(
telethon.types.ReactionCustomEmoji(
5406748567303900401
) # https://t.me/addemoji/BeBrilliant
)
if toxicity >= 0.8:
reactions.append(
telethon.types.ReactionCustomEmoji(
5407118673225730467
) # https://t.me/addemoji/BeBrilliant
)
if toxicity >= 0.98:
reactions.append(
telethon.types.ReactionCustomEmoji(
5406772623415720314
) # https://t.me/addemoji/BeBrilliant
)
# if negativity >= 0.9:
# reactions.append(telethon.types.ReactionEmoji("😢"))
if reactions:
await tg(
telethon.tl.functions.messages.SendReactionRequest(
peer=event.peer_id,
msg_id=event.id,
reaction=reactions[:3],
)
)
@tg.on(
telethon.events.NewMessage(chats=config["bot"]["slowmode"]["monitor-chats"])
)
async def slow_mode(event):
now = datetime.datetime.now()
# bounce the message back to the sender
if event.sender_id in state.last_message_at and now - state.last_message_at[
event.sender_id
] < datetime.timedelta(seconds=30):
await tg.forward_messages(event.sender_id, event.id, event.chat_id)
await tg.delete_messages(event.chat_id, event.id)
return
# update the last message time
async with state.lock:
state.last_message_at[event.sender_id] = now
await asyncio.Event().wait()
def main():
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s",
level=logging.WARNING,
)
asyncio.run(amain())
if __name__ == "__main__":
main()