I have a telegram bot, this is the code: import os import re import shutil import…
- User
I have a telegram bot, this is the code:
import os import re import shutil import zipfile import subprocess from pathlib import Path from io import BytesIO from urllib.parse import urlsplit
import requests from telegram import Update, InputFile from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
TOKEN = "2028600450:AAH7N-rovp1rmJ_HmCd_3IuQtaMGyPRl71M" DOWNLOADS_DIR = Path("incoming_logs") if not DOWNLOADS_DIR.exists(): DOWNLOADS_DIR.mkdir()
ZIP_DOWNLOAD_DIRECTORY = Path('incoming_logs') DECRYPTED_DIRECTORY = Path('decrypted_income_file') REPORT_CASE_DIRECTORY = Path('report_case')
def start(update: Update, context: CallbackContext): update.message.reply_text("Hi, I'm the File Decrypt and Parse bot.") update.message.reply_text("Please forward a file with .enc extension and I'll decrypt and parse it.")
def handle_file(update: Update, context: CallbackContext): document = update.message.document
if not document.file_name.endswith('.enc'): update.message.reply_text("File is not acceptable. Please send a file with .enc extension.") return file_id = document.file_id file_obj = context.bot.get_file(file_id) file_name_tuple = document.file_name.split(".", 1) in_file_name = Path(DOWNLOADS_DIR, f"{file_name_tuple[0]}.zip.enc") file_obj.download(custom_path=str(in_file_name)) update.message.reply_text("File received. Processing…") # Decrypt the file outfile_name = DOWNLOADS_DIR / f"{file_name_tuple[0]}.zip" openssl_cmd = f"openssl aes-128-cbc -d -md md5 -nosalt -pass pass:braiins -in {in_file_name} -out {outfile_name}" os.system(openssl_cmd) # Extract the zip file report_case_path = REPORT_CASE_DIRECTORY / f"{file_name_tuple[0]}" if not report_case_path.exists(): report_case_path.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(outfile_name, "r") as zip_ref: zip_ref.extractall(report_case_path) outfile_name.unlink() parse_report(report_case_path, update)def parse_report(report_case_path, update): file_paths = { "filepath": report_case_path / "filesystem/etc", "configpath": report_case_path / "filesystem/etc/bosminer.toml", "pingpath": report_case_path / "builtin/ping_report", "metricspath": report_case_path / "filesystem/var/log/metrics/metrics.prom", "logpath": report_case_path / "filesystem/var/log/bosminer/bosminer.log", "loggzpath": report_case_path / "filesystem/var/log/bosminer/bosminer.log.", "uptimepath": report_case_path / "filesystem/proc/uptime", "ippath": report_case_path / "builtin/public_ip", "localip": report_case_path / "command/ifconfig_-a", "profilepath": report_case_path / "filesystem/etc/bosminer-autotune.json", "hwidpath": report_case_path / "filesystem/tmp/miner_hwid", "refidpath": report_case_path / "filesystem/etc/bos_refid", "platformpath": report_case_path / "filesystem/etc/bos_platform", "dmesgpath": report_case_path / "command/dmesg", "cpupath": report_case_path / "filesystem/proc/cpuinfo" }
variant = subprocess.check_output(f'''grep -o "BHB[0-9]*" {file_paths["dmesgpath"]} || echo "Variant: Not found!"''', shell=True) psu_type = subprocess.check_output(f'''grep -m1 "PSU: version" {file_paths["logpath"]} | cut -d"'" -f2 | awk "{{print $1}}" || echo "PSU type: Not found!"''', shell=True) if file_paths["platformpath"].is_file(): platform = subprocess.check_output(f"cat {file_paths['filepath']}/bos_platform", shell=True, text=True) platform = f"Platform: {platform.strip()}" else: platform = "Platform: Not found!" cb_type = subprocess.check_output(f'grep "Hardware" {file_paths["cpupath"]} | cut -d":" -f2 | sed "s/Generic AM33XX/BeagleBone/" || echo "CB Type: Not found!"', shell=True) bos_mode = subprocess.check_output(f"cat {file_paths['filepath']}/bos_mode || echo 'BOS+ Mode: Not found!'", shell=True) bos_version = subprocess.check_output(f"cat {file_paths['filepath']}/bos_version || echo 'Version: Not found!'", shell=True) hwid = subprocess.check_output(f"cat {file_paths['hwidpath']} || echo 'HW ID: Not found!'", shell=True) local_ip = subprocess.check_output(f'''grep -m1 "inet addr" {file_paths["localip"]} | cut -d: -f2 | awk "{{print $1}}" || echo "Local IP: Not found!"''', shell=True) if file_paths["ippath"].is_file(): public_ip = subprocess.check_output(f"cat {file_paths['ippath']} || echo 'Public IP: Not found!'", shell=True) else: public_ip = "Public IP: Not found!" if file_paths["uptimepath"].is_file(): uptime = subprocess.check_output(f'''awk "{{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}}" {file_paths["uptimepath"]} || echo "Uptime: Not found!"''', shell=True) else: uptime = "Uptime: Not found!" if file_paths["refidpath"].is_file(): ref_id = subprocess.check_output(f"cat {file_paths['refidpath']} || echo 'Ref ID: Not found!'", shell=True) else: ref_id = "Ref ID: Not found!" result = f"Variant: {variant.decode('utf-8').strip()}\nPSU type: {psu_type.decode('utf-8').strip()}\n{platform}\nCB Type: {cb_type.decode('utf-8').strip()}\nBOS+ Mode: {bos_mode.decode('utf-8').strip()}\nVersion: {bos_version.decode('utf-8').strip()}\nHW ID: {hwid.decode('utf-8').strip()}\nLocal IP: {local_ip.decode('utf-8').strip()}\n{public_ip.strip()}\nUptime: {uptime.decode('utf-8').strip()}\nRef ID: {ref_id.decode('utf-8').strip()}" update.message.reply_text(result)def main(): updater = Updater(token=TOKEN)
dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(MessageHandler(Filters.document, handle_file)) updater.start_polling() updater.idle()if name == 'main': main()
It gives these errors:
*** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. awk: cmd. line:1: error: Unmatched ( or (: /3600):int((%3600)/ No error handlers are registered, logging exception. Traceback (most recent call last): File "/usr/local/lib/python3.10/dist-packages/telegram/ext/dispatcher.py", line 555, in process_update handler.handle_update(update, self, check, context) File "/usr/local/lib/python3.10/dist-packages/telegram/ext/handler.py", line 198, in handle_update return self.callback(update, context) File "/root/bot2/./logreader.py", line 59, in handle_file parse_report(report_case_path, update) File "/root/bot2/./logreader.py", line 113, in parse_report result = f"Variant: {variant.decode('utf-8').strip()}\nPSU type: {psu_type.decode('utf-8').strip()}\n{platform}\nCB Type: {cb_type.decode('utf-8').strip()}\nBOS+ Mode: {bos_mode.decode('utf-8').strip()}\nVersion: {bos_version.decode('utf-8').strip()}\nHW ID: {hwid.decode('utf-8').strip()}\nLocal IP: {local_ip.decode('utf-8').strip()}\n{public_ip.strip()}\nUptime: {uptime.decode('utf-8').strip()}\nRef ID: {ref_id.decode('utf-8').strip()}" AttributeError: 'str' object has no attribute 'decode' ^C^CExiting immediately!
- Assistant
It seems that
AttributeError: 'str' object has no attribute 'decode'error occurs because you're trying to usedecodefunction on a string object. To fix this, just remove the.decode('utf-8')parts in the "result" string building section.The fixed
parse_reportfunction should look like this:def parse_report(report_case_path, update): # ... (the rest of the code above here) result = f"Variant: {variant.strip()}\nPSU type: {psu_type.strip()}\n{platform}\nCB Type: {cb_type.strip()}\nBOS+ Mode: {bos_mode.strip()}\nVersion: {bos_version.strip()}\nHW ID: {hwid.strip()}\nLocal IP: {local_ip.strip()}\n{public_ip.strip()}\nUptime: {uptime.strip()}\nRef ID: {ref_id.strip()}" update.message.reply_text(result)This should fix the AttributeError you encountered. However, there is still another issue with the awk command that might cause problems in your output. The correct awk command should look like this:
uptime = subprocess.check_output(f'''awk '{{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}}' {file_paths["uptimepath"]} || echo "Uptime: Not found!"''', shell=True)Pay attention to the single and double quotes in the awk command. The above changes in the
parse_reportfunction should fix both errors.