From 153d9a0a649f83e5335e9f668f57fdfa8a9f3e35 Mon Sep 17 00:00:00 2001 From: hlf20010508 Date: Thu, 14 Sep 2023 20:18:33 +0800 Subject: [PATCH] perf: offer more information of error - offer more information of error with details and analysis - use pre-built cryptg docker image to accelerate building progress --- Dockerfile | 9 ++------- README.md | 2 +- bot.py | 53 +++++++++++++++++++++++++++++++++++------------------ log.py | 11 +++++++++-- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Dockerfile b/Dockerfile index 68933ca..54dc17f 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,7 @@ -FROM python:3.8.16-alpine3.17 AS cryptg_builder -RUN apk add --update --no-cache rustup build-base &&\ - rustup-init -y &&\ - source $HOME/.cargo/env &&\ - pip install cryptg==0.4.0 - +FROM hlf01/cryptg:cryptg0.4.0-python3.8.16-alpine3.17 AS cryptg_builder FROM python:3.8.16-alpine3.17 WORKDIR /telegram-onedrive COPY ./ ./ RUN apk add --update --no-cache libgcc &&\ pip install --no-cache-dir telethon requests flask onedrivesdk==1.1.8 -COPY --from=cryptg_builder /usr/local/lib/python3.8/site-packages/cryptg /usr/local/lib/python3.8/site-packages +COPY --from=cryptg_builder /cryptg /usr/local/lib/python3.8/site-packages diff --git a/README.md b/README.md index cef55b3..433bb1c 100755 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Example: - In the group or channel, forward or upload files(or videos, photos). - If you want to transfer restricted content from a group or channel, right click the content, copy the message link, and send the link. - Wait until the transfer completes. You can check status on pinned status message. -- Use `/help` for more information about other commands. +- Use `/help` for more information about other command. ## Preparation 1. Open `docker-compose.yml` and edit the environment config. diff --git a/bot.py b/bot.py index 2f168d1..9fb07e1 100644 --- a/bot.py +++ b/bot.py @@ -321,14 +321,17 @@ async def url(event): _url = cmd[0] name = _url.split('/')[-1] except: - await delete_message(event) - await event.respond(''' + await event.reply(''' Command `/url` format wrong. Usage: `/url` file_url ''') raise events.StopPropagation + if not get_link(_url): + await event.reply(logger("Please offer an HTTP url.")) + raise events.StopPropagation + try: progress_url = onedrive.upload_from_url(_url) print(progress_url) @@ -343,16 +346,30 @@ async def url(event): status = "Uploaded: %.2f%%" % float(progress['percentageComplete']) logger(status) - logger("File uploaded to %s"%os.path.join(onedrive.remote_root_path, name)) - msg_link = 'https://t.me/c/%d/%d'%(event.message.peer_id.channel_id, event.message.id) - await tg_bot.edit_message(status_bar, 'Status:\n\n%s\n\n%s'%(msg_link, status)) - if not delete_flag: - await event.reply('Done.') - await delete_message(event) - await tg_bot.edit_message(status_bar, 'Status:\n\nNo job yet.') + if 'fail' not in str(progress) and 'error' not in str(progress): + logger("File uploaded to %s"%os.path.join(onedrive.remote_root_path, name)) + msg_link = 'https://t.me/c/%d/%d'%(event.message.peer_id.channel_id, event.message.id) + await tg_bot.edit_message(status_bar, 'Status:\n\n%s\n\n%s'%(msg_link, status)) + if not delete_flag: + await event.reply('Done.') + await delete_message(event) + await tg_bot.edit_message(status_bar, 'Status:\n\nNo job yet.') + else: + await event.reply(logger('Error: something is wrong\n\nResponse: %s' % progress)) + await event.reply(logger("Analysis: try again later, or offer a proper url")) + except Exception as e: - logger(e) - await event.reply('Error: %s'%e) + await event.reply('Error: %s\nResponse: %s' % (logger(e), logger(progress))) + try: + if progress['errorCode'] == 'ParameterIsTooLong': + await event.reply(logger("Analysis: url too long.OneDrive API doesn't support long url.")) + elif progress['errorCode'] == 'Forbidden': + await event.reply(logger("Analysis: url protocol is not HTTP, or the url has been forbidden.")) + elif progress['errorCode'] == 'NotFound': + await event.reply(logger("Analysis: content not found.")) + except Exception as e: + logger(e) + raise events.StopPropagation @@ -370,8 +387,7 @@ async def links(event): for offset in range(offsets): await tg_client.send_message(event.chat_id, message='%s/%d'%(link_body, head_message_id + offset)) except: - await delete_message(event) - await event.respond(''' + await event.reply(''' Command `/links` format wrong. Usage: `/links` message_link range @@ -418,8 +434,7 @@ async def callback(current, total): await delete_message(message) await tg_bot.edit_message(status_bar, "Status:\n\nNo job yet.") except Exception as e: - logger(e) - await event.reply('Error: %s'%e) + await event.reply('Error: %s' % logger(e)) else: msg_link = get_link(event.text) @@ -454,10 +469,12 @@ async def callback(current, total): await tg_bot.edit_message(status_bar, "Status:\n\nNo job yet.") except Exception as e: - logger(e) - await event.reply('Error: %s'%e) + await event.reply('Error: %s' % logger(e)) else: - await event.reply("Message not found.") + await event.reply(logger("Message not found.")) + else: + if event.text != '/auth': + await event.reply("Use /help for available command.") raise events.StopPropagation diff --git a/log.py b/log.py index 83e5b40..f9ef508 100644 --- a/log.py +++ b/log.py @@ -1,10 +1,17 @@ from datetime import datetime +from traceback import print_exc +from io import StringIO log_path = 'log' def logger(message): with open(log_path, 'a') as log_file: time = datetime.now() - message = '%s\n%s\n'%(time, message) + if isinstance(message, Exception): + message = StringIO() + print_exc(file=message) + message = message.getvalue() + message = '%s\n%s\n' % (time, message) print(message, end='') - log_file.write(message) \ No newline at end of file + log_file.write(message) + return message \ No newline at end of file