forked from TeleDark/v2ray-tel-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
291 lines (225 loc) · 9.35 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import qrcode
import requests
import base64
import re
from io import BytesIO
from PIL import Image
from pyzbar.pyzbar import decode
from keys import *
from utils import account_info
from telegram import (
Update,
InlineKeyboardButton,
InlineKeyboardMarkup,
BotCommand,
error
)
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
CallbackQueryHandler,
ConversationHandler,
filters,
)
from telegram.constants import ParseMode
ACCOUNT_LINK_INPUT = 1
AFTER_QR_SENT = 2
WHAT_APP = {
"Nekoray": {
"name": "Nekoray",
'desc': "Nekoray",
"image_path": os.path.join(real_dir,"images/nekoray.jpg")
},
"V2rayNG": {
"name": "V2rayNG",
"desc": "V2rayNG",
"image_path": os.path.join(real_dir,"images/v2rayng.jpg")
},
"OneClick": {
"name": "OneClick",
"desc": "OneClick",
"image_path": os.path.join(real_dir,"images/oneclick.jpg")
},
"NamsternetV": {
"name": "NapsterNetV",
"desc": "NapsterNetV",
"image_path": os.path.join(real_dir,"images/napsternetv.jpg")
},
}
def is_base64(s):
try:
return base64.b64encode(base64.b64decode(s)) == s
except Exception:
return False
def is_url(text):
url_pattern = re.compile(r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+')
return url_pattern.match(text) is not None
async def check_membership(context: ContextTypes.DEFAULT_TYPE, channel_id: str, user_id: int) -> bool:
try:
member_status = await context.bot.get_chat_member(chat_id=channel_id, user_id=user_id)
return member_status.status == 'member'
except error.TelegramError:
return False
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_name = update.message.from_user.first_name
user_id = update.message.from_user.id
await update.message.reply_text(f"سلام {user_name} عزیز خوش اومدی\n" + msg_yaml['start_msg'])
if channel_id:
is_member = await check_membership(context, channel_id, user_id)
if not is_member:
await update.message.reply_text(msg_yaml['channel_msg'])
async def generate_qrcode(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text(msg_yaml['qrcode'])
return ACCOUNT_LINK_INPUT
async def process_account_link(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
account_link = update.message.text
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=3
)
qr.add_data(account_link)
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
qr_image_buffer = BytesIO()
qr_image.save(qr_image_buffer)
qr_image_buffer.seek(0)
await update.message.reply_photo(photo=qr_image_buffer)
return AFTER_QR_SENT
async def get_account_info(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# check if message is edited
if update.edited_message is not None:
return
user_id = update.message.from_user.id
if channel_id:
is_member = await check_membership(context, channel_id, user_id)
if not is_member:
await update.message.reply_text(msg_yaml['channel_msg'])
return
if update.message.photo:
# If the message contains a photo, decode the QR code
photo_id = update.message.photo[-1].file_id
photo_file = await context.bot.get_file(photo_id)
photo_data = await photo_file.download_as_bytearray()
img = Image.open(BytesIO(photo_data))
decode_objects = decode(img)
if decode_objects:
# If a QR code is found, extract the decoded text
decode_qrcode = decode_objects[0].data.decode("utf-8")
acc_info = account_info(decode_qrcode)
else:
await update.message.reply_text("لطفا QRCode ارسال کنید")
return ConversationHandler.END
else:
# If the message doesn't contain a photo, use the text message
client_msg = update.message.text
"""
Fetch the first account detail from the given subscription URL.
If the URL content is base64 encoded, it decodes it.
Returns the first account or None if an error occurs.
"""
if is_url(client_msg):
request_url = client_msg
try:
res = requests.get(request_url)
res_content = res.content
if is_base64(res_content):
accounts = base64.b64decode(res_content).decode('utf-8')
else:
accounts = res_content.decode('utf-8')
client_msg = accounts.split()[0]
except requests.exceptions.RequestException as e:
await update.message.reply_text("در خواندن لینک ساب شما مشکلی پیش آمد")
acc_info = account_info(client_msg)
if acc_info == 'not found':
await update.message.reply_text(msg_yaml['not_found'], parse_mode=ParseMode.HTML)
return ConversationHandler.END
status, account_name, up, down, used, total, traffic_remaining, expiry = acc_info
rem_time, expiry = expiry
keyboard = [
[InlineKeyboardButton(f"نام اکانت: {account_name}", callback_data='1')],
[InlineKeyboardButton(f"⚙️ وضعیت اکانت: {status}", callback_data='1')],
[
InlineKeyboardButton(f"⬆️ {up} :آپلود",callback_data='1'),
InlineKeyboardButton(f"⬇️ {down} :دانلود",callback_data='1',)
],
[InlineKeyboardButton(f"{used} :میزان مصرف⏳", callback_data='1')],
[InlineKeyboardButton(
f"📡 حجم باقی مانده : {traffic_remaining}", callback_data='1')],
[InlineKeyboardButton(
f"🕒 زمان باقی مانده : {rem_time}", callback_data='1')],
[InlineKeyboardButton(f" 🌐 حجم کل: {total}", callback_data='1')],
[InlineKeyboardButton(f"{expiry} 🔚", callback_data='1')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(msg_yaml['acc_info'], reply_markup=reply_markup)
return ConversationHandler.END
async def show_what_app_handle(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
keyboard = []
for what_app, what_app_dict in WHAT_APP.items():
keyboard.append([InlineKeyboardButton(
what_app_dict["name"], callback_data=f"what_app|{what_app}")])
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(msg_yaml['whatapp_msg'], reply_markup=reply_markup)
async def what_app_handle(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if channel_id:
user_id = update.message.from_user.id
is_member = await check_membership(context, channel_id, user_id)
if not is_member:
await update.message.reply_text(msg_yaml['channel_msg'])
return
query = update.callback_query
await query.answer()
what_app = query.data.split("|")[1]
desc_app = f"{WHAT_APP[what_app]['desc']}"
image_path = f"{WHAT_APP[what_app]['image_path']}"
await query.message.reply_photo(open(image_path, 'rb'), desc_app)
await query.delete_message()
async def help_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if channel_id:
user_id = update.message.from_user.id
is_member = await check_membership(context, channel_id, user_id)
if not is_member:
await update.message.reply_text(msg_yaml['channel_msg'])
return
await update.message.reply_text(msg_yaml['help_msg'])
async def post_init(application: Application):
await application.bot.set_my_commands([
BotCommand("/start", "استارت"),
BotCommand("/qrcode", "ساخت QRCode"),
BotCommand("/what", "چه نرم افزاری استفاده میکنید؟"),
])
def main() -> None:
"""Run bot."""
try:
# Create the Application and pass it your bot's token.
application = Application.builder().token(telegram_token).post_init(post_init).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_handler))
conv_handler = ConversationHandler(
entry_points= [
CommandHandler("qrcode", generate_qrcode), MessageHandler(
~filters.COMMAND, get_account_info)
],
states= {
ACCOUNT_LINK_INPUT: [MessageHandler(filters.TEXT, process_account_link)],
AFTER_QR_SENT: [MessageHandler(filters.TEXT, get_account_info)]
},
fallbacks=[],
)
application.add_handler(conv_handler)
application.add_handler(CommandHandler(
"what", show_what_app_handle))
application.add_handler(CallbackQueryHandler(
what_app_handle, pattern="^what_app"))
# Run the bot until the user presses Ctrl-C
application.run_polling()
except error.InvalidToken:
print('You must pass the token you received from https://t.me/Botfather!')
except Exception as e:
print("error:", e)
if __name__ == "__main__":
main()