approve-dev / chatbot /plugins /join_request.py
randydev's picture
Update chatbot/plugins/join_request.py
864a0ae verified
raw
history blame
4.71 kB
import os
import time
import json
import asyncio
import io
import os
import re
import logging
import string
import random
from pyrogram import *
from pyrogram.types import *
from pyrogram.errors import *
from database import db
from logger import LOGS
from pyrogram import Client, filters
from pyrogram.enums import ChatMemberStatus, ChatMembersFilter, ChatType
from pyrogram.types import (
CallbackQuery,
InlineKeyboardMarkup,
InlineKeyboardButton,
Message
)
from PIL import Image, ImageDraw, ImageFont
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
user_list = []
captcha_texts = {}
def generate_captcha():
letters = string.ascii_uppercase + string.digits
captcha_text = ''.join(random.choice(letters) for _ in range(5))
img = Image.new('RGB', (150, 60), color = (255, 255, 255))
d = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("arial.ttf", 40)
except IOError:
font = ImageFont.load_default()
d.text((10,10), captcha_text, font=font, fill=(0,0,0))
img_path = f"captcha_{captcha_text}.png"
img.save(img_path)
return captcha_text, img_path
@Client.on_chat_join_request(filters.chat("KillerXSupport"))
async def join_request(client: Client, event: ChatJoinRequest):
member = await client.get_chat_member(event.chat.id, "me")
if member.status != ChatMemberStatus.ADMINISTRATOR:
return await client.send_message(event.chat.id, text="I am not an administrator in this group.")
async for m in client.get_chat_members(event.chat.id, filter=ChatMembersFilter.ADMINISTRATORS):
if not m.user.is_bot:
user_list.append(m.user.id)
captcha_text, img_path = generate_captcha()
captcha_texts[event.from_user.id] = captcha_text
captcha_texts["chat_id"] = event.chat.id
keyboard = InlineKeyboardMarkup(
[
[InlineKeyboardButton("πŸ”„ Refresh CAPTCHA", callback_data="refresh_captcha")],
[InlineKeyboardButton("βœ… Verify", callback_data="verify_captcha")]
]
)
if event.chat.type == ChatType.SUPERGROUP:
try:
await client.send_photo(
event.from_user.id,
photo=img_path,
caption=f"❗️ **Verify that you are human!**\n\n❔ Please enter the CAPTCHA text below the image.",
reply_markup=keyboard
)
os.remove(img_path)
except Exception as e:
await client.send_message(
event.chat.id,
text=str(e)
)
logger.error(str(e))
@Client.on_callback_query(filters.regex("^refresh_captcha$"))
async def refresh_captcha_callback(client: Client, cb: CallbackQuery):
user_id = cb.from_user.id
if user_id in captcha_texts:
del captcha_texts[user_id]
captcha_text, img_path = generate_captcha()
captcha_texts[user_id] = captcha_text
keyboard = InlineKeyboardMarkup(
[
[InlineKeyboardButton("πŸ”„ Refresh CAPTCHA", callback_data="refresh_captcha")],
[InlineKeyboardButton("βœ… Verify", callback_data="verify_captcha")]
]
)
await cb.edit_message_media(
media=InputMediaPhoto(img_path),
reply_markup=keyboard
)
os.remove(img_path)
await cb.answer("CAPTCHA refreshed!", show_alert=False)
@Client.on_callback_query(filters.regex("^verify_captcha$"))
async def verify_captcha_callback(client: Client, cb: CallbackQuery):
user_id = cb.from_user.id
if user_id not in captcha_texts:
await cb.answer("❗️ Please start the CAPTCHA verification first by join request.", show_alert=True)
return
await cb.message.reply_text("πŸ” **Please enter the CAPTCHA text:**")
await cb.answer()
@Client.on_message(filters.text & filters.private)
async def handle_captcha_response(client: Client, message: Message):
user_id = message.from_user.id
user_response = message.text.strip().upper()
if user_id not in captcha_texts:
return
correct_captcha = captcha_texts.get(user_id)
if user_response == correct_captcha:
await message.reply("βœ… CAPTCHA verification successful!")
await client.approve_chat_join_request(
chat_id=captcha_texts.get("chat_id"),
user_id=user_id
)
del captcha_texts[user_id]
del captcha_texts["chat_id"]
else:
await message.reply("❌ Incorrect CAPTCHA. Please try again by join request again.")
await client.decline_chat_join_request(
chat_id=captcha_texts.get("chat_id"),
user_id=user_id
)
del captcha_texts[user_id]
del captcha_texts["chat_id"]