randydev commited on
Commit
eebbd4e
Β·
verified Β·
1 Parent(s): 6157f17

Update chatbot/plugins/join_request.py

Browse files
Files changed (1) hide show
  1. chatbot/plugins/join_request.py +97 -95
chatbot/plugins/join_request.py CHANGED
@@ -1,22 +1,4 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
- # Copyright 2020-2024 (c) Randy W @xtdevs, @xtsea
4
- #
5
- # from : https://github.com/TeamKillerX
6
- # Channel : @RendyProjects
7
- # This program is free software: you can redistribute it and/or modify
8
- # it under the terms of the GNU Affero General Public License as published by
9
- # the Free Software Foundation, either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU Affero General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU Affero General Public License
18
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
-
20
  import time
21
  import json
22
  import asyncio
@@ -24,88 +6,44 @@ import io
24
  import os
25
  import re
26
  import logging
 
 
27
  from pyrogram import *
28
- from pyrogram.enums import ChatMemberStatus, ChatMembersFilter, ChatType
29
- from pyrogram import Client, filters
30
  from pyrogram.types import *
31
  from pyrogram.errors import *
32
  from database import db
33
  from logger import LOGS
34
 
 
 
 
 
 
 
 
 
 
 
 
35
  logging.basicConfig(level=logging.INFO)
36
  logger = logging.getLogger(__name__)
37
 
38
  user_list = []
 
39
 
40
- @Client.on_callback_query(filters.regex("^joinrequest_"))
41
- async def joinrequest_callback(client: Client, cb: CallbackQuery):
42
- data = cb.data.split("_")
43
- if len(data) != 2:
44
- await cb.answer("Invalid data format.", True)
45
- return
46
  try:
47
- user_id = int(data[1])
48
- except ValueError:
49
- await cb.answer("Invalid user ID.", True)
50
- return
51
- try:
52
- if cb.from_user.id in user_list:
53
- await client.approve_chat_join_request(
54
- chat_id=cb.message.chat.id,
55
- user_id=user_id,
56
- )
57
- await cb.edit_message_text(
58
- f"User {user_id} joined the request in the group",
59
- disable_web_page_preview=True,
60
- )
61
- else:
62
- await cb.answer("Only the Admins can perform this action.", True)
63
- except Exception as e:
64
- await cb.answer(f"Error: {e}", True)
65
-
66
- @Client.on_callback_query(filters.regex("^declinerequest_"))
67
- async def declinerequest_callback(client: Client, cb: CallbackQuery):
68
- data = cb.data.split("_")
69
- if len(data) != 2:
70
- await cb.answer("Invalid data format.", True)
71
- return
72
- try:
73
- user_id = int(data[1])
74
- except ValueError:
75
- await cb.answer("Invalid user ID.", True)
76
- return
77
- try:
78
- if cb.from_user.id in user_list:
79
- await client.decline_chat_join_request(
80
- chat_id=cb.message.chat.id,
81
- user_id=user_id,
82
- )
83
- await cb.edit_message_text(
84
- f"User {user_id} declined to join the support group",
85
- disable_web_page_preview=True,
86
- )
87
- else:
88
- await cb.answer("Only the Admins can perform this action.", True)
89
- except Exception as e:
90
- await cb.answer(f"Error: {e}", True)
91
-
92
- def create_button_join_request(user_id):
93
- return InlineKeyboardMarkup(
94
- [
95
- [
96
- InlineKeyboardButton(
97
- text="βœ… Approve",
98
- callback_data=f"joinrequest_{user_id}"
99
- )
100
- ],
101
- [
102
- InlineKeyboardButton(
103
- text="❌ Decline",
104
- callback_data=f"declinerequest_{user_id}"
105
- )
106
- ]
107
- ]
108
- )
109
 
110
  @Client.on_chat_join_request(filters.chat("KillerXSupport"))
111
  async def join_request(client: Client, event: ChatJoinRequest):
@@ -115,17 +53,81 @@ async def join_request(client: Client, event: ChatJoinRequest):
115
  async for m in client.get_chat_members(event.chat.id, filter=ChatMembersFilter.ADMINISTRATORS):
116
  if not m.user.is_bot:
117
  user_list.append(m.user.id)
 
 
 
 
 
 
 
 
 
118
  if event.chat.type == ChatType.SUPERGROUP:
119
  try:
120
- user_request_text = f"UserID: {event.from_user.id} by admin soon"
121
- await client.send_message(
122
- event.chat.id,
123
- user_request_text,
124
- reply_markup=create_button_join_request(event.from_user.id)
125
  )
 
126
  except Exception as e:
127
  await client.send_message(
128
  event.chat.id,
129
  text=str(e)
130
  )
131
- logger.error(str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import time
3
  import json
4
  import asyncio
 
6
  import os
7
  import re
8
  import logging
9
+ import string
10
+ import random
11
  from pyrogram import *
 
 
12
  from pyrogram.types import *
13
  from pyrogram.errors import *
14
  from database import db
15
  from logger import LOGS
16
 
17
+ from pyrogram import Client, filters
18
+ from pyrogram.enums import ChatMemberStatus, ChatMembersFilter, ChatType
19
+ from pyrogram.types import (
20
+ CallbackQuery,
21
+ InlineKeyboardMarkup,
22
+ InlineKeyboardButton,
23
+ Message
24
+ )
25
+
26
+ from PIL import Image, ImageDraw, ImageFont
27
+
28
  logging.basicConfig(level=logging.INFO)
29
  logger = logging.getLogger(__name__)
30
 
31
  user_list = []
32
+ captcha_texts = {}
33
 
34
+ def generate_captcha():
35
+ letters = string.ascii_uppercase + string.digits
36
+ captcha_text = ''.join(random.choice(letters) for _ in range(5))
37
+ img = Image.new('RGB', (150, 60), color = (255, 255, 255))
38
+ d = ImageDraw.Draw(img)
 
39
  try:
40
+ font = ImageFont.truetype("arial.ttf", 40)
41
+ except IOError:
42
+ font = ImageFont.load_default()
43
+ d.text((10,10), captcha_text, font=font, fill=(0,0,0))
44
+ img_path = f"captcha_{captcha_text}.png"
45
+ img.save(img_path)
46
+ return captcha_text, img_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  @Client.on_chat_join_request(filters.chat("KillerXSupport"))
49
  async def join_request(client: Client, event: ChatJoinRequest):
 
53
  async for m in client.get_chat_members(event.chat.id, filter=ChatMembersFilter.ADMINISTRATORS):
54
  if not m.user.is_bot:
55
  user_list.append(m.user.id)
56
+ captcha_text, img_path = generate_captcha()
57
+ captcha_texts[user_id] = captcha_text
58
+ captcha_texts["chat_id"] = event.chat.id
59
+ keyboard = InlineKeyboardMarkup(
60
+ [
61
+ [InlineKeyboardButton("πŸ”„ Refresh CAPTCHA", callback_data="refresh_captcha")],
62
+ [InlineKeyboardButton("βœ… Verify", callback_data="verify_captcha")]
63
+ ]
64
+ )
65
  if event.chat.type == ChatType.SUPERGROUP:
66
  try:
67
+ await client.send_photo(
68
+ event.from_user.id,
69
+ photo=img_path,
70
+ caption=f"❗️ **Verify that you are human!**\n\n❔ Please enter the CAPTCHA text below the image.",
71
+ reply_markup=keyboard
72
  )
73
+ os.remove(img_path)
74
  except Exception as e:
75
  await client.send_message(
76
  event.chat.id,
77
  text=str(e)
78
  )
79
+ logger.error(str(e))
80
+
81
+ @Client.on_callback_query(filters.regex("^refresh_captcha$"))
82
+ async def refresh_captcha_callback(client: Client, cb: CallbackQuery):
83
+ user_id = cb.from_user.id
84
+ if user_id in captcha_texts:
85
+ del captcha_texts[user_id]
86
+ captcha_text, img_path = generate_captcha()
87
+ captcha_texts[user_id] = captcha_text
88
+ keyboard = InlineKeyboardMarkup(
89
+ [
90
+ [InlineKeyboardButton("πŸ”„ Refresh CAPTCHA", callback_data="refresh_captcha")],
91
+ [InlineKeyboardButton("βœ… Verify", callback_data="verify_captcha")]
92
+ ]
93
+ )
94
+ await cb.edit_message_media(
95
+ media=InputMediaPhoto(open(img_path, "rb")),
96
+ reply_markup=keyboard
97
+ )
98
+ os.remove(img_path)
99
+ await cb.answer("CAPTCHA refreshed!", show_alert=False)
100
+
101
+ @Client.on_callback_query(filters.regex("^verify_captcha$"))
102
+ async def verify_captcha_callback(client: Client, cb: CallbackQuery):
103
+ user_id = cb.from_user.id
104
+ if user_id not in captcha_texts:
105
+ await cb.answer("❗️ Please start the CAPTCHA verification first by join request.", show_alert=True)
106
+ return
107
+ await cb.message.reply_text("πŸ” **Please enter the CAPTCHA text:**")
108
+ await cb.answer()
109
+
110
+ @Client.on_message(filters.text & filters.private)
111
+ async def handle_captcha_response(client: Client, message: Message):
112
+ user_id = message.from_user.id
113
+ user_response = message.text.strip().upper()
114
+ if user_id not in captcha_texts:
115
+ await message.reply("❗️ Please start the CAPTCHA verification first by join request again.")
116
+ return
117
+ correct_captcha = captcha_texts.get(user_id)
118
+ if user_response == correct_captcha:
119
+ await message.reply("βœ… CAPTCHA verification successful!")
120
+ await client.approve_chat_join_request(
121
+ chat_id=captcha_texts.get("chat_id"),
122
+ user_id=user_id
123
+ )
124
+ del captcha_texts[user_id]
125
+ del captcha_texts["chat_id"]
126
+ else:
127
+ await message.reply("❌ Incorrect CAPTCHA. Please try again by join request again.")
128
+ await client.decline_chat_join_request(
129
+ chat_id=captcha_texts.get("chat_id"),
130
+ user_id=user_id
131
+ )
132
+ del captcha_texts[user_id]
133
+ del captcha_texts["chat_id"]