-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.py
452 lines (382 loc) · 14.7 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Virtual Number bot for Telegram
# Sends random virtual numbers to user
# Service: OnlineSim.io
# SourceCode (https://github.com/Kourva/OnlineSimBot)
# Standard library imports
import json
import random
import time
from typing import ClassVar, NoReturn, Any, Union, List, Dict
# Related third party module imports
import telebot
import phonenumbers
import countryflag
# Local application module imports
from src import utils
from src.utils import User
from src.vneng import VNEngine
# Initialize the bot token
bot: ClassVar[Any] = telebot.TeleBot(utils.get_token())
print(f"\33[1;36m::\33[m Bot is running with ID: {bot.get_me().id}")
@bot.message_handler(commands=["start", "restart"])
def start_command_handler(message: ClassVar[Any]) -> NoReturn:
"""
Function to handle start commands in bot
Shows welcome messages to users
Parameters:
message (typing.ClassVar[Any]): Incoming message object
Returns:
None (typing.NoReturn)
"""
# Fetch user's data
user: ClassVar[Union[str, int]] = User(message.from_user)
# Send welcome message
bot.send_chat_action(chat_id=message.chat.id, action="typing")
bot.reply_to(
message=message,
text=(
f"⁀➴ Hello {user.pn}\n"
"Welcome to Virtual Number Bot\n\n"
"Send /help to get help message\n"
"Send /number to get a virtual number"
)
)
@bot.message_handler(commands=["help", "usage"])
def help_command_handler(message: ClassVar[Any]) -> NoReturn:
"""
Function to handle help commands in bot
Shows help messages to users
Parameters:
message (typing.ClassVar[Any]): Incoming message object
Returns:
None (typing.NoReturn)
"""
# Fetch user's data
user: ClassVar[Union[str, int]] = User(message.from_user)
# Send Help message
bot.send_chat_action(chat_id=message.chat.id, action="typing")
bot.reply_to(
message=message,
text=(
"·ᴥ· Virtual Number Bot\n\n"
"This bot is using api from onlinesim.io and fetches "
"online and active number.\n"
"All you need is sending few commands to bot and it will "
"find a random number for you.\n\n══════════════\n"
"★ To get new number you can simply send /number command "
"or you can use inline button (Renew) to re-new your number.\n\n"
"★ To get inbox messages use (inbox) inline button. this will show you "
"last 5 messages.\n\n"
"★ You can also check number's telegram profile using inline button "
"(check phone number's profile)\n══════════════\n\n"
"This is all you need to know about this bot!"
)
)
@bot.message_handler(commands=["number"])
def number_command_handler(message: ClassVar[Any]) -> NoReturn:
"""
Function to handle number commands in bot
Finds and sends new virtual number to user
Parameters:
message (typing.ClassVar[Any]): Incoming message object
Returns:
None (typing.NoReturn)
"""
# Send waiting prompt
bot.send_chat_action(chat_id=message.chat.id, action="typing")
prompt: ClassVar[Any] = bot.reply_to(
message=message,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:"
),
)
# Initialize the Virtual Number engine
engine: ClassVar[Any] = VNEngine()
# Get the countries and shuffle them
countries: List[Dict[str, str]] = engine.get_online_countries()
random.shuffle(countries)
# Update prompt based on current status
bot.edit_message_text(
chat_id=message.chat.id,
message_id=prompt.message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
),
)
# Find online and active number
for country in countries:
# Get numbers in country
numbers: List[Dict[str, str]] = engine.get_country_numbers(
country=country['name']
)
# Format country name
country_name: str = country["name"].replace("_", " ").title()
# Check numbers for country and find first valid one
for number in numbers:
# Parse the country to find it's details
parsed_number: ClassVar[Union[str, int]] = phonenumbers.parse(
number=f"+{number[1]}"
)
# Format number to make it readable for user
formatted_number: str = phonenumbers.format_number(
numobj=parsed_number,
num_format=phonenumbers.PhoneNumberFormat.NATIONAL
)
# Find flag emoji for number
flag: str = countryflag.getflag(
[
phonenumbers.region_code_for_country_code(
country_code=parsed_number.country_code
)
]
)
# Update prompt based on current status
bot.edit_message_text(
chat_id=message.chat.id,
message_id=prompt.message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
f"Trying {country_name} ({formatted_number})"
),
)
# Check if number is valid and it's inbox is active
if engine.get_number_inbox(country['name'], number[1]):
# Make keyboard markup for number
Markup: ClassVar[Any] = telebot.util.quick_markup(
{
"𖥸 Inbox": {
"callback_data": f"msg&{country['name']}&{number[1]}"
},
"꩜ Renew": {
"callback_data": f"new_phone_number"
},
"Check phone number's profile": {
"url": f"tg://resolve?phone=+{number[1]}"
}
},
row_width=2
)
# Update prompt based on current status
bot.edit_message_text(
chat_id=message.chat.id,
message_id=prompt.message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
f"Trying {country_name} ({formatted_number})\n\n"
f"{flag} Here is your number: +{number[1]}\n\n"
f"Last Update: {number[0]}"
),
reply_markup=Markup
)
# Return the function
return 1
# Send failure message when no number found
else:
# Update prompt based on current status
bot.edit_message_text(
chat_id=message.chat.id,
message_id=prompt.message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
f"There is no online number for now!"
),
)
# Return the function
return 0
@bot.callback_query_handler(func=lambda x:x.data.startswith("msg"))
def number_inbox_handler(call: ClassVar[Any]) -> NoReturn:
"""
Callback query handler to handle inbox messages
Sends last 5 messages in number's inbox
Parameters:
call (typing.ClassVar[Any]): incoming call object
Returns:
None (typing.NoReturn)
"""
# Initialize the Virtual Number engine
engine: ClassVar[Any] = VNEngine()
# Get country name and number from call's data
country: str
number: str
_, country, number = call.data.split("&")
# Get all messages and select last 5 messages
messages: List[Dict[str, str]] = engine.get_number_inbox(
country=country,
number=number
)[:5]
# Send messages to user
for message in messages:
for key, value in message.items():
bot.send_message(
chat_id=call.message.chat.id,
reply_to_message_id=call.message.message_id,
text=(
f"⚯͛ Time: {key}\n\n"
f"{value.split('received from OnlineSIM.io')[0]}"
)
)
# Answer callback query
bot.answer_callback_query(
callback_query_id=call.id,
text=(
"⁀➴ Here is your last 5 messages\n\n"
"If you didn't get your message, try again after 1 minute!"
),
show_alert=True
)
@bot.callback_query_handler(func=lambda x:x.data == "new_phone_number")
def new_number_handler(call):
"""
Callback query handler to re-new number
Find new phone number and updates the message
Parameters:
call (typing.ClassVar[Any]): incoming call object
Returns:
None (typing.NoReturn)
"""
# Get chat id and message id from call object
chat_id = call.message.chat.id
message_id = call.message.message_id
# Edit message based on current status
bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:"
),
)
# Initialize the Virtual Number engine
engine: ClassVar[Any] = VNEngine()
# Get the countries and shuffle them
countries: List[Dict[str, str]] = engine.get_online_countries()
random.shuffle(countries)
# Update prompt based on current status
bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
),
)
# Find online and active number
for country in countries:
# Get numbers in country
numbers: List[Dict[str, str]] = engine.get_country_numbers(
country=country['name']
)
# Format country name
country_name: str = country["name"].replace("_", " ").title()
# Check numbers for country and find first valid one
for number in numbers:
# Parse the country to find it's details
parsed_number: ClassVar[Union[str, int]] = phonenumbers.parse(
number=f"+{number[1]}"
)
# Format number to make it readable for user
formatted_number: str = phonenumbers.format_number(
numobj=parsed_number,
num_format=phonenumbers.PhoneNumberFormat.NATIONAL
)
# Find flag emoji for number
flag: str = countryflag.getflag(
[
phonenumbers.region_code_for_country_code(
country_code=parsed_number.country_code
)
]
)
# Update prompt based on current status
bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
f"Trying {country_name} ({formatted_number})"
),
)
# Check if number is valid and it's inbox is active
if engine.get_number_inbox(country['name'], number[1]):
# Make keyboard markup for number
Markup: ClassVar[Any] = telebot.util.quick_markup(
{
"𖥸 Inbox": {
"callback_data": f"msg&{country['name']}&{number[1]}"
},
"꩜ Renew": {
"callback_data": f"new_phone_number"
},
"Check phone number's profile": {
"url": f"tg://resolve?phone=+{number[1]}"
}
},
row_width=2
)
# Update prompt based on current status
bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
f"Trying {country_name} ({formatted_number})\n\n"
f"{flag} Here is your number: +{number[1]}\n\n"
f"Last Update: {number[0]}"
),
reply_markup=Markup
)
# Answer callback query
bot.answer_callback_query(
callback_query_id=call.id,
text="⁀➴ Your request updated",
show_alert=False
)
# Return the function
return 1
# Send failure message when no number found
else:
# Update prompt based on current status
bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=(
"Getting a random number for you...\n\n"
"⁀➴ Fetching online countries:\n"
f"Got {len(countries)} countries\n\n"
"⁀➴ Testing active numbers:\n"
f"There is no online number for now!"
),
)
# Return the function
return 0
# Run the bot on polling mode
if __name__ == '__main__':
try:
bot.infinity_polling(
skip_pending=True
)
except KeyboardInterrupt:
raise SystemExit("\n\33[1;31m::\33[m Terminated by user")