forked from seriybeliy11/Pyroman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_pyromanko.py
310 lines (260 loc) · 13.8 KB
/
update_pyromanko.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
import json
from pyrogram import Client, filters, idle
from pyrogram.types import Message, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton, KeyboardButton, CallbackQuery, BotCommand
from pyrogram.handlers import MessageHandler
import datetime
from pyromod import listen
from pyrogram import *
import asyncio
import logging
api_id = ""
api_hash = ""
bot_token = ""
bot_client = Client("bot_client", api_id=api_id, api_hash=api_hash, bot_token=bot_token)
parser_client = Client("parser_client", api_id=api_id, api_hash=api_hash)
USER_DATA_FILE = "user_data.json"
id_collection = []
def load_user_data():
try:
with open(USER_DATA_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_user_data(user_data):
with open(USER_DATA_FILE, "w") as f:
json.dump(user_data, f)
user_data = load_user_data()
main_menu_keyboard = ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text='Отслеживать канал')],
[KeyboardButton(text='Добавить канал')],
[KeyboardButton(text='Удалить канал')],
[KeyboardButton(text='Список каналов')]
], resize_keyboard=True)
cache = {}
async def get_messages(client, channel, period):
cache_key = (channel, period)
if cache_key in cache:
return cache[cache_key]
messages = []
today = datetime.date.today()
this_week_start = today - datetime.timedelta(days=today.weekday())
two_hours_ago = datetime.datetime.now() - datetime.timedelta(hours=2)
one_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
if period == '1':
async for message in client.get_chat_history(chat_id=channel, limit=1000):
if message.date.date() == today and message.date <= datetime.datetime.now():
messages.append(message)
elif period == '7':
async for message in client.get_chat_history(chat_id=channel, limit=1700):
if message.date.date() >= this_week_start and message.date.date() <= datetime.datetime.now().date():
messages.append(message)
elif period == '2h':
async for message in client.get_chat_history(chat_id=channel, limit=800):
if message.date >= two_hours_ago and message.date <= datetime.datetime.now():
messages.append(message)
elif period == '1h':
async for message in client.get_chat_history(chat_id=channel, limit=800):
if message.date >= one_hour_ago and message.date <= datetime.datetime.now():
messages.append(message)
cache[cache_key] = messages
print(cache)
return messages
#patch VACCINE
async def send_messages(client, user_id, messages):
lm = []
for message in messages:
try:
if message.media_group_id is None:
if message.video:
video = await message.download(in_memory=True)
await client.send_video(chat_id=user_id, video=video, caption=message.caption.html, caption_entities=message.caption_entities, parse_mode=enums.ParseMode.HTML)
elif message.photo:
photo = await message.download(in_memory=True)
await client.send_photo(chat_id=user_id, photo=photo, caption=message.caption, caption_entities=message.caption_entities, parse_mode=enums.ParseMode.HTML)
elif message.text:
await client.send_message(chat_id=user_id, text=message.text, entities=message.entities, parse_mode=enums.ParseMode.HTML)
elif message.animation:
animation = await message.download(in_memory=True)
await client.send_animation(chat_id=user_id, animation=animation)
else:
media_group = await client.get_media_group(chat_id=message.chat.id, message_id=message.id)
for media in media_group:
if media.video:
_media = await client.download_media(media.video.file_id, in_memory=True, file_name=media.video.file_name)
lm.append({
'type': 'video',
'media': _media,
'caption': media.caption if media.caption else None
})
elif media.audio:
_media = await client.download_media(media.audio.file_id, in_memory=True)
lm.append({
'type': 'audio',
'media': _media,
'caption': media.caption if media.caption else None
})
elif media.photo:
_media = await client.download_media(media.photo.file_id, in_memory=True)
lm.append({
'type': 'photo',
'media': _media,
'caption': media.caption if media.caption else None
})
elif media.document:
_media = await client.download_media(media.document.file_id, in_memory=True, file_name=media.document.file_name)
lm.append({
'type': 'document',
'media': _media,
'caption': media.caption if media.caption else None
})
media_group_to_send = []
for item in lm:
if item['type'] == 'video':
media_group_to_send.append(types.InputMediaVideo(media=item['media'], caption=item['caption']))
elif item['type'] == 'audio':
media_group_to_send.append(types.InputMediaAudio(media=item['media'], caption=item['caption']))
elif item['type'] == 'photo':
media_group_to_send.append(types.InputMediaPhoto(media=item['media'], caption=item['caption']))
elif item['type'] == 'document':
media_group_to_send.append(types.InputMediaDocument(media=item['media'], caption=item['caption']))
await client.send_media_group(chat_id=user_id, media=media_group_to_send)
except Exception as e:
print(f'Errors: {e}')
await client.send_message(chat_id=user_id, text="Все сообщения отправлены")
@bot_client.on_message(filters.command("start"))
async def command_start(client, message: Message):
idf = str(message.from_user.id)
if idf not in id_collection:
id_collection.append(idf)
print(id_collection)
await message.reply('Welcome!', reply_markup=main_menu_keyboard)
@bot_client.on_message(filters.command("track_channel") | filters.regex(r"^Отслеживать канал$"))
async def track_channel(client, message: Message):
if not user_data:
await message.reply("У вас нет отслеживаемых каналов.")
return
channels = user_data
inline_keyboard = [[InlineKeyboardButton(channel, callback_data=f"track#{channel}")] for channel in channels]
reply_markup = InlineKeyboardMarkup(inline_keyboard)
await message.reply("Выберите канал для отслеживания", reply_markup=reply_markup)
@bot_client.on_callback_query(filters.regex(r"^track#"))
async def track_channel_callback(client, callback_query: CallbackQuery):
data = callback_query.data
user_id = str(callback_query.from_user.id)
parts = data.split('#')
channel = parts[1]
print(f'{user_id} получен при запросе на {channel}')
data_inline_keyboard = InlineKeyboardMarkup([
[InlineKeyboardButton('1 день', callback_data=f'1#day#{channel}')],
[InlineKeyboardButton('1 неделя', callback_data=f"7#day#{channel}")],
[InlineKeyboardButton('1 час', callback_data=f"1h#hour#{channel}")],
[InlineKeyboardButton('2 часа', callback_data=f"2h#hour#{channel}")],
[InlineKeyboardButton('Назад', callback_data="back")]
])
await callback_query.message.reply("Выберите период для отслеживания", reply_markup=data_inline_keyboard)
@bot_client.on_callback_query(filters.regex(r"^1#day#(.+)$|^7#day#(.+)$|^1h#hour#(.+)$|^2h#hour#(.+)$"))
async def track_channel_period_callback(client, callback_query: CallbackQuery):
user_id = str(callback_query.from_user.id)
print(user_id)
data = callback_query.data
parts = data.split('#')
channel = str(parts[2])
period = str(parts[0])
period_name = 0
if period == "7":
period_name = "неделю"
if period == "1":
period_name = "1 день"
if period == "1h":
period_name = "1 час"
if period == "2h":
period_name = "2 часа"
await bot_client.send_message(user_id, f"Начинаю вывод сообщений из канала... {channel} за {period_name}")
messages = await get_messages(parser_client, channel, period)
await send_messages(bot_client, user_id, messages)
@bot_client.on_message(filters.command("add_channel") | filters.regex(r"^Добавить канал$"))
async def add_channel(client, message: Message):
channel_input = await client.ask(message.chat.id, "Отлично! Пришлите имя канала в формате @channelname или URL канала в формате https://t.me/channelname")
channel_input = channel_input.text.strip()
if len(channel_input) < 64:
if channel_input.startswith("@"):
channel_name = channel_input
elif channel_input.startswith("https://t.me/"):
channel_name = "@" + channel_input.split("/")[-1]
else:
inline_keyboard = [[InlineKeyboardButton("Назад", callback_data="back")]]
reply_markup = InlineKeyboardMarkup(inline_keyboard)
await message.reply(f"Invalid input. Please make sure that the input is in the format @channelname or https://t.me/channelname.", reply_markup=reply_markup)
return
if channel_name in user_data:
await message.reply(f"Channel {channel_name} is already in the list of channels.")
return
user_data[channel_name] = None
save_user_data(user_data)
await message.reply(f"Channel {channel_name} added.")
else:
await message.reply(f"Invalid channelname")
@bot_client.on_message(filters.command("remove_channel") | filters.regex(r"^Удалить канал$"))
async def remove_channel(client, message: Message):
if not user_data:
await message.reply("У вас нет отслеживаемых каналов.")
return
channels = user_data
inline_keyboard = [[InlineKeyboardButton(channel, callback_data=f"remove#{channel}")] for channel in channels]
inline_keyboard.append([InlineKeyboardButton("Назад", callback_data="back")])
reply_markup = InlineKeyboardMarkup(inline_keyboard)
await message.reply("Выберите канал, который нужно удалить", reply_markup=reply_markup)
@bot_client.on_callback_query(filters.regex(r"^remove#"))
async def remove_channel_callback(client, callback_query: CallbackQuery):
channel = callback_query.data.split("#")[1]
user_id = str(callback_query.from_user.id)
if channel not in user_data:
await callback_query.message.edit_text("Канал больше не существует в базе данных.")
return
del user_data[channel]
save_user_data(user_data)
await callback_query.message.edit_text(f"Channel {channel} removed.")
@bot_client.on_callback_query(filters.regex("^back"))
async def back_command(client: Client, callback_query: CallbackQuery):
await callback_query.message.reply("Вы вернулись в главное меню.")
@bot_client.on_message(filters.command("list_channel") | filters.regex(r"^Список каналов$"))
async def list_channels(_, message: Message):
if not user_data:
await message.reply("У вас нет отслеживаемых каналов.")
return
channels = user_data
inline_keyboard = [[InlineKeyboardButton(channel, callback_data=channel)] for channel in channels]
inline_keyboard.append([InlineKeyboardButton("Назад", callback_data="back")])
reply_markup = InlineKeyboardMarkup(inline_keyboard)
await message.reply("Список каналов", reply_markup=reply_markup)
bot_client.add_handler(MessageHandler(command_start, filters.command(commands='start')))
bot_client.add_handler(MessageHandler(track_channel, filters.command(commands='track_channel')))
bot_client.add_handler(MessageHandler(add_channel, filters.command(commands='add_channel')))
bot_client.add_handler(MessageHandler(remove_channel, filters.command(commands='remove_channel')))
bot_client.add_handler(MessageHandler(list_channels, filters.command(commands='list_channels')))
bot_commands = [
BotCommand(
command='start',
description='get started'
),
BotCommand(
command='track_channel',
description='Pick channel for tracking'
),
BotCommand(
command='add_channel',
description='Add channel to list for tracking'
),
BotCommand(
command='remove_channel',
description='Remove channel from the list for tracking'
),
BotCommand(
command='list_channels',
description='Show list for tracking'
),
]
bot_client.start()
parser_client.start()
bot_client.set_bot_commands(bot_commands)
idle()