-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmain.py
206 lines (194 loc) · 8.93 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# (c) @AbirHasan2005
import os
import random
import logging
import aiohttp
import asyncio
from configs import Config
from pyrogram import Client, filters
from pyrogram.errors import UserNotParticipant
from helpers.captcha import make_captcha
from helpers.generate_id import generate_rnd_id
from helpers.markup_maker import make_captcha_markup
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message, CallbackQuery, ChatPermissions
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(threadName)s %(name)s %(message)s",
handlers=[logging.StreamHandler(), logging.FileHandler("log.txt")],
)
CaptchaBot = Client(
Config.SESSION_NAME,
api_id=Config.API_ID,
api_hash=Config.API_HASH,
bot_token=Config.BOT_TOKEN
)
CaptchaDB = {}
@CaptchaBot.on_message(filters.command("start"))
async def start_handler(_, event: Message):
await event.reply_text("Hi, I am captcha bot by @AbirHasan2005.")
@CaptchaBot.on_chat_member_updated()
async def welcome_handler(bot: Client, event: Message):
if (event.chat.id != Config.GROUP_CHAT_ID) or (event.from_user.is_bot is True):
return
try:
user_ = await bot.get_chat_member(event.chat.id, event.from_user.id)
if (user_.is_member is False) and (CaptchaDB.get(event.from_user.id, None) is not None):
try:
await bot.delete_messages(
chat_id=event.chat.id,
message_ids=CaptchaDB[event.from_user.id]["message_id"]
)
except:
pass
return
elif (user_.is_member is False) and (CaptchaDB.get(event.from_user.id, None) is None):
return
except UserNotParticipant:
return
try:
if CaptchaDB.get(event.from_user.id, None) is not None:
try:
await bot.send_message(
chat_id=event.chat.id,
text=f"{event.from_user.mention} again joined group without verifying!\n\n"
f"He can try again after 10 minutes.",
disable_web_page_preview=True
)
await bot.restrict_chat_member(
chat_id=event.chat.id,
user_id=event.from_user.id,
permissions=ChatPermissions(can_send_messages=False)
)
await bot.delete_messages(chat_id=event.chat.id,
message_ids=CaptchaDB[event.from_user.id]["message_id"])
except:
pass
await asyncio.sleep(600)
del CaptchaDB[event.from_user.id]
else:
await bot.restrict_chat_member(
chat_id=event.chat.id,
user_id=event.from_user.id,
permissions=ChatPermissions(can_send_messages=False)
)
await bot.send_message(
chat_id=event.chat.id,
text=f"{event.from_user.mention}, to chat here, please verify that you are not a robot.",
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton("Verify Now", callback_data=f"startVerify_{str(event.from_user.id)}")]
])
)
except:
pass
@CaptchaBot.on_callback_query()
async def buttons_handlers(bot: Client, cb: CallbackQuery):
if cb.data.startswith("startVerify_"):
__user = cb.data.split("_", 1)[-1]
if cb.from_user.id != int(__user):
await cb.answer("This Message is Not For You!", show_alert=True)
return
await cb.message.edit("Generating Captcha ...")
print("Fetching Captcha ...")
data, emoji_path_ = make_captcha(generate_rnd_id())
print("Done!")
markup = [[], [], []]
__emojis = data.split(": ", 1)[-1].split()
print(__emojis)
_emojis = ['🐻', '🐔', '☁️', '🔮', '🌀', '🌚', '💎', '🐶', '🍩', '🌏', '🐸', '🌕', '🍏', '🐵', '🌙',
'🐧', '🍎', '😀', '🐍', '❄️', '🐚', '🐢', '🌝', '🍺', '🍔', '🍒', '🍫', '🍡', '🌑', '🍟',
'☕️', '🍑', '🍷', '🍧', '🍕', '🍵', '🐋', '🐱', '💄', '👠', '💰', '💸', '🎹', '📦', '📍',
'🐊', '🦕', '🐬', '💋', '🦎', '🦈', '🦷', '🦖', '🐠', '🐟','💀', '🎃', '👮', '⛑', '🪢', '🧶',
'🧵', '🪡', '🧥', '🥼', '🥻', '🎩', '👑', '🎒', '🙊', '🐗', '🦋', '🦐', '🐀', '🐿', '🦔', '🦦',
'🦫', '🦡', '🦨', '🐇']
random.shuffle(_emojis)
_emojis = _emojis[:20]
print("Cleaning Answer Emojis from Emojis List ...")
for a in range(len(__emojis)):
if __emojis[a] in _emojis:
_emojis.remove(__emojis[a])
show = __emojis
print("Appending New Emoji List ...")
for b in range(9):
show.append(_emojis[b])
print("Randomizing ...")
random.shuffle(show)
count = 0
print("Appending to ROW - 1")
for _ in range(5):
markup[0].append(InlineKeyboardButton(f"{show[count]}",
callback_data=f"verify_{str(cb.from_user.id)}_{show[count]}"))
count += 1
print("Appending to ROW - 2")
for _ in range(5):
markup[1].append(InlineKeyboardButton(f"{show[count]}",
callback_data=f"verify_{str(cb.from_user.id)}_{show[count]}"))
count += 1
print("Appending to ROW - 3")
for _ in range(5):
markup[2].append(InlineKeyboardButton(f"{show[count]}",
callback_data=f"verify_{str(cb.from_user.id)}_{show[count]}"))
count += 1
print("Setting Up in Database ...")
CaptchaDB[cb.from_user.id] = {
"emojis": data.split(": ", 1)[-1].split(),
"mistakes": 0,
"group_id": cb.message.chat.id,
"message_id": None
}
print("Sending Captcha ...")
__message = await bot.send_photo(
chat_id=cb.message.chat.id,
photo=emoji_path_,
caption=f"{cb.from_user.mention}, select all the emojis you can see in the picture. "
f"You are allowed only (3) mistakes.",
reply_markup=InlineKeyboardMarkup(markup)
)
os.remove(emoji_path_)
CaptchaDB[cb.from_user.id]["message_id"] = __message.id
await cb.message.delete(revoke=True)
elif cb.data.startswith("verify_"):
__emoji = cb.data.rsplit("_", 1)[-1]
__user = cb.data.split("_")[1]
if cb.from_user.id != int(__user):
await cb.answer("This Message is Not For You!", show_alert=True)
return
if cb.from_user.id not in CaptchaDB:
await cb.answer("Try Again After Re-Join!", show_alert=True)
await cb.message.delete()
if __emoji not in CaptchaDB.get(cb.from_user.id).get("emojis"):
CaptchaDB[cb.from_user.id]["mistakes"] += 1
await cb.answer("You pressed wrong emoji!", show_alert=True)
n = 3 - CaptchaDB[cb.from_user.id]['mistakes']
if n == 0:
await cb.message.delete(True)
await bot.send_message(
chat_id=cb.message.chat.id,
text=f"{cb.from_user.mention}, you failed to solve the captcha!\n\n"
f"You can try again after 10 minutes."
)
await asyncio.sleep(600)
del CaptchaDB[cb.from_user.id]
return
markup = make_captcha_markup(cb.message.reply_markup.inline_keyboard, __emoji, "❌")
await cb.message.edit_caption(
caption=f"{cb.from_user.mention}, select all the emojis you can see in the picture. "
f"You are allowed only ({n}) mistakes.",
reply_markup=InlineKeyboardMarkup(markup)
)
return
else:
CaptchaDB.get(cb.from_user.id).get("emojis").remove(__emoji)
markup = make_captcha_markup(cb.message.reply_markup.inline_keyboard, __emoji, "✅")
await cb.message.edit_reply_markup(reply_markup=InlineKeyboardMarkup(markup))
if not CaptchaDB.get(cb.from_user.id).get("emojis"):
await cb.answer("You Passed the Captcha!", show_alert=True)
del CaptchaDB[cb.from_user.id]
try:
UserOnChat = await bot.get_chat_member(user_id=cb.from_user.id, chat_id=cb.message.chat.id)
if UserOnChat.restricted_by.id == (await bot.get_me()).id:
await bot.unban_chat_member(chat_id=cb.message.chat.id, user_id=cb.from_user.id)
except:
pass
await cb.message.delete(True)
await cb.answer()
CaptchaBot.run()