I have this code and I want to be able run this with multiple users at the same time, so…
- User
I have this code and I want to be able run this with multiple users at the same time, so they files should’ve mixed in the server.
def referral(update, context): if not is_allowed_user(update.message.from_user.id): return update.message.reply_text('Please enter the referral ID:') return GET_REFID
def get_refid(update, context): if update.message.text.lower() == "/cancel": return cancel(update, context) refid = update.message.text
# Load referral IDs from the JSON file with open("referrals.json", "r") as f: referrals = json.load(f) if refid in referrals: context.user_data['refid'] = refid update.message.reply_text('referral ID saved. Now, please enter the company name (without space):') return GET_CONAME else: update.message.reply_text('Invalid referral ID. Please enter a valid referral ID:') return GET_REFIDdef get_coname(update, context): refid = context.user_data['refid'] if update.message.text.lower() == "/cancel": return cancel(update, context) coname = update.message.text context.user_data['coname'] = coname
update.message.reply_text("Please wait, I'm processing…") for zip_file, tar_ext in zip(ZIP_FILES, TAR_EXTENSIONS): # Extract ZIP file with zipfile.ZipFile(zip_file, 'r') as zip_ref: zip_ref.extractall() # Rename binary os.rename('braiins-toolbox', f'braiins_toolbox_{refid}') # Add executable permission os.chmod(f'braiins_toolbox_{refid}', os.stat(f'braiins_toolbox_{refid}').st_mode | stat.S_IEXEC) # Compress with TAR output_file = f'braiins_toolbox_{tar_ext}_{coname}.tar.gz' with tarfile.open(output_file, 'w:gz') as tar: tar.add(f'braiins_toolbox_{refid}', arcname=f'braiins_toolbox_{refid}', filter=lambda tarinfo: tarinfo) # Cleanup - only remove the file if it exists if os.path.isfile(f'braiins_toolbox_{refid}'): os.remove(f'braiins_toolbox_{refid}') # Send the file to the user message_text = f'\n"{coname}" \n"{refid}"' with open(output_file, 'rb') as f: context.bot.send_document( chat_id=update.effective_chat.id, document=InputFile(f), filename=output_file, caption=message_text, ) os.remove(output_file) final_message = f'''==============
Company Name: {coname} referral ID:
{refid}Remote Installation on [Beaglebone/Amlogic]:
Download the proper braiins-toolbox from the files above and run the command below. (please note the suffix of toolbox name should be your referral ID):
./braiins_toolbox_{refid} firmware install --ip-file list.txtIf you want to use old toolbox you can download it with your referral ID here: ○ Windows Version ○ Linux Version
./bos-toolbox_{refid} update list.csv bos-referralSD Images: ○ Antminer X19 (Beaglebone) SD ○ Antminer X19 (Zynq) SD
============== ''' update.message.reply_text(final_message, parse_mode='Markdown') update.message.reply_text('⚠️ Hide sender’s name! when you want to forward the messages') return ConversationHandler.END
add user chat id in the path to manage this
- Assistant
To manage multiple users in this code and prevent their files from mixing on the server, you can use the user's id as a separate folder while creating and managing the files. Here's the modified version of the code:
import os def get_coname(update, context): refid = context.user_data['refid'] if update.message.text.lower() == "/cancel": return cancel(update, context) coname = update.message.text context.user_data['coname'] = coname update.message.reply_text("Please wait, I'm processing…") userid = update.message.from_user.id os.makedirs(f'user_{userid}', exist_ok=True) for zip_file, tar_ext in zip(ZIP_FILES, TAR_EXTENSIONS): # Extract ZIP file with zipfile.ZipFile(zip_file, 'r') as zip_ref: zip_ref.extractall(f'user_{userid}') # Rename binary os.rename(os.path.join(f'user_{userid}', 'braiins-toolbox'), f'user_{userid}/braiins_toolbox_{refid}') # Add executable permission os.chmod(f'user_{userid}/braiins_toolbox_{refid}', os.stat(f'user_{userid}/braiins_toolbox_{refid}').st_mode | stat.S_IEXEC) # Compress with TAR output_file = f'user_{userid}/braiins_toolbox_{tar_ext}_{coname}.tar.gz' with tarfile.open(output_file, 'w:gz') as tar: tar.add(f'user_{userid}/braiins_toolbox_{refid}', arcname=f'braiins_toolbox_{refid}', filter=lambda tarinfo: tarinfo) # Cleanup - only remove the file if it exists if os.path.isfile(f'user_{userid}/braiins_toolbox_{refid}'): os.remove(f'user_{userid}/braiins_toolbox_{refid}') # Send the file to the user message_text = f'\n"{coname}" \n"{refid}"' with open(output_file, 'rb') as f: context.bot.send_document( chat_id=update.effective_chat.id, document=InputFile(f), filename=os.path.basename(output_file), caption=message_text, ) os.remove(output_file) final_message = f''' ============== Company Name: **{coname}** referral ID: `{refid}` ... ============== ''' update.message.reply_text(final_message, parse_mode='Markdown') update.message.reply_text('⚠️ Hide sender’s name! when you want to forward the messages') return ConversationHandler.ENDIn this code, I've created a unique folder for each user based on their chat ID, ensuring that their files don't mix on the server. Make sure to add
import osat the beginning of your script if it's not already there.