I need a telegram bot based on python-telegram-bot 3.17 to decrypt and unzip the files…
- User
I need a telegram bot based on python-telegram-bot 3.17 to decrypt and unzip the files with extension ".zip.enc" that users forward them to it decrypt command: openssl aes-128-cbc -d -md md5 -nosalt -pass pass:braiins -in "forwarded_file" -out "random_name" unzip command: unzip -q "random_name".zip -d "random_name"
The path of files: ver=latest filepath="out_name"/filesystem/etc/bosminer.toml pingpath="out_name"/filesystem/var/log/metrics/metrics.prom logpath="out_name"/filesystem/var/log/bosminer/bosminer.log.* uptimepath="out_name"/builtin/public_ip localip="out_name"/filesystem/etc/bosminer-autotune.json hwidpah="out_name"/filesystem/etc/bos_refid platformpath="out_name"/command/dmesg cpupath="$out_name"/filesystem/proc/cpuinfo
The sample analyze commands for Device Overview in bashscript which you can convert it to python: ============= Device Overview ===============
board=$filepath/board.json
if [[ -f "$board" ]]; then
sed -e '4q;d' $filepath/board.json | xargs -L1 echo "Board"
fi
grep -o 'BHB[0-9]*' logpath | cut -d' -f2 | awk '{print 1}' | xargs -L1 echo "PSU type: " if [[ -f platformpath ]]; then cat cpupath | cut -d':' -f2 | sed 's/Generic AM33XX/BeagleBone/' | xargs -L1 echo "CB Type: " cat filepath/bos_version | xargs -L1 echo "Version: " printf "hwidpah | xargs -L1 echo "HW ID: " printf "localip | cut -d: -f2 | awk '{print 1}' | xargs -L1 echo "Local IP: " if [[ -f ippath ]]; then cat uptimepath ]]; then printf "Uptime: " && awk '{print int(1%3600)/60)":"int(uptimepath else echo "Uptime: Not found!"
fi if [[ ! -f refidpath | xargs -L1 echo "Ref ID: " fi - Assistant
I can definitely help you build this Telegram Bot based on python-telegram-bot 3.17. Here's a sample code to get you started:
import os import subprocess from telegram.ext import Updater, CommandHandler # Define the decrypt command def decrypt(update, context): # Get the file object that the user forwarded file_obj = context.bot.getFile(update.message.document.file_id) # Fetch the file name and its extension file_name = file_obj.file_path.split("/")[-1] file_extension = file_name.split(".")[-1] # Check if the file extension is .zip.enc if file_extension == "enc" and "zip" in file_name: # Download the file file_path = file_obj.download() # Decrypt the file using openssl decrypted_file_path = file_path.replace(".enc", "") password = "braiins" decrypt_command = f'openssl aes-128-cbc -d -md md5 -nosalt -pass pass:{password} -in "{file_path}" -out "{decrypted_file_path}"' subprocess.call([decrypt_command], shell=True) # Unzip the decrypted file unzipped_file_path = decrypted_file_path.split(".")[0] unzip_command = f'unzip -q "{decrypted_file_path}" -d "{unzipped_file_path}"' subprocess.call([unzip_command], shell=True) # Send the unzipped file back to the user unzip_file_name = os.listdir(unzipped_file_path)[0] context.bot.send_document(chat_id=update.effective_chat.id, document=open(f"{unzipped_file_path}/{unzip_file_name}", 'rb')) else: context.bot.send_message(chat_id=update.effective_chat.id, text="Please forward only '.zip.enc' files.") # Define the Device Overview command def device_overview(update, context): # Define the paths of the files ver = "latest" out_name = "/path/to/files" filepath = f"{out_name}/filesystem/etc" configpath = f"{out_name}/filesystem/etc/bosminer.toml" pingpath = f"{out_name}/builtin/ping_report" metricspath = f"{out_name}/filesystem/var/log/metrics/metrics.prom" logpath = f"{out_name}/filesystem/var/log/bosminer/bosminer.log" loggzpath = f"{out_name}/filesystem/var/log/bosminer/bosminer.log.*" uptimepath = f"{out_name}/filesystem/proc/uptime" ippath = f"{out_name}/builtin/public_ip" localip = f"{out_name}/command/ifconfig_-a" profilepath = f"{out_name}/filesystem/etc/bosminer-autotune.json" hwidpah = f"{out_name}/filesystem/tmp/miner_hwid" refidpath = f"{out_name}/filesystem/etc/bos_refid" platformpath = f"{out_name}/filesystem/etc/bos_platform" dmesgpath = f"{out_name}/command/dmesg" cpupath = f"{out_name}/filesystem/proc/cpuinfo" # Fetch the device overview information and send it back to the user result = [] board_path = f"{filepath}/board.json" if os.path.exists(board_path): board = subprocess.check_output(['sed', '-e', '4q;d', board_path, '|', 'xargs', '-L1', 'echo', 'Board']) result.append(board) variant = subprocess.check_output([f"grep -o 'BHB[0-9]*' {dmesgpath} | xargs -L1 echo 'Variant: '"]) result.append(variant) psu_type = subprocess.check_output([f"grep -m1 'PSU: version' {logpath} | cut -d\' -f2 | awk '{{print $1}}' | xargs -L1 echo 'PSU type: '"]) result.append(psu_type) if os.path.exists(platformpath): platform = subprocess.check_output([f"cat {filepath}/bos_platform | xargs -L1 echo 'Platform: '"]) result.append(platform) cb_type = subprocess.check_output([f"grep 'Hardware' {cpupath} | cut -d':' -f2 | sed 's/Generic AM33XX/BeagleBone/' | xargs -L1 echo 'CB Type: '"]) result.append(cb_type) bos_mode = subprocess.check_output([f"cat {filepath}/bos_mode | xargs -L1 echo 'BOS+ Mode: '"]) result.append(bos_mode) bos_version = subprocess.check_output([f"cat {filepath}/bos_version | xargs -L1 echo 'Version: '"]) result.append(bos_version) with open(hwidpah) as f: hw_id = f.read() hw_id = f'Hw ID: {hw_id}' result.append(hw_id) local_ip = subprocess.check_output([f"grep -m1 'inet addr' {localip} | cut -d: -f2 | awk '{{print $1}}' | xargs -L1 echo 'Local IP: '"]) result.append(local_ip) if os.path.exists(ippath): public_ip = subprocess.check_output([f"cat {ippath} | xargs -L1 echo 'Public IP: '"]) result.append(public_ip) else: result.append("Public IP: Not found!") if os.path.exists(uptimepath): uptime = subprocess.check_output([f"awk '{{print int($1/3600)\\\":\\\"int(($1%3600)/60)\\\":\\\"int($1%60)}}' {uptimepath}"]) uptime = f'Uptime: {uptime}' result.append(uptime) else: result.append("Uptime: Not found!") if os.path.exists(refidpath): with open(refidpath) as f: ref_id = f.read() ref_id = f'Ref ID: {ref_id}' result.append(ref_id) else: result.append("Ref ID: Not found!") context.bot.send_message(chat_id=update.effective_chat.id, text='\n'.join(result)) # Set up the Telegram Bot updater = Updater(token="YOUR_BOT_TOKEN", use_context=True) dispatcher = updater.dispatcher # Set up the command handlers dispatcher.add_handler(CommandHandler("decrypt", decrypt)) dispatcher.add_handler(CommandHandler("device_overview", device_overview)) # Start the Bot updater.start_polling() updater.idle()Please note that the code above may need some adjustments depending on the actual file paths in your system. Let me know if you have any questions or if you need further assistance in building the full Telegram Bot.