I've created a bot that initial target is sending photos to it, after photos are sent im doing some operations in db, user always send photos in a bundle from files so they dont get compressed.
i've created a handler that is run, for every photo and in that handler im creating a timer that runs a ProcessPhoto function after 20 sec, if another photo comes it resets the timer and so on.
Is there a way to count the files sent from the user in a bundle, so if a user sends 100 photos i can delete the user_state and give him a warning spam message at the very start so i dont permit telegram uploading the photos and wasting time and resources?
if message.document.file_name.lower().endswith(
('jpg', 'png', 'jpeg', 'svg', 'heif', 'heic', 'tiff', 'tif', 'webp')):
print(message.media_group_id)
if(user_states[message.chat.id]['izvestuvanjePrikacuvanjeFajl']) == 0:
user_states[message.chat.id]['izvestuvanjePrikacuvanjeFajl'] = 1
bot_message = bot.send_message(message.chat.id,
'We are adding your files please wait....')
user_states[message.chat.id]['sent_message_ids'].add((message.chat.id, bot_message.message_id))
user_states[message.chat.id]['sent_message_ids'].add((message.chat.id, message.id))
file_id = message.document.file_id
file_info = bot.get_file(file_id)
downloaded_file = bot.download_file(file_info.file_path)
# Prepare file data for processing
timestamp = datetime.now().strftime('%Y%m%d%H%M%S%f')[:-3]
file_data = {'data': downloaded_file, 'path': save_path, 'name': f"{timestamp}.jpg"}
user_file_buffers[chat_id]['files'].append(file_data)
if user_file_buffers[chat_id]['timer']:
user_file_buffers[chat_id]['timer'].cancel()
file_count = len(user_file_buffers[chat_id]['files'])
# if(file_count > 90):
# bot_message = bot.send_message(message.chat.id,
# 'too much files')
# user_states[message.chat.id]['sent_message_ids'].add((message.chat.id, bot_message.message_id))
#
# if user_file_buffers[chat_id]['timer']:
# user_file_buffers[chat_id]['timer'].cancel()
# user_file_buffers[chat_id]['timer'] = None # Clear the timer reference
#
# # Delete user state and clean up
# funkcijaBrisenje(user_states[message.chat.id]['sent_message_ids'])
# del user_states[message.chat.id]
# del user_file_buffers[chat_id] # Also clear the file buffer for safety
#
# return # Exit function to prevent further execution
if(file_count < 60):
timer_duration = max(2.0, min(10.0, file_count * 1.5))
else:
timer_duration = max(2.0, min(20.0, file_count * 1.5))
print(timer_duration)
user_file_buffers[chat_id]['timer'] = Timer(timer_duration, process_user_files, [chat_id, message])
user_file_buffers[chat_id]['timer'].start()