-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
427 lines (362 loc) · 20.7 KB
/
service.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
from telegram import Update
from telegram.ext import filters, ApplicationBuilder, CommandHandler, MessageHandler
from telegram.ext import ContextTypes
import telethon.types
from telethon import TelegramClient
from telethon.tl.types import InputChannel, InputUser
from telethon.tl.custom.dialog import Dialog
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
from datetime import datetime, timedelta
from enum import StrEnum, auto
import logging
import os
import persistence
from persistence import create_db, UserManager
logging.basicConfig(level=logging.ERROR)
class Config:
def __init__(self, *, admins: list[int], api_id: int, api_hash: str, bot_token: str, phone: str):
self.admins = admins
self.api_id = api_id
self.api_hash = api_hash
self.bot_token = bot_token
self.phone = phone
class CommandType(StrEnum):
CLEAN_DB = auto()
IMPORT = auto()
INVITE = auto()
NO_OP = auto()
POST = auto()
SIGNIN = auto()
SIGN_OUT = auto()
STAT = auto()
TOKEN_INIT = auto()
class Service:
def __init__(self, config: Config):
self.config = config
self.last_cmd: CommandType = CommandType.NO_OP
self.invited_users_24h: int = 0
self.last_invite: datetime = datetime.now()
self.scout_client: TelegramClient = TelegramClient('real_user', config.api_id, config.api_hash)
# DEBUG MODE: .start(phone=lambda: config.phone))
self.bot_client: TelegramClient | None = None
self.app = ApplicationBuilder().token(config.bot_token).build()
self.app.add_handler(CommandHandler("clean_cache", self._clean_cache))
self.app.add_handler(CommandHandler("clean_db", self._clean_db))
self.app.add_handler(CommandHandler("disconnect", self._disconnect))
self.app.add_handler(CommandHandler("start", self._start))
self.app.add_handler(CommandHandler("import", self._import_users))
self.app.add_handler(CommandHandler("invite", self._invite))
self.app.add_handler(CommandHandler("listchats", self._list_chats))
self.app.add_handler(CommandHandler("new_post", self._new_post))
self.app.add_handler(CommandHandler("signin", self._signin))
self.app.add_handler(CommandHandler("sendcode", self._send_code))
self.app.add_handler(CommandHandler("signout", self._sign_out))
self.app.add_handler(CommandHandler("stat", self._stat))
self.app.add_handler(CommandHandler("token", self._token))
self.app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), self._text))
def run(self):
self.app.run_polling()
async def _clean_cache(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
try:
with os.scandir("./") as scan_iter:
import fnmatch
for file in scan_iter:
if file.is_file() and fnmatch.fnmatch(file.name, "stat_*.txt"):
os.remove("./" + file.name)
await update.message.reply_text("Stats files deleted")
except OSError as err:
await update.message.reply_text(f"{err}")
self.last_cmd = CommandType.NO_OP
async def _clean_db(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Are you sure you want cleanup the database? (yes/no)")
self.last_cmd = CommandType.CLEAN_DB
async def _disconnect(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
if (res := await self._check_client()) is not None:
await update.message.reply_text(res)
self.last_cmd = CommandType.NO_OP
else:
await self.scout_client.disconnect()
await update.message.reply_text("Client disconnected.")
self.last_cmd = CommandType.NO_OP
async def _import_users(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
await self._check_conn()
await update.message.reply_text('From which public source group you want import? ')
self.last_cmd = CommandType.IMPORT
async def _invite(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
await self._check_conn()
await update.message.reply_text('Specify limit number of users, destination group and '
'if to force already invited users. Format: (e.g: 200,hotelForAll,yes)')
self.last_cmd = CommandType.INVITE
async def _list_chats(self, update: Update, _: ContextTypes.DEFAULT_TYPE):
"""Lists client chats formatted"""
if (res := await self._check_client()) is not None:
await update.message.reply_text(res)
self.last_cmd = CommandType.NO_OP
return
groups: list[str] = []
channels: list[str] = []
user_chats: list[str] = []
dialog: Dialog
async for dialog in self.scout_client.iter_dialogs():
if dialog.is_group:
groups.append(f"{dialog.name}: {dialog.id})")
elif dialog.is_channel:
channels.append(f"{dialog.name}, {dialog.id}")
else:
user_chats.append(f"{dialog.name}, {dialog.id}")
groups_fmt = "\n".join(groups)
channels_fmt = "\n".join(channels)
user_chats_fmt = "\n".join(user_chats)
response = (f'Groups:\n{groups_fmt}\n\n'
f'Channels:\n{channels_fmt}\n\n'
f'Private chats:\n{user_chats_fmt}')
await update.message.reply_text(response)
self.last_cmd = CommandType.NO_OP
async def _new_post(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
if self.bot_client.is_user_authorized() and self.bot_client.is_connected():
await update.message.reply_text("Write your post here.")
self.last_cmd = CommandType.POST
else:
await update.message.reply_text("Please use /token to login your userbot.")
async def _send_code(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
if self.scout_client.is_user_authorized():
await update.message.reply_text("Real user already authorized.")
else:
await self.scout_client.send_code_request(self.config.phone)
await update.message.reply_text(f"Auth code sent on {self.config.phone}. Use /signin to login.")
self.last_cmd = CommandType.NO_OP
async def _signin(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text(f'Please paste the auth code received on {self.config.phone}')
self.last_cmd = CommandType.SIGNIN
async def _sign_out(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
if (res := await self._check_client()) is not None:
await update.message.reply_text(res)
self.last_cmd = CommandType.NO_OP
else:
await update.message.reply_text("Are you sure you want log out the real user? (yes/no) ")
self.last_cmd = CommandType.SIGN_OUT
async def _start(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
"""Bot entry point callback. Allowed only to admins."""
if update.effective_user.id not in self.config.admins:
await update.message.reply_text(
f"Sorry {update.effective_user.first_name} you're not enabled for this service.")
return
await create_db()
message: str = f'Hello {update.effective_user.first_name}!'
if not await self.scout_client.is_user_authorized():
_ = await self.scout_client.send_code_request(self.config.phone)
await update.message.reply_text(f'{message}. Auth code sent to {self.config.phone}.\n'
f'Call /signin command to login.')
else:
await self._check_conn()
await update.message.reply_text(f'{message} Please write the bot user token for add users.')
self.last_cmd = CommandType.TOKEN_INIT
async def _stat(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text('From which date you want statistics? (DD-MM-YYYY). \'today\' for all.')
self.last_cmd = CommandType.STAT
async def _text(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Text handler based on the last command"""
response: str = ""
message_text = update.message.text
if message_text == "" or message_text is None:
await update.message.reply_text("Message text empty.")
return
match self.last_cmd:
case CommandType.CLEAN_DB:
mess_to_lower = message_text.lower()
match mess_to_lower:
case "yes":
await UserManager.delete_all()
response = "Database data burned. DB structure is still saved."
self.last_cmd = CommandType.NO_OP
case "no":
response = "Operation cancelled. Your database integrity is save."
self.last_cmd = CommandType.NO_OP
case _:
response = "Answer not accepted. Accepted: (yes/no). Try again!"
case CommandType.IMPORT:
await context.bot.send_message(chat_id=update.effective_chat.id,
text=f'Try importing users from {message_text}...')
match await self._search_dialog(message_text):
case Dialog() as d:
user: telethon.types.User
users_count: int = 0
async for user in self.scout_client.iter_participants(d.id):
# skips itself and the admins
if user.is_self or user.id in self.config.admins:
continue
read_id = await UserManager.create(user.id, user.username)
if read_id > 0:
users_count += 1
response = f'Successfully imported {users_count} users from group {message_text}.'
case None:
response = f'Chat {message_text} not found in chats. Try again!'
case CommandType.INVITE:
try:
(limit_str, destination, forced) = message_text.split(',')
limit = int(limit_str)
if limit > 200:
await update.message.reply_text(f"{limit} is greater then max limit 200.")
return
# verifies the violation of 200+ invited in 24h
diff = datetime.now() - self.last_invite
if diff.days >= 1:
self.last_invite = datetime.now()
self.invited_users_24h = 0
if self.invited_users_24h == 200:
await update.message.reply_text(f"Daily limit reached. "
f"You've to wait until to "
f"{self.last_invite + timedelta(days=1)}")
return
match await self._search_dialog(destination):
case Dialog() as d:
# forces the API to invite also already invited users
is_forced = forced.lower() == "yes"
forced_message = "forcing" if is_forced else "not forcing"
await context.bot.send_message(chat_id=update.effective_chat.id,
text=f'Try inviting {limit_str} users '
f'to {destination} ({forced_message})...')
channel_peer_entity = await self.scout_client.get_input_entity(d.id)
channel_entity = InputChannel(channel_peer_entity.channel_id,
channel_peer_entity.access_hash)
# read the users from db and try to add them to `destination` chat
refused: int = 0
total_invited: int = 0
user_read: persistence.User
for user_read in await UserManager.read_all(
include_invited=is_forced,
limit=limit):
# check if 48 hours are passed since the last invite to user_read, if not skip
if (invited_datetime := user_read.invited_at) is not None:
if (datetime.now() - invited_datetime).days < 2:
continue
try:
user_peer_entity = await (self.scout_client
.get_input_entity(user_read.telegram_id))
user_entity = InputUser(user_peer_entity.user_id, user_peer_entity.access_hash)
# if self.bot_client is None:
# raise ValueError("Bot client not initialized")
await self.scout_client(InviteToChannelRequest(channel_entity, [user_entity]))
await UserManager.update_to_invited(user_read.telegram_id)
except UserPrivacyRestrictedError as err:
# is useless to keep data of a user who locks coming connections
await UserManager.delete()
logging.error(f"user_id:{user_read.telegram_id} -> {err}")
refused += 1
except telethon.errors.rpcerrorlist.UserNotMutualContactError as err:
# you're locked for 24/48h after the first unilateral contact (User.invited_at)
logging.error(f"user_id:{user_read.telegram_id} -> {err}")
refused += 1
except ValueError as verr:
await update.message.reply_text(f"{verr}")
return
else:
total_invited += 1
self.invited_users_24h += 1
if self.invited_users_24h == 200:
break
real_inv = f"{limit} users not available, only {total_invited}." \
if total_invited < limit else ""
if self.invited_users_24h == 200:
response = ""
else:
response = (f'{real_inv} Successfully invited '
f'{total_invited - refused}/{total_invited} '
f'users to {destination}. (restriction due to limit 200 users reached)')
case None:
response = f'Group {destination} not found in chats. Try again!'
except PeerFloodError as err:
logging.error(f"{err}")
response = ("Flood error, too many attempts."
"Try /disconnect or, if not works, /sign_out after 60 seconds or more.")
except ValueError as verr:
response = f'{verr}.\nRight format(3 elements): limitNum, destination, forced. Try again!'
case CommandType.NO_OP:
response = f"Cannot accept text messages for command {self.last_cmd.name}."
case CommandType.POST:
pass
case CommandType.SIGNIN:
result = await self.scout_client.sign_in(self.config.phone, message_text)
if type(result) is telethon.types.User:
response = f"User signed in correctly."
self.last_cmd = CommandType.NO_OP
else:
response = f'\nWrong auth code format. Try again!'
case CommandType.SIGN_OUT:
mess_to_lower = message_text.lower()
match mess_to_lower:
case "yes":
response = ("Client successfully logged out. "
"To use again the APIs please use /send_code and then /signin") \
if await self.scout_client.log_out() \
else "INTERNAL SERVER ERROR: could not log out correctly!"
self.last_cmd = CommandType.NO_OP
case "no":
response = "Operation cancelled. You're still authorized."
self.last_cmd = CommandType.NO_OP
case _:
response = "Answer not accepted. Accepted: (yes/no). . Try again!"
case CommandType.STAT:
dt: datetime = datetime.now()
try:
if message_text != "today":
from dateutil import parser
dt = parser.parse(message_text)
if dt > datetime.now():
raise ValueError()
import aiofiles
path: str = f"./stat_{datetime.now()}.txt"
async with aiofiles.open(path, mode="w") as file:
await file.write(f"Statistics until {dt}:\n\n")
await file.write("id | username | created_at | invited_at\n\n")
user_info: persistence.User
for user_info in await UserManager.read_all(until_to=dt):
await file.write(f"{user_info.telegram_id} | {user_info.username} | "
f"{user_info.created_at} | {user_info.invited_at}\n")
message_to_bot_admin = await update.message.reply_document(path)
if message_to_bot_admin is not None:
os.remove(path)
response = f"Download stat file complete."
self.last_cmd = CommandType.NO_OP
else:
response = "Error while downloading the stat file. Try again!"
except (OverflowError, ValueError) as err:
response = f'{err}\nWrong date string format or limit. Try again!'
except FileNotFoundError as err:
response = f"{err}"
case CommandType.TOKEN_INIT:
try:
self.bot_client = (TelegramClient('bot_user', self.config.api_id, self.config.api_hash)
.start(bot_token=message_text))
response = "Bot user connected."
self.last_cmd = CommandType.NO_OP
except ValueError as error:
logging.error(error)
response = f"{error}. Try again!"
await update.message.reply_text(response)
async def _token(self, update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Please, write the bot token")
self.last_cmd = CommandType.TOKEN_INIT
async def _check_conn(self) -> str | None:
"""Returns `str` if the connection fails, else `None`"""
if not self.scout_client.is_connected():
try:
await self.scout_client.connect()
except OSError as os_error:
return f"Failed to connect to telegram client {os_error}"
return None
async def _check_client(self) -> str | None:
"""Returns `str` if the user is not authorized or the connection fails, else `None`"""
if not await self.scout_client.is_user_authorized():
return "User not authorized. Please use /send_"
if (res := await self._check_conn()) is not None:
return res
return None
async def _search_dialog(self, message_text: str) -> Dialog | None:
"""Returns `int` (chat_id) if the user's chats name is `message_str` """
dialog: Dialog
async for dialog in self.scout_client.iter_dialogs():
if dialog.name == message_text:
return dialog
return None