-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
bot.py
128 lines (110 loc) Β· 4.16 KB
/
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
116
117
118
119
120
121
122
123
124
125
126
127
128
from telethon import TelegramClient, events, Button, types
from decouple import config
from ProfanityDetector import detector
import logging
from telethon.tl.functions.channels import GetParticipantRequest
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.INFO
)
log = logging.getLogger(__name__)
# vars
apiid = 6
apihash = "eb06d4abfb49dc3eeb1aeb98ae0f581e"
BOT_TOKEN = config("BOT_TOKEN", default=None)
log.info("Starting Bot...")
bot = TelegramClient("bot", apiid, apihash).start(bot_token=BOT_TOKEN)
# check admins
async def check_if_admin(message):
result = await bot(
GetParticipantRequest(
channel=message.chat_id,
user_id=message.sender_id,
)
)
p = result.participant
return isinstance(
p, (types.ChannelParticipantCreator, types.ChannelParticipantAdmin)
)
@bot.on(
events.NewMessage(incoming=True, pattern="^/start$", func=lambda e: e.is_private)
)
async def start_msg(event):
sender = await bot.get_entity(event.sender_id)
await event.reply(
f"Hi {sender.first_name}!\nI am a profanity detector bot.\n\nMake me admin in your group with `delete messages` permission and I'll delete messsages containing abuses!",
buttons=[
[Button.inline("Help π", data="helpme")],
[
Button.url(
"Add me to a group β",
url=f"http://t.me/{(await bot.get_me()).username}?startgroup=botstart",
)
],
[
Button.url("π₯ Channel", url="https://t.me/BotzHub"),
Button.url(
"Package π¦", url="https://pypi.org/project/ProfanityDetector/"
),
],
],
)
@bot.on(events.NewMessage(incoming=True, pattern="^/start", func=lambda e: e.is_group))
async def start_grp(event):
sender = await bot.get_entity(event.sender_id)
await event.reply(
f"Hey {sender.first_name}!\n__I'm up, protecting this group!__\n**False positives?** Report them to @BotzHubChat!"
)
@bot.on(events.callbackquery.CallbackQuery(data="helpme"))
async def helper_(event):
sender = await bot.get_entity(event.sender_id)
await event.edit(
f"""
{sender.first_name}, here is the help menu.\n
**How to use?**
- Add me to a group, and make me admin, with \"delete messages\" permission.
- If the bot is not admin, it will not be deleting messages containing blacklisted words.\n
**Report False Positives:**
- You are free to report False detections in @BotzHubChat.""",
buttons=[[Button.inline("Back", data="start")]],
)
@bot.on(events.callbackquery.CallbackQuery(data="start"))
async def start_msg(event):
sender = await bot.get_entity(event.sender_id)
await event.edit(
f"Hi {sender.first_name}!\nI am a profanity detector bot.\n\nMake me admin in your group with `delete messages` permission and I'll delete messsages containing abuses!",
buttons=[
[Button.inline("Help π", data="helpme")],
[
Button.url(
"Add me to a group β",
url=f"http://t.me/{(await bot.get_me()).username}?startgroup=botstart",
)
],
[
Button.url("π₯ Channel", url="https://t.me/BotzHub"),
Button.url(
"Package π¦", url="https://pypi.org/project/ProfanityDetector/"
),
],
],
)
@bot.on(events.NewMessage(incoming=True, func=lambda e: e.is_group))
async def deleter_(event):
try:
if await check_if_admin(event):
return
except:
return
sentence = event.raw_text
sender = await bot.get_entity(event.sender_id)
word, detected = detector(sentence)
if detected:
try:
await event.reply(
f"Hey {sender.first_name}, you used a blacklisted word and so your message has been deleted!"
)
await event.delete()
except:
log.info(f"Cannot delete messages in {(await event.get_chat()).title}.")
log.info("Bot has started!")
bot.run_until_disconnected()